| Users | What Changes |
|---|---|
| 100 users | Single database handles all microservices; low contention; simple coordination. |
| 10,000 users | Database load increases; contention on shared tables; slower transactions; microservices tightly coupled. |
| 1,000,000 users | Database becomes bottleneck; high latency; schema changes affect multiple services; difficult to deploy independently. |
| 100,000,000 users | Database overloads; scaling vertically is costly; outages impact all services; no isolation; very hard to maintain and evolve. |
Shared database anti-pattern in Microservices - Scalability & System Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
The shared database is the first bottleneck. As all microservices read and write to the same database, contention and locking increase. This causes slow queries and blocks other services. Schema changes become risky because they affect all services, reducing agility.
- Database per service: Each microservice owns its own database to reduce contention and coupling.
- API communication: Services communicate via APIs instead of sharing data directly.
- Event-driven architecture: Use events to sync data asynchronously between services.
- Read replicas and caching: For read-heavy workloads, use replicas and caches to reduce load.
- Data partitioning: Split data by service domain to isolate workloads.
Assuming 1 million users generate 10,000 requests per second total:
- Database QPS needed: ~10,000 QPS (likely exceeds single DB capacity).
- Storage: Shared DB grows fast, schema changes affect all services, increasing maintenance cost.
- Bandwidth: Internal network traffic increases due to contention and locking delays.
- Scaling vertically (bigger DB server) becomes expensive and hits limits quickly.
Start by explaining the shared database anti-pattern and why it causes tight coupling and scaling issues. Then discuss how the database becomes a bottleneck as traffic grows. Finally, propose solutions like database per service and asynchronous communication to improve scalability and independence.
Your database handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: The first step is to decouple microservices by giving each its own database to reduce contention. Then introduce caching and read replicas to handle read load. This prevents the shared database from becoming a bottleneck.
Practice
shared database anti-pattern in microservices?Solution
Step 1: Understand the shared database anti-pattern
This anti-pattern happens when multiple microservices use the same database schema directly.Step 2: Identify the impact on service independence
Sharing the database causes tight coupling, making services dependent on each other's data structure changes.Final Answer:
Tight coupling between services due to shared data schema -> Option BQuick Check:
Shared database = Tight coupling [OK]
- Thinking shared DB improves scaling
- Assuming shared DB isolates faults
- Believing shared DB simplifies service design
Solution
Step 1: Recall best practice for microservice data management
Each microservice should have its own database to maintain independence.Step 2: Identify the correct communication method
Services communicate through APIs, not by sharing databases or direct queries.Final Answer:
Each service owns its own database and communicates via APIs -> Option DQuick Check:
Separate DB + APIs = Avoid shared DB anti-pattern [OK]
- Choosing shared schema for simplicity
- Allowing direct cross-service DB queries
- Centralizing all data in one DB
Solution
Step 1: Analyze the impact of schema change on shared DB
When services share a database, schema changes affect all services using it.Step 2: Predict Service B's behavior
Service B expects the old schema; a change causes runtime errors like failed queries or crashes.Final Answer:
Service B experiences runtime errors due to schema mismatch -> Option CQuick Check:
Schema change + shared DB = runtime errors [OK]
- Assuming automatic schema adaptation
- Believing no impact on other services
- Thinking performance improves
Solution
Step 1: Identify the root cause of tight coupling
Sharing the same database schema causes deployment and coupling problems.Step 2: Apply the correct fix
Separating databases and using APIs decouples services and allows independent deployment.Final Answer:
Create separate databases and add API communication -> Option AQuick Check:
Separate DB + APIs fix shared DB anti-pattern [OK]
- Merging services loses microservice benefits
- Adding indexes doesn't fix coupling
- Allowing shared writes keeps tight coupling
Solution
Step 1: Understand the trade-offs in migration
Separating databases improves independence but can cause data consistency challenges.Step 2: Choose a pattern that balances consistency and independence
Event-driven messaging allows services to sync data asynchronously while keeping separate databases.Final Answer:
Split databases per service and use event-driven messaging for data sync -> Option AQuick Check:
Separate DB + events balance consistency and independence [OK]
- Keeping shared DB limits independence
- Merging services loses microservice benefits
- Using direct SQL breaks service boundaries
