Bird
Raised Fist0
Microservicessystem_design~25 mins

Database per service pattern in Microservices - System Design Exercise

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
Design: Database per Service Pattern Implementation
Design focuses on the database per service pattern within a microservices architecture. It excludes detailed implementation of individual microservices business logic and UI design.
Functional Requirements
FR1: Each microservice must have its own dedicated database.
FR2: Services should be loosely coupled and independently deployable.
FR3: Data consistency should be maintained across services where needed.
FR4: Support for service-specific data models and storage technologies.
FR5: Allow services to evolve their database schema independently.
Non-Functional Requirements
NFR1: System should handle 10,000 concurrent users.
NFR2: API response latency p99 under 300ms.
NFR3: Availability target of 99.9% uptime.
NFR4: Data synchronization between services should tolerate eventual consistency.
NFR5: Databases must be isolated to prevent cross-service data leaks.
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
API Gateway
Individual Microservices
Dedicated Databases per Service
Service Communication Layer (REST/gRPC/Message Queue)
Event Bus or Message Broker for asynchronous updates
Service Registry and Discovery
Design Patterns
Database per Service Pattern
Event-Driven Architecture
Saga Pattern for distributed transactions
CQRS (Command Query Responsibility Segregation)
API Gateway Pattern
Reference Architecture
          +----------------+          +----------------+
          |  Microservice  |          |  Microservice  |
          |      A         |          |      B         |
          +-------+--------+          +-------+--------+
                  |                           |
          +-------v--------+          +-------v--------+
          |  Database A    |          |  Database B    |
          +----------------+          +----------------+
                  |                           |
                  +------------+--------------+
                               |
                       +-------v-------+
                       |  Message Bus  |
                       +---------------+
                               |
                       +-------v-------+
                       | API Gateway   |
                       +---------------+
Components
Microservice A
Any suitable language/framework
Handles specific business logic and owns Database A
Microservice B
Any suitable language/framework
Handles different business logic and owns Database B
Database A
Relational or NoSQL database
Stores data exclusively for Microservice A
Database B
Relational or NoSQL database
Stores data exclusively for Microservice B
Message Bus
Kafka, RabbitMQ, or similar
Enables asynchronous communication and event propagation between services
API Gateway
Nginx, Kong, or custom
Single entry point for clients, routes requests to appropriate microservices
Request Flow
1. Client sends request to API Gateway.
2. API Gateway routes request to the appropriate microservice.
3. Microservice processes request using its own database.
4. If data changes affect other services, microservice publishes event to Message Bus.
5. Other microservices subscribe to relevant events and update their own data accordingly.
6. Microservices respond back to API Gateway, which returns response to client.
Database Schema
Each microservice has its own schema tailored to its domain. For example, Microservice A's database contains tables/entities related only to its business domain, isolated from Microservice B's schema. There are no shared tables or direct cross-service foreign keys.
Scaling Discussion
Bottlenecks
Cross-service data consistency delays due to asynchronous event propagation.
Increased complexity managing distributed transactions.
Message Bus becoming a single point of failure or bottleneck.
API Gateway overload under high traffic.
Database scaling limits for individual services.
Solutions
Implement Saga pattern to manage distributed transactions with compensating actions.
Use highly available and partitioned message brokers to handle load and failures.
Scale API Gateway horizontally with load balancers.
Use database sharding or read replicas for scaling individual service databases.
Design services to tolerate eventual consistency where possible to reduce tight coupling.
Interview Tips
Time: Spend 10 minutes understanding requirements and clarifying assumptions, 20 minutes designing the architecture and data flow, 10 minutes discussing scaling and trade-offs, and 5 minutes summarizing.
Explain why each service needs its own database for loose coupling and independent deployment.
Discuss trade-offs between strong and eventual consistency.
Describe how asynchronous messaging helps maintain data synchronization.
Highlight patterns like Saga for distributed transactions.
Address scaling challenges and solutions clearly.

Practice

(1/5)
1. What is the main advantage of the Database per service pattern in microservices architecture?
easy
A. It reduces the number of databases needed in the system.
B. All services share the same database for easier data management.
C. Each service can be developed, deployed, and scaled independently.
D. It allows direct database access between services.

Solution

  1. Step 1: Understand the pattern's goal

    The Database per service pattern means each microservice owns its own database to avoid tight coupling.
  2. Step 2: Analyze the benefits

    This independence allows each service to be developed, deployed, and scaled without affecting others.
  3. Final Answer:

    Each service can be developed, deployed, and scaled independently. -> Option C
  4. Quick Check:

    Service independence [OK]
Hint: Database per service means independent databases per microservice [OK]
Common Mistakes:
  • Thinking all services share one database
  • Assuming database sharing improves independence
  • Believing it reduces total databases
2. Which of the following is the correct way for microservices to access data in the Database per service pattern?
easy
A. Directly query another service's database.
B. Use database triggers to sync data between services.
C. Share a common database connection pool.
D. Use APIs to communicate and request data from other services.

Solution

  1. Step 1: Recall communication rules in this pattern

    Services do not share databases; they communicate via APIs to maintain independence.
  2. Step 2: Identify correct data access method

    Using APIs ensures loose coupling and clear service boundaries.
  3. Final Answer:

    Use APIs to communicate and request data from other services. -> Option D
  4. Quick Check:

    API communication [OK]
Hint: Microservices talk via APIs, not direct DB access [OK]
Common Mistakes:
  • Trying to query other service databases directly
  • Assuming shared connection pools exist
  • Using database triggers for cross-service sync
3. Consider two microservices: OrderService and InventoryService, each with its own database. If OrderService needs to check stock before placing an order, what is the correct flow?
medium
A. OrderService sends an API request to InventoryService to get stock information.
B. InventoryService pushes stock updates to OrderService's database.
C. OrderService writes stock info to its own database and reads from there.
D. OrderService queries InventoryService's database directly to check stock.

Solution

  1. Step 1: Identify data ownership

    InventoryService owns stock data in its own database; OrderService cannot access it directly.
  2. Step 2: Determine communication method

    OrderService must call InventoryService's API to get current stock info.
  3. Final Answer:

    OrderService sends an API request to InventoryService to get stock information. -> Option A
  4. Quick Check:

    API call for data [OK]
Hint: Always use API calls to get data from other services [OK]
Common Mistakes:
  • Direct DB queries between services
  • Duplicating data in multiple databases
  • Relying on push updates to other service DBs
4. A developer tries to implement the Database per service pattern but notices data inconsistency between services. What is the most likely cause?
medium
A. Services are sharing the same database schema.
B. Services are directly querying each other's databases.
C. Services communicate asynchronously via APIs.
D. Each service has its own database and communicates via APIs.

Solution

  1. Step 1: Identify incorrect practice

    Directly querying another service's database breaks independence and can cause stale or inconsistent data.
  2. Step 2: Understand correct communication

    Services should communicate via APIs to keep data consistent and boundaries clear.
  3. Final Answer:

    Services are directly querying each other's databases. -> Option B
  4. Quick Check:

    Direct DB queries cause inconsistency [OK]
Hint: Avoid direct DB queries between services to prevent inconsistency [OK]
Common Mistakes:
  • Assuming shared schema is the problem
  • Thinking async API calls cause inconsistency
  • Believing separate DBs cause inconsistency
5. You are designing a microservices system with the Database per service pattern. How can you ensure data consistency across services when a transaction involves multiple services?
hard
A. Implement eventual consistency using event-driven communication and compensating actions.
B. Use distributed transactions with two-phase commit across all databases.
C. Allow services to share a single database to simplify transactions.
D. Synchronize databases by copying data between services periodically.

Solution

  1. Step 1: Understand distributed transaction challenges

    Two-phase commit is complex and reduces service independence, so it's rarely used in microservices.
  2. Step 2: Identify best practice for consistency

    Event-driven communication with eventual consistency and compensating actions allows services to stay independent and handle failures gracefully.
  3. Final Answer:

    Implement eventual consistency using event-driven communication and compensating actions. -> Option A
  4. Quick Check:

    Event-driven eventual consistency [OK]
Hint: Use events and compensations for cross-service consistency [OK]
Common Mistakes:
  • Trying distributed two-phase commit in microservices
  • Sharing a single database defeats independence
  • Periodic data copying causes stale data