Bird
Raised Fist0
Microservicessystem_design~25 mins

Shared database anti-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: Microservices Architecture with Shared Database
Focus on the implications of using a shared database among microservices. Out of scope are detailed service implementations or UI design.
Functional Requirements
FR1: Multiple microservices must access and update data related to their domain.
FR2: Each microservice should be independently deployable and scalable.
FR3: Data consistency must be maintained across services.
FR4: Services should be loosely coupled to allow independent development.
Non-Functional Requirements
NFR1: Database schema is shared among all microservices.
NFR2: Latency for API responses should be under 300ms p99.
NFR3: System should handle 5,000 concurrent users.
NFR4: Availability target is 99.9% uptime.
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
Key Components
Microservices
Shared relational database
API Gateway or service communication
Data access layer
Caching layer
Design Patterns
Database per service pattern
Event-driven architecture
API composition
CQRS (Command Query Responsibility Segregation)
Reference Architecture
          +----------------+          
          |  API Gateway   |          
          +-------+--------+          
                  |                   
  +---------------+---------------+  
  |               |               |  
+----+         +----+          +----+
| S1 |         | S2 |          | S3 |
+----+         +----+          +----+
   |              |               |   
   +--------------+---------------+   
                  |                   
          +-------+--------+          
          | Shared Database |          
          +----------------+          
Components
Microservices (S1, S2, S3)
Any language/framework (e.g., Spring Boot, Node.js)
Implement business logic for different domains but share the same database schema.
Shared Database
Relational DB (e.g., PostgreSQL, MySQL)
Central data store accessed by all microservices.
API Gateway
Nginx, Kong, or custom gateway
Route client requests to appropriate microservices.
Data Access Layer
ORM or direct SQL
Microservices use this to read/write shared database.
Cache Layer (optional)
Redis or Memcached
Improve read performance and reduce DB load.
Request Flow
1. Client sends request to API Gateway.
2. API Gateway routes request to the appropriate microservice.
3. Microservice executes business logic and accesses shared database directly.
4. Microservice reads or writes data in shared tables.
5. Microservice returns response to API Gateway.
6. API Gateway sends response back to client.
Database Schema
Entities: User, Order, Product, Payment Relationships: - User 1:N Order - Order N:1 Product - Order 1:1 Payment All tables are shared and accessed by multiple microservices, leading to tight coupling.
Scaling Discussion
Bottlenecks
Tight coupling due to shared schema limits independent deployment.
Schema changes require coordination across teams.
Database becomes a single point of failure and performance bottleneck.
Hard to enforce service boundaries and data ownership.
Risk of data corruption due to concurrent writes from multiple services.
Solutions
Adopt database per service pattern to isolate data ownership.
Use asynchronous communication (events) to sync data between services.
Implement API composition or CQRS to separate read/write models.
Introduce service-level caching to reduce database load.
Use database replication and sharding to improve availability and scalability.
Interview Tips
Time: Spend 10 minutes understanding the shared database pattern and its drawbacks, 15 minutes discussing alternatives and improvements, 10 minutes on scaling and trade-offs.
Explain how shared database creates tight coupling and deployment challenges.
Discuss impact on team autonomy and service independence.
Highlight risks of schema changes and data consistency issues.
Present alternatives like database per service and event-driven design.
Show understanding of scaling bottlenecks and mitigation strategies.

Practice

(1/5)
1. What is the main problem caused by the shared database anti-pattern in microservices?
easy
A. Better fault isolation between services
B. Tight coupling between services due to shared data schema
C. Easier scaling of individual services
D. Improved performance by sharing data directly

Solution

  1. Step 1: Understand the shared database anti-pattern

    This anti-pattern happens when multiple microservices use the same database schema directly.
  2. Step 2: Identify the impact on service independence

    Sharing the database causes tight coupling, making services dependent on each other's data structure changes.
  3. Final Answer:

    Tight coupling between services due to shared data schema -> Option B
  4. Quick Check:

    Shared database = Tight coupling [OK]
Hint: Shared DB means tight coupling, breaking microservices independence [OK]
Common Mistakes:
  • Thinking shared DB improves scaling
  • Assuming shared DB isolates faults
  • Believing shared DB simplifies service design
2. Which of the following is the correct way to avoid the shared database anti-pattern in microservices?
easy
A. Use a single database schema shared by all services
B. Store all data in a centralized monolithic database
C. Allow direct SQL queries from one service to another's database
D. Each service owns its own database and communicates via APIs

Solution

  1. Step 1: Recall best practice for microservice data management

    Each microservice should have its own database to maintain independence.
  2. Step 2: Identify the correct communication method

    Services communicate through APIs, not by sharing databases or direct queries.
  3. Final Answer:

    Each service owns its own database and communicates via APIs -> Option D
  4. Quick Check:

    Separate DB + APIs = Avoid shared DB anti-pattern [OK]
Hint: Separate DB per service + API calls avoid shared DB anti-pattern [OK]
Common Mistakes:
  • Choosing shared schema for simplicity
  • Allowing direct cross-service DB queries
  • Centralizing all data in one DB
3. Consider two microservices, Service A and Service B, sharing the same database. Service A changes a table schema without informing Service B. What is the most likely outcome?
medium
A. Service B automatically adapts to the new schema
B. Service B continues working without issues
C. Service B experiences runtime errors due to schema mismatch
D. Both services improve performance

Solution

  1. Step 1: Analyze the impact of schema change on shared DB

    When services share a database, schema changes affect all services using it.
  2. Step 2: Predict Service B's behavior

    Service B expects the old schema; a change causes runtime errors like failed queries or crashes.
  3. Final Answer:

    Service B experiences runtime errors due to schema mismatch -> Option C
  4. Quick Check:

    Schema change + shared DB = runtime errors [OK]
Hint: Schema change in shared DB breaks other services [OK]
Common Mistakes:
  • Assuming automatic schema adaptation
  • Believing no impact on other services
  • Thinking performance improves
4. You find that two microservices share a database causing tight coupling and deployment issues. Which change fixes this problem?
medium
A. Create separate databases and add API communication
B. Merge the two services into one monolith
C. Add more indexes to the shared database
D. Allow both services to write to the same tables

Solution

  1. Step 1: Identify the root cause of tight coupling

    Sharing the same database schema causes deployment and coupling problems.
  2. Step 2: Apply the correct fix

    Separating databases and using APIs decouples services and allows independent deployment.
  3. Final Answer:

    Create separate databases and add API communication -> Option A
  4. Quick Check:

    Separate DB + APIs fix shared DB anti-pattern [OK]
Hint: Separate DB + APIs fix tight coupling from shared DB [OK]
Common Mistakes:
  • Merging services loses microservice benefits
  • Adding indexes doesn't fix coupling
  • Allowing shared writes keeps tight coupling
5. A company has three microservices sharing one database. They want to migrate to avoid the shared database anti-pattern. Which approach best balances data consistency and service independence?
hard
A. Split databases per service and use event-driven messaging for data sync
B. Keep shared database but add strict schema versioning
C. Merge all services into a single database schema with shared tables
D. Use direct SQL queries between services to keep data consistent

Solution

  1. Step 1: Understand the trade-offs in migration

    Separating databases improves independence but can cause data consistency challenges.
  2. Step 2: Choose a pattern that balances consistency and independence

    Event-driven messaging allows services to sync data asynchronously while keeping separate databases.
  3. Final Answer:

    Split databases per service and use event-driven messaging for data sync -> Option A
  4. Quick Check:

    Separate DB + events balance consistency and independence [OK]
Hint: Use separate DB + events for sync to avoid shared DB anti-pattern [OK]
Common Mistakes:
  • Keeping shared DB limits independence
  • Merging services loses microservice benefits
  • Using direct SQL breaks service boundaries