DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • 4 Key Observability Metrics for Distributed Applications
  • How To Get Closer to Consistency in Microservice Architecture
  • Implement a Distributed Database to Your Java Application
  • Modeling Saga as a State Machine

Trending

  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  • Doris: Unifying SQL Dialects for a Seamless Data Query Ecosystem
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • Segmentation Violation and How Rust Helps Overcome It
  1. DZone
  2. Data Engineering
  3. Data
  4. 6 Data Management Patterns for Microservices

6 Data Management Patterns for Microservices

Data management in microservices can get pretty complex. So, in this post, we break down 6 popular ways of handling data in microservice apps.

By 
Saurabh Dashora user avatar
Saurabh Dashora
DZone Core CORE ·
Feb. 04, 19 · Analysis
Likes (18)
Comment
Save
Tweet
Share
73.4K Views

Join the DZone community and get the full member experience.

Join For Free

Data Management in a monolithic system can get pretty complex. However, it could be a completely different ball-game in a microservices architecture. In this post, we will look at 6 data management patterns for microservices.

Every software application relies on data. The success or failure of any business relies on efficient 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-defined 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 defining 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 specific database solution for a given microservice.

Shared Database

A 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 benefits of microservices. Developers across teams need to coordinate for schema changes to tables. There could also be run-time conflicts 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

The 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, the below steps can occur:

  • Food Order service creates an order. At this point, the order is in a 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 confirmation, 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 inefficient 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 an 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.

microservice Data (computing) Data management Database Event application

Opinions expressed by DZone contributors are their own.

Related

  • 4 Key Observability Metrics for Distributed Applications
  • How To Get Closer to Consistency in Microservice Architecture
  • Implement a Distributed Database to Your Java Application
  • Modeling Saga as a State Machine

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!