Does each Microservices really need its own DB?

Picture of Microservices Architecture

Problem Statement

In a complex distributed cloud application (example: amazon.com ecommerce), there are many Microservices running. Each of these Microservices require storing data to a database. In a distributed application, data is stored across multiple machines (Partition tolerant from CAP theorem). In order to keep up with the high amount of user transactions, there has to be a persistent storage system which can handle fast concurrent read/ writes and still has the consistent database.

Possible Solutions

There are two approaches which can gracefully handle this problem -

Each of these approaches has its own pros and cons which are discussed below.

Solution Evaluations

Approach 1: Common database with Two Phase Commit Protocol

The 2 phase commit protocol is also called XA (eXtended Architecture) transactions. This protocol provides ACID-like properties for global transaction processing. It consists of two phases - the first one is commit-request phase in which transaction manager coordinates all of the transaction resources to commit or abort. In the commit-phase, transaction manager decides to finalize operation by committing or aborting according to the votes of the each transaction resource.

## Working of 2 phase commit protocol ##

XA transactions need a global transaction id and local transaction id(xid) for each XA resource. Each XA Resource is enlisted to XA Manager by start(xid) method. This method tells that XA Resource is being involved in the transaction(be ready for operations).

2 Phase Commit Concept

Source Image xa-transactions-2-phase-commit

Pros
Cons

Approach 2: Separate databases with Event Driven data replication

This is an event-driven, eventually consistent approach. Each Microservice has its own database. Each Microservice publishes an event whenever it update its data. Other Microservices subscribe to events. When an event is received, a Microservice updates its data.

Below are the sequence of diagrams that shows how one can use an event-driven approach to checking for available credit when creating an order. The Microservices exchange events via a Message Broker (Example: RabbitMQ or Kafka).

Picture of Microservices Architecture

Source Image Event driven data management microservices

Picture of Microservices Architecture

Source Image Event driven data management microservices

Picture of Microservices Architecture

Source Image Event driven data management microservices

Pros
Cons

In a microservices architecture, each microservice has its own private datastore. Different microservices might use different SQL and NoSQL databases. While this database architecture has significant benefits, it creates some distributed data management challenges –

Conclusion

I studied existing implementations and discussions for both approaches. And it is realized if an application is designed to be built as microservices then it is implied it is meant to be scalable and highly responsive. And there exists many open source tools and databases which can help reduce the cost of development where each microservice uses its own database. The later approach is a good solution which offers more agility and velocity to a distributed application. Because of the blocking nature of the two phase commit protocol, the former approach is not well suited for high traffic concurrent transactions.

References used for study

[Two Phase Commit Protocol & One database for all Microservices]

[Event Driven Microservices Architecture & One database per Microservice]