
- Microservices Design Patterns Tutorial
- Microservices Design Patterns - Home
- Microservices Design Patterns - Overview
- Decomposition Design Patterns
- Decompose by Business Capability
- Decompose by Subdomain
- Decompose by Strangler
- Integration Design Patterns
- API Gateway
- Aggregator
- Proxy
- Client Side UI Composition
- Chain Of Responsibilities
- Branch
- Database Design Patterns
- Database per Service
- Shared Database per Service
- Command Query Responsibility Segregator
- Saga
- Aysynchronous Messaging
- Event Sourcing
- Observability Design Patterns
- Log Aggregation
- Performance Metrics
- Distributed Tracing
- Health Check
- Cross Cutting Concern Design Patterns
- External Configuration
- Service Discovery
- Circuit Breaker
- Blue Green Deployment
- Useful Resources
- Quick Guide
- Useful Resources
- Discussion
Shared Database per Service
Problem Statement
Microservice architecture structures an application as a set of loosely coupled microservices and each service can be developed independently in agile manner to enable continous delivery/deployment. What should be the database structure/architecture in microservices based application.
Solution
We can use a database which is shared among microservices. Each service is free to use data accessible to other services. Database will maintain the ACID transactions.

In this pattern, each service should use transaction management of underlying database so the ACID property of the database can be utilized. Conside the following pseudocode −
BEGIN TRANSACTION … SELECT * FROM ORDERS WHERE CUSTOMER_ID = ? … SELECT CREDIT_LIMIT FROM CUSTOMERS WHERE CUSTOMER_ID = ? … INSERT INTO ORDERS ... WHERE ORDER_LIMIT < CREDIT_LIMIT … COMMIT TRANSACTION
Here order service uses database transaction to ensure that during order, credit limit of the customer is checked.
Advertisements