Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Design: Database Decomposition Strategy for Microservices
Focus on strategies to split a monolithic database into multiple databases owned by microservices. Out of scope: detailed microservice business logic, UI design, or deployment infrastructure.
Functional Requirements
FR1: Support multiple microservices each owning its own data
FR2: Ensure data consistency within each microservice
FR3: Allow microservices to evolve independently
FR4: Enable efficient querying within each microservice
FR5: Support communication between microservices for cross-service data needs
Non-Functional Requirements
NFR1: Handle up to 10,000 concurrent requests across services
NFR2: API response latency p99 under 300ms
NFR3: Availability target of 99.9% uptime
NFR4: Data consistency within a microservice must be strong; eventual consistency allowed across services
NFR5: Data storage must be scalable and maintainable
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Service-specific databases
API gateways or service mesh for communication
Event-driven messaging or asynchronous communication
Data replication or caching for cross-service queries
Database per service pattern
Design Patterns
Database per service
Shared database anti-pattern
Event sourcing and CQRS for cross-service data
Saga pattern for distributed transactions
API composition for aggregating data
Reference Architecture
+---------------------+
| API Gateway |
+----------+----------+
|
+------------------+------------------+
| |
+-------v-------+ +-------v-------+
| User Service | | Order Service |
| +-----------+| | +-----------+|
| | User DB || | | Order DB ||
| +-----------+| | +-----------+|
+---------------+ +---------------+
| |
+------------------+------------------+
|
+----------v----------+
| Event Bus / Message |
| Queue |
+---------------------+
Components
API Gateway
Nginx or Kong
Route client requests to appropriate microservices
User Service
Node.js/Java/Spring Boot
Handles user-related business logic and owns User database
Order Service
Node.js/Java/Spring Boot
Handles order-related business logic and owns Order database
User DB
PostgreSQL or MongoDB
Stores user data, owned exclusively by User Service
Order DB
PostgreSQL or MongoDB
Stores order data, owned exclusively by Order Service
Event Bus / Message Queue
Kafka or RabbitMQ
Facilitates asynchronous communication and eventual consistency between services
Request Flow
1. Client sends request to API Gateway
2. API Gateway routes request to appropriate microservice
3. Microservice processes request using its own database
4. If data needed by another service, microservice publishes event to Event Bus
5. Other microservices subscribe to relevant events and update their own data or caches
6. Microservices respond back to API Gateway
7. API Gateway sends response to client
Database Schema
Entities are split by service ownership. For example, User Service owns User entity with attributes like user_id, name, email. Order Service owns Order entity with order_id, user_id (foreign key reference by ID only, not enforced by DB), product details, status. No shared tables. Relationships across services handled via events and IDs.
Scaling Discussion
Bottlenecks
Single database per service can become a bottleneck under heavy load
Cross-service data queries can be slow or inconsistent
Distributed transactions are complex and can reduce performance
Eventual consistency may cause stale data issues
Message queue can become a bottleneck if overloaded
Solutions
Scale databases vertically or horizontally (read replicas, sharding) per service
Use caching and CQRS pattern to optimize cross-service queries
Implement Saga pattern to manage distributed transactions reliably
Design services to tolerate eventual consistency and provide user feedback
Scale message queue cluster and partition topics to handle load
Interview Tips
Time: Spend 10 minutes understanding requirements and clarifying scope, 20 minutes designing the decomposition strategy and data flow, 10 minutes discussing scaling and trade-offs, 5 minutes summarizing.
Explain importance of bounded contexts and service ownership of data
Discuss pros and cons of database per service pattern
Highlight how asynchronous communication supports eventual consistency
Mention challenges of distributed transactions and solutions like Saga
Show awareness of scaling bottlenecks and mitigation strategies
Practice
(1/5)
1. Which of the following best describes vertical decomposition in database design for microservices?
easy
A. Dividing a database by rows to distribute data across multiple databases
B. Combining multiple databases into one large database
C. Separating databases based on geographic location
D. Splitting a database by grouping related tables or columns into separate databases
Solution
Step 1: Understand vertical decomposition
Vertical decomposition means splitting a database by grouping related tables or columns, often by business capability or domain.
Step 2: Compare with other options
Horizontal decomposition splits by rows, geographic is location-based, and combining is the opposite of decomposition.
Final Answer:
Splitting a database by grouping related tables or columns into separate databases -> Option D
Quick Check:
Vertical decomposition = splitting by columns/tables [OK]
Hint: Vertical = split by columns or tables, horizontal = split by rows [OK]
Common Mistakes:
Confusing vertical with horizontal decomposition
Thinking vertical means geographic split
Assuming decomposition means combining databases
2. Which of the following is the correct description of horizontal decomposition in microservices database design?
easy
A. Dividing data by rows, such as by customer or region
B. Splitting data by columns or tables based on functionality
C. Merging multiple databases into one for simplicity
D. Separating databases by different database engines
Solution
Step 1: Define horizontal decomposition
Horizontal decomposition splits data by rows, for example, dividing customers by region or user ID ranges.
Step 2: Eliminate incorrect options
Splitting data by columns or tables based on functionality describes vertical decomposition, C is merging (not decomposition), and D is about engines, not decomposition strategy.
Final Answer:
Dividing data by rows, such as by customer or region -> Option A
Quick Check:
Horizontal decomposition = split by rows [OK]
Hint: Horizontal = split by rows, vertical = split by columns [OK]
Common Mistakes:
Mixing horizontal with vertical decomposition
Thinking horizontal means merging databases
Confusing database engine separation with decomposition
3. Consider a microservices system where the user database is split by region using horizontal decomposition. If a query requests all users from Europe, which database(s) will be queried?
medium
A. Only the database shard containing European users
B. All database shards regardless of region
C. Only the database shard containing North American users
D. A combined database with all users merged
Solution
Step 1: Understand horizontal decomposition by region
Horizontal decomposition splits data by rows, so each shard holds users from a specific region.
Step 2: Identify which shard to query
Querying European users targets only the shard holding European data, not others.
Final Answer:
Only the database shard containing European users -> Option A
Quick Check:
Query targets relevant shard only [OK]
Hint: Query only the shard holding requested data region [OK]
Common Mistakes:
Querying all shards unnecessarily
Querying wrong region shard
Assuming data is merged in one database
4. A microservices team decomposed their database vertically but notices frequent cross-service joins causing latency. What is the likely cause and fix?
medium
A. Cause: Using NoSQL instead of SQL; Fix: Switch to SQL databases
B. Cause: Horizontal decomposition; Fix: Merge databases into one
C. Cause: Poor vertical decomposition causing cross-service joins; Fix: Redesign to reduce cross-service dependencies
D. Cause: Too many database shards; Fix: Increase shards further
Solution
Step 1: Identify problem with vertical decomposition
Vertical decomposition splits by tables/domains, but if services need to join data often, it causes latency.
Step 2: Recommend fix
Redesign to reduce cross-service joins by better domain boundaries or data duplication to avoid latency.
Final Answer:
Poor vertical decomposition causing cross-service joins; Fix: Redesign to reduce cross-service dependencies -> Option C
Quick Check:
Cross-service joins cause latency; fix by better decomposition [OK]
Hint: Cross-service joins mean bad vertical split; redesign domains [OK]
Common Mistakes:
Confusing horizontal with vertical decomposition issues
Thinking merging databases fixes latency
Blaming database type instead of design
5. A company wants to scale their microservices database by splitting user data by country (horizontal) and splitting user profile and orders into separate databases (vertical). What is the best approach to handle queries that need both profile and order data for users in a specific country?
hard
A. Perform cross-database joins directly on all shards for each country
B. Use API composition to aggregate data from profile and order services after querying country-specific shards
C. Merge profile and order data into a single database shard per country
D. Store all user data in one large database to avoid complexity
Solution
Step 1: Understand combined vertical and horizontal decomposition
Data is split horizontally by country and vertically by data type (profile, orders), so data is in different shards and databases.
Step 2: Choose best query approach
Cross-database joins are expensive and complex; merging data loses benefits. API composition aggregates data from services after querying relevant shards efficiently.
Final Answer:
Use API composition to aggregate data from profile and order services after querying country-specific shards -> Option B
Quick Check:
API composition handles multi-db queries efficiently [OK]
Hint: Use API composition to combine data from vertical and horizontal splits [OK]