In this post, we will look at three important patterns that can help you create microservices. We look into how event sourcing, sagas, and CQRS patterns effect the development of microservices.
The microservices architectural style is gaining a significant amount of popularity in the industry right now.
However, building microservices is not easy.
Event sourcing tries to solve the problem around atomically updating the database and also publishing an event.
What this means is that when you do something on a Domain Entity, it can be treated like a Domain Event. And the state of the Domain Entity is stored as a sequence of these Domain Events. In other words, a new event is created whenever a new record is inserted or something is updated on an existing record. An event store keeps track of all the events occuring on the entity.
Now, let's assume the following transactions occur on a particular account:
Below is how it will be stored in an event sourcing way:
Whenever there is a request for the current state of the domain object, the events are replayed and the state is constructed.
CQRS stands for Command-Query Responsibility Segregation. Often, CQRS is implemented in conjunction with Event Sourcing.
The main use of CQRS is to get around the problem arising from API Composition.
In a typical Event Sourcing and CQRS setup, a CQRS application listens to domain events from multiple applications. Using these updates, it creates and maintains a query database. Depending on the business case, this query database can aggregate complex queries and serve them whenever a request for such queries comes up in the application.
Saga Pattern is a direct solution to implementing distributed transactions in a microservices architecture.
Typically, in a microservice based application, each microservice will have its own database. However, certain business processes will require communication between multiple microservices. That's where Saga Pattern comes in. A typical Saga implementation can be seen as a sequence of local transactions where each transaction does a small part of the overall work of the Saga. In other words, a Saga Pattern is pretty much a necessity in a microservices architecture.
Let's see a simple example in a typical food delivery app flow.
When a user places an order, the sequence of actions that could happen are:
Sagas can also be of different types such as choreography-based or orchestration-based. An orchestration-based Saga can be implemented by using the Spring Boot and Axon frameworks.
Thanks for reading ❤
If you liked this post, share it with all of your programming buddies!
Python For Data Analysis - Build a Data Analysis Library from Scratch - Learn Python in 2019
When we talk about data processing, Data Science vs Big Data vs Data Analytics are the terms that one might think of and there has always been a confusion between them. In this article on Data science vs Big Data vs Data Analytics, I will understand the similarities and differences between them