gous md
gous md

Reputation: 147

How to achieve Statefullness in Microservices

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

Answers (2)

Ankur Kumar
Ankur Kumar

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:

  • Leverage reactive microservices approach to use events (in the form of messages) as means of communications between Product & Checkout Service. For example - an item added to cart will generate an event and Checkout service can process that event by listening to the queue. Read more about reactive microservices here: Reactive Microservices
  • Both Product & Checkout service should have its own database for storing data, which is another underlying principle of microservices to keep datastore separate (even though data is being stored at multiple places - DRY principle does not apply here)
  • In terms of communication, you can use REST interface between services to call each other (with the help of any API gateway or similar solution for security or any mediation)
  • You can also introduce ShoppingCart Service, which can have its own datastore to store cart objects. Essentially, all services having its own datastore and shared state should be kept minimum and only to be accessed using REST interface between services.
  • You can use a caching solution for shared session data such as Couchbase, Redis etc.

See sample implementation by Kbastani here: Sample Microservices Code

Upvotes: 0

Constantin Galbenu
Constantin Galbenu

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

Related Questions