ewolff / microservice

Sample of a Microservice setup for my book. Based on Spring Cloud / Netflix / Java / Docker / Docker Compose / Docker Machine / Vagrant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How would you implement a shopping cart?

fl0tsch opened this issue · comments

Thanks for writing the book "Das Microservices-Praxisbuch". I really enjoyed reading it. The full running examples are amazing.
But I still have some questions, especially about some concepts of SCS.

I am struggeling to implement a shopping cart.

(1) In your example the form for adding order lines is in the order service. Usually in a web shop each item has a "add to cart" button (catalog service). So the best way to do this, would be some frontend integration and call the "addLine" functionality (order service) from catalog service? For sure the full shopping cart page will be provided by the order service.
addToCart

(2) If I have a none logged in user, how can I save the cart on server? I need some kind of identifier. Can I use the sessionId (and sticky sessions when I have a loadbalancer) or would it break the concept of SCS.

(3) Somewhere in the checkout process (order service) you have to validate the item data like price (never trust the frontend) from the catalog service. In the SCS way there should not be synchronous calls, so the order service has to know all prices of all items forehanded (through async call). But then there is still the possibility to checkout an item without the correct price (when price update event from catalog service was not processed in the order service).

Thanks for the nice feedback! Let me quickly mention the English translation of the book https://practical-microservices.com/

Disclaimer: This example is meant as a technology demo. The architecture isn't that great. https://github.com/ewolff/microservice-istio ist better in that regard.

Concerning your questions:

1 - "Add to shopping cart" should submit a POST to the Order service. Parameters should be the ID of the product and a URL to redirect to after the POST. That way Order would add the item and then redirect to the product page.

2 - I'd save the shopping cart in the databae and provide the ID of the shopping cart as a cookie. A session is no good for a shopping cart as the shopping cart would be empty when the session times out. The cookie also solves the problem with the load balancer. Any instance can load the shopping cart from the database and use it. Of course caching and routing requests with a specific cookie to a specific instance might improve response times.

3 - Checkout and catalog should both have all information for the domain in question. This is the concept of bounded context from domain-driven design. That includes the price. It might be redundant or maybe there is a difference i.e. a rebate that is only calculated during check out.

Hope this helps. I'll close the issue for now. Feel free to open it again if needed.

Thanks a lot for your detailed and quick response. Regarding your answers I have 2 more questions:

(1) Would it also possible to do this with Ajax, instead of a redirect? Or is this not the SCS way?

(3) Just for a better understanding. When I have for instance an Inventory service and I want to display the number in stock, the catalog service stores the available number of each item (redundant the inventory service).
stock

1 - You could do optimizations, of course. I would try to use some kind of POST. Also I would try to ensure that the counter for the items in the shopping cart is the responsibility of the order service not the catalog service. https://github.com/ewolff/crimson-assurance-demo shows more advanced integration using Javascript in particular for the Postbox. It is also described in the book.

3 - why would you have a separate inventory service? Is the data really the same or are some items reserved? Are number made up to show shortage and encourage people to buy? Should the numbers be the most up to date or is some time lag ok? Note that the number on the page might be outdated because the page does not change when a new order arrives. Ideally all information should be in one bounded context including in this case the inventory. So this is really a question about how to design bounded contexts and organize domain logic. I am afraid there is no simple solution. Sorry.

Thanks again for your response.

I'm not working on a real product. I'm just playing around and want know more about SCS.

For Completion: Maybe in the inventory service I want to save all data about when and where items were bought. In my opinion this kind of data does not belong to the catalog service.

I meant when I have an online shop and I buy something from a wholesaler or a vendor. Sorry for the confusion.