Bird
Raised Fist0
Microservicessystem_design~7 mins

Database decomposition strategy in Microservices - System Design Guide

Choose your learning style10 modes available

Start learning this pattern below

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
Problem Statement
When a monolithic database grows too large, it becomes a bottleneck for development and scaling. Teams face challenges like slow deployments, complex schema changes, and performance degradation because all services share the same database.
Solution
Database decomposition splits a large monolithic database into smaller, service-specific databases. Each microservice owns its own database schema or instance, enabling independent development, scaling, and deployment. Services communicate via APIs or events instead of direct database sharing.
Architecture
Microservice A
Database A
Microservice B
Database B
Microservice C
Database C

This diagram shows multiple microservices each owning their own database. They communicate with each other through APIs or event messages instead of sharing a single database.

Trade-offs
✓ Pros
Enables independent service deployment and scaling without affecting others.
Reduces risk of schema conflicts and complex migrations across teams.
Improves fault isolation; one database failure doesn't bring down all services.
Allows technology heterogeneity; each service can choose the best database type.
✗ Cons
Increases complexity of data consistency across services due to distributed data.
Requires additional infrastructure for inter-service communication and data synchronization.
Makes complex queries involving multiple services harder and less efficient.
When the system has multiple teams working on different domains and the monolithic database causes deployment or scaling bottlenecks, typically at tens of thousands of requests per second or more.
When the system is small with low traffic (under 1,000 requests per second) or when strong transactional consistency across all data is critical and cannot be compromised.
Real World Examples
Amazon
Decomposed their monolithic database into service-specific databases to enable independent scaling and faster deployments across their vast e-commerce platform.
Netflix
Uses database decomposition to allow each microservice to own its data, improving fault isolation and enabling polyglot persistence.
Uber
Split their monolithic data store into multiple databases per service to handle high write volumes and reduce cross-team dependencies.
Code Example
The before code shows two services directly accessing the same shared database, causing tight coupling. The after code shows each service using its own database connection, enforcing database decomposition. Services communicate via events or APIs instead of shared tables.
Microservices
### Before: Monolithic database access (violating decomposition)
class OrderService:
    def create_order(self, user_id, product_id):
        # Directly access shared orders table
        db.execute("INSERT INTO orders (user_id, product_id) VALUES (?, ?)", (user_id, product_id))

class PaymentService:
    def process_payment(self, order_id, amount):
        # Directly access shared payments table
        db.execute("INSERT INTO payments (order_id, amount) VALUES (?, ?)", (order_id, amount))


### After: Each service owns its own database
class OrderService:
    def __init__(self, order_db):
        self.order_db = order_db

    def create_order(self, user_id, product_id):
        self.order_db.execute("INSERT INTO orders (user_id, product_id) VALUES (?, ?)", (user_id, product_id))

class PaymentService:
    def __init__(self, payment_db):
        self.payment_db = payment_db

    def process_payment(self, order_id, amount):
        self.payment_db.execute("INSERT INTO payments (order_id, amount) VALUES (?, ?)", (order_id, amount))

# Communication via API or events instead of shared DB
# For example, OrderService emits event 'OrderCreated' that PaymentService listens to.
OutputSuccess
Alternatives
Shared Database with Schema Separation
All services share the same database instance but use separate schemas or tablespaces.
Use when: When teams want some isolation but cannot afford multiple database instances due to cost or operational complexity.
Database Sharding
Data is partitioned horizontally across multiple database instances based on a shard key, but still logically one database.
Use when: When scaling a single large dataset horizontally is the main concern rather than service ownership.
Event Sourcing with CQRS
Services store events instead of current state and build read models separately, decoupling writes and reads.
Use when: When complex data consistency and auditability are required along with scalability.
Summary
Database decomposition splits a large monolithic database into smaller, service-owned databases to reduce coupling and improve scalability.
It enables independent service deployment, fault isolation, and technology flexibility but adds complexity in data consistency and communication.
This pattern is best for large systems with multiple teams and high traffic, while small systems may not benefit from its complexity.

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

  1. Step 1: Understand vertical decomposition

    Vertical decomposition means splitting a database by grouping related tables or columns, often by business capability or domain.
  2. Step 2: Compare with other options

    Horizontal decomposition splits by rows, geographic is location-based, and combining is the opposite of decomposition.
  3. Final Answer:

    Splitting a database by grouping related tables or columns into separate databases -> Option D
  4. 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

  1. Step 1: Define horizontal decomposition

    Horizontal decomposition splits data by rows, for example, dividing customers by region or user ID ranges.
  2. 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.
  3. Final Answer:

    Dividing data by rows, such as by customer or region -> Option A
  4. 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

  1. Step 1: Understand horizontal decomposition by region

    Horizontal decomposition splits data by rows, so each shard holds users from a specific region.
  2. Step 2: Identify which shard to query

    Querying European users targets only the shard holding European data, not others.
  3. Final Answer:

    Only the database shard containing European users -> Option A
  4. 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

  1. Step 1: Identify problem with vertical decomposition

    Vertical decomposition splits by tables/domains, but if services need to join data often, it causes latency.
  2. Step 2: Recommend fix

    Redesign to reduce cross-service joins by better domain boundaries or data duplication to avoid latency.
  3. Final Answer:

    Poor vertical decomposition causing cross-service joins; Fix: Redesign to reduce cross-service dependencies -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Use API composition to aggregate data from profile and order services after querying country-specific shards -> Option B
  4. Quick Check:

    API composition handles multi-db queries efficiently [OK]
Hint: Use API composition to combine data from vertical and horizontal splits [OK]
Common Mistakes:
  • Trying cross-database joins causing latency
  • Merging databases losing scalability
  • Ignoring decomposition benefits for simplicity