Problem
What’s the database architecture in a microservices application?
Forces
-
Services must be loosely coupled so that they can be developed, deployed and scaled independently
-
Some business transactions must enforce invariants that span multiple services. For example, the
Place Order
use case must verify that a new Order will not exceed the customer’s credit limit. Other business transactions, must update data owned by multiple services. -
Some business transactions need to query data that is owned by multiple services. For example, the
View Available Credit
use must query the Customer to find thecreditLimit
and Orders to calculate the total amount of the open orders. -
Some queries must join data that is owned by multiple services. For example, finding customers in a particular region and their recent orders requires a join between customers and orders.
-
Databases must sometimes be replicated and sharded in order to scale. See the Scale Cube.
-
Different services have different data storage requirements. For some services, a relational database is the best choice. Other services might need a NoSQL database such as MongoDB, which is good at storing complex, unstructured data, or Neo4J, which is designed to efficiently store and query graph data.
We have talked about one database per service being ideal for microservices, but that is possible when the application is greenfield and to be developed with DDD.
But if the application is a monolith and trying to break into microservices, denormalization is not that easy. What is the suitable architecture in that case?
Solution
A shared database per service is not ideal, but that is the working solution for the above scenario. Most people consider this an anti-pattern for microservices, but for brownfield applications, this is a good start to break the application into smaller logical pieces.
This should not be applied to greenfield applications. In this pattern, one database can be aligned with more than one microservice, but it has to be restricted to 2-3 maximum, otherwise scaling, autonomy, and independence will be challenging to execute.
Resulting context
The benefits of this pattern are:
- A developer uses familiar and straightforward ACID transactions to enforce data consistency
- A single database is simpler to operate
The drawbacks of this pattern are:
-
Development time coupling - a developer working on, for example, the
OrderService
will need to coordinate schema changes with the developers of other services that access the same tables. This coupling and additional coordination will slow down development. -
Runtime coupling - because all services access the same database they can potentially interfere with one another. For example, if long running
CustomerService
transaction holds a lock on theORDER
table then theOrderService
will be blocked. -
Single database might not satisfy the data storage and access requirements of all services.