6 Data Management Patterns For Microservices - PROGRESSIVE CODER
6 Data Management Patterns For Microservices - PROGRESSIVE CODER
However, it could be a
So what are these 6 patterns of data management that can help us manage our data
effectively?
It sounds easier than it actually is to implement this pattern. Applications usually are not so well
demarcated. Usually, microservices need data from each other for implementing their logic. This
leads to spaghetti-like interactions between various services in your application.
The success of this pattern hinges on effectively de ning the bounded contexts in your
application. For a new application or system, it is easier to do so. But for large and existing
monolithic systems, it is troublesome.
Other challenges include implementing business transactions that span several microservices.
Another challenge could be implementing queries that want to expose data from two or three
different bounded contexts.
However, if done properly, the major advantages of this pattern are loose coupling between
microservices. You can save your application from impact-analysis hell.
Also, you could scale up microservices individually. It can provide freedom to the developers to
choose a speci c database solution for a given microservice.
Shared Database
Shared database could be a viable option if the challenges surrounding Database Per Service
become too tough to handle for your team.
This approach tries to solve the same problems. But it does so by adopting a much more lenient
approach by using a shared database accessed by multiple microservices.
Mostly, this is a safer pattern for developers as they are able to work in existing ways. Familiar
ACID transactions are used to enforce consistency.
However, this approach takes away most of the bene ts of microservices. Developers across
teams need to co-ordinate for schema changes to tables. There could also be run-time con icts
when multiple services are trying to access the same database resources.
Overall, this approach can do more harm than good in the long run.
Saga Pattern
Saga pattern is the solution to implementing business transactions spanning multiple
microservices.
A Saga is basically a sequence of local transactions. For every transaction performed within a
Saga, the service performing the transaction publishes an event. The subsequent transaction is
triggered based on the output of the previous transaction. And if one of the transactions in this
chain fails, the Saga executes a series of compensating transactions to undo the impact of all the
previous transactions.
To understand this better, let’s take a simple example. Assume that there is a food-delivery app.
When a customer tries to order food, below steps can occur:
Food Order service creates an order. At this point, the order is in PENDING state. A Saga
manages the chain of events.
The Saga contacts the restaurant via the Restaurant service.
The Restaurant service attempts to place the order with the chosen restaurant. After
getting a con rmation, it sends back a reply.
The Saga receives the reply. And depending on the reply, it can Approve the order or Reject
the order.
The Food Order service then changes the state of the order. If the order was Approved, it
would inform the customer with the next details. If Rejected, it will also inform the
customer with an apology message.
As you can see, this is drastically different from the usual point-to-point call approach. This
approach adds complexity. However, in my view, Sagas are a very powerful tool to solve some
tricky challenges. But they should be used sparingly.
API Composition
This pattern is a direct solution to the problem of implementing complex queries in a
microservices architecture.
In this pattern, an API Composer invokes other microservices in the required order. And after
fetching the results it performs an in-memory join of the data before providing it to the
consumer.
As evident, the downside to this pattern is the use of inef cient in-memory joins on potentially
large datasets.
CQRS
CQRS or Command Query Responsibility Segregation is an attempt to get around the issues
with API Composition pattern.
An application listens to domain events from other microservices and updates the view or query
database. You can serve complex aggregation queries from this database. You could optimize
the performance and scale up the query microservices accordingly.
The downside to this is increase in complexity. All of a sudden, your microservice should be
handling events. This can cause latency issues where the view database is eventually consistent
rather than always consistent. It can also increase code duplication.
Event Sourcing
Event sourcing mainly tries to solve the problem of atomically updating the database and
publishing an event.
In event sourcing, you store the state of the entity or the aggregate as a sequence of state
changing events. A new event is created whenever there is an update or an insert. The event
store is used to store the events.
You can use this pattern in conjunction with CQRS. By doing so, a lot of challenges around event
handling and maintaining query data can be solved.
However, as a drawback, this pattern imposes an unfamiliar programming style. Also, the data is
eventually consistent which may not be suitable for some use cases.
In the coming posts, we would explore these patterns in greater details along with
implementation examples.
UPDATE 31/01/2019:
We have now implemented Event Sourcing using Axon and Spring Boot in a sample
application. You can check out the step-by-step process in the series of posts here.
UPDATE 3/3/2019:
We have now implemented Event Sourcing and CQRS using Axon and Spring Boot in a sample
application. Check out the step-by-step process in the series of posts here.
4 Comments
Abhishek · June 11, 2019 at 11:39 am
Great Job done. Kindly provide the saga pattern in two different ms.
REPLY
REPLY
REPLY
REPLY
Leave a Reply
Name
Email
Website
POST COMMENT
Search …
Recent Posts
Advertisement
Advertisement
Advertisement
Subscription Form
Email Address *
First Name *
Last Name
SUBSCRIBE
Categories
Select Category
Advertisement
Related Posts
BLOG
BLOG
Log4J2 File Appender Setup with Spring Boot
In this post, we will look at setting up Log4J2 File Appender with Spring Boot. Apart from console
logging, we also want our logs to be pushed to les. This helps in arranging logs by Read more…
BLOG
Recent Posts
LOG4J2 FILE APPENDER SETUP WITH SPRING BOOT January 11, 2021