Reputation: 147
I have two microservices 1) Product Microservices 2) Checkout Microservices both are spring boot projects. In Checkout Microservice , i should get all the products that i have shopped, means my microservices should be STATEFUL to know what happend previously. Please suggest examples on how to achieve the statefulness it can be like Asyncronous with event source with Kafka/RabbitMQ. But please suggest architecture, code, example in detail how to get product details in checkout service.
Upvotes: 2
Views: 6062
Reputation: 104
Firstly, Microservices are fine-grained services and as per design, they should be stateless. This helps them to scale as processes without any overhead of statefulness. My recommendations are:
See sample implementation by Kbastani here: Sample Microservices Code
Upvotes: 0
Reputation: 17693
You make an stateful microservice by attaching a shared resource. In your case, you attach or use a database that stores all the products that customers have buyed. A shared resource means that the database should be accessible my multiple instances of the microservice. In case one microservice fails and you must start another instance then the data is not lost. This helps also if you want to scale the microservice by running multiple instances at the same time.
it can be like Asyncronous with event source with Kafka/RabbitMQ
You use events to synchronize between multiple microservices. For example, the Product microservice
publishes an event (ProductTitleChanged
event) that is picked up by Checkout microservice
to update its state (a command is issued to UpdateProductTitle
). This is needed because microservices duplicate some data from one to another in order to achieve greater resilience (i.e. one can function even if another is down).
But please suggest architecture, code, example in detail how to get product details in checkout service
There are just too many architectures on too many levels. One that I particularly like is CQRS with Event sourcing. In this architecture one microservice persist the events to an Event store. Then, the other microservices poll the Event store and get the new published events. In this way, the Checkout microservice could get all product related events (like ProductAddedToInventory
, ProductChangedTitle
, ProductChangedDescription
etc) and maintain a local list of all products but with only the properties that are relevant to it. So, when the Checkout microservice needs to display the product's title it won't make a remote call to the Inventory microservice but query its local database; this increases the resilience and also the speed.
Upvotes: 1