0% found this document useful (0 votes)
31 views7 pages

6 Data Management Patterns For Microservices - PROGRESSIVE CODER

This document discusses 6 patterns for managing data in microservices architectures: 1) Database per service, 2) Shared database, 3) Saga pattern, 4) API composition, 5) CQRS, and 6) Event sourcing. It provides details on each pattern, including advantages and disadvantages of each approach.

Uploaded by

abenito1313
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views7 pages

6 Data Management Patterns For Microservices - PROGRESSIVE CODER

This document discusses 6 patterns for managing data in microservices architectures: 1) Database per service, 2) Shared database, 3) Saga pattern, 4) API composition, 5) CQRS, and 6) Event sourcing. It provides details on each pattern, including advantages and disadvantages of each approach.

Uploaded by

abenito1313
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Data Management in a monolithic system can get pretty complex.

However, it could be a

6 Data Management Patterns for Microservices


completely different ball-game in a microservices architecture. In this post, we will look at 6
data management patterns for microservices.
Published by Saurabh Dashora on January 10, 2019
Every software application relies on data. The success or failure of any business relies on ef cient
data management. The holy grail of a successful business is making the data available to the
right stakeholder at the right time. Failure to do so can be catastrophic.

So what are these 6 patterns of data management that can help us manage our data
effectively?

Let’s look at them one by one:

Database Per Service


In this pattern, each microservice manages its own data. What this implies is that no other
microservice can access that data directly. Communication or exchange of data can only
happen using a set of well-de ned APIs.

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.

Categories: BLOG MICROSERVICES

Tags: Blog Microservices

4 Comments
Abhishek · June 11, 2019 at 11:39 am
Great Job done. Kindly provide the saga pattern in two different ms.

REPLY

Saurabh Dashora · June 11, 2019 at 11:55 am


Hi Abhishek, Thanks for the gr8 feedback.

Saga pattern sample is also available at the link.

REPLY

Lokesh Barhate · August 8, 2020 at 10:50 am


hey Saurabh, great info. https://progressive-coder.local/saga-pattern-implementation-
with-axon-and-spring-boot-part-1/ This link is not valid anymore!

REPLY

Saurabh Dashora · August 9, 2020 at 1:40 am


Thanks Lokesh. Fixed the link now

REPLY

Leave a Reply
Name
Email

Website

What's on your mind?

POST COMMENT

Search …

Recent Posts

Understanding the Basics of Graph Data Structure

Log4J2 File Appender Setup with Spring Boot

Spring Boot Log4J2 Con guration Examples and Options

Spring Boot Log4J2 Setup

Coin Change Problem Dynamic Programming Approach

Advertisement

Advertisement

Advertisement

Subscription Form

Subscribe to our mailing list


indicates required
indicates required

Email Address *

First Name *

Last Name

SUBSCRIBE

Categories

Select Category

Advertisement

Related Posts

BLOG

Understanding the Basics of Graph Data Structure


In this post, we will look at understanding the basics of Graph data structure. Graph is arguably one of
the most practically used data structures. For example, below are some of the way graph data
Read more…

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

Spring Boot Log4J2 Con guration Examples and Options


In this post, we will look at Spring Boot Log4J2 con guration examples. This will build up from the
previous post about the basic Spring Boot Log4J2 setup and therefore, it would be good to read
Read more…

Recent Posts

UNDERSTANDING THE BASICS OF GRAPH DATA STRUCTURE January 15, 2021

LOG4J2 FILE APPENDER SETUP WITH SPRING BOOT January 11, 2021

SPRING BOOT LOG4J2 CONFIGURATION EXAMPLES AND OPTIONS January 7, 2021

Created my free logo at LogoMakr.com

You might also like