What if one small change could stop your whole app from breaking? Discover how to avoid that with a simple pattern.
Why Database per service pattern in Microservices? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine a team building a big app where many parts share one giant database. Every time someone changes the database, it risks breaking others. It feels like everyone is trying to use the same notebook at once, causing confusion and mistakes.
Using one shared database slows down development because teams must coordinate every change. Bugs spread easily since one service's error can corrupt shared data. It's hard to scale or update parts independently, making the whole system fragile and slow.
The database per service pattern gives each service its own private database. This way, teams work independently without stepping on each other's toes. Services control their data fully, making the system more reliable, easier to scale, and faster to develop.
ServiceA and ServiceB both read/write to the same 'users' table in one big database.
ServiceA uses its own 'users' database; ServiceB uses a separate 'users' database.
This pattern unlocks true independence for each service, allowing faster updates, better fault isolation, and easier scaling.
Think of an online store where the payment service and product catalog service each have their own databases. If the payment database needs an update, it won't affect the product catalog, so the store keeps running smoothly.
Sharing one database creates tight coupling and risks.
Database per service isolates data, reducing errors and conflicts.
It enables independent development, deployment, and scaling.
Practice
Database per service pattern in microservices architecture?Solution
Step 1: Understand the pattern's goal
The Database per service pattern means each microservice owns its own database to avoid tight coupling.Step 2: Analyze the benefits
This independence allows each service to be developed, deployed, and scaled without affecting others.Final Answer:
Each service can be developed, deployed, and scaled independently. -> Option CQuick Check:
Service independence [OK]
- Thinking all services share one database
- Assuming database sharing improves independence
- Believing it reduces total databases
Solution
Step 1: Recall communication rules in this pattern
Services do not share databases; they communicate via APIs to maintain independence.Step 2: Identify correct data access method
Using APIs ensures loose coupling and clear service boundaries.Final Answer:
Use APIs to communicate and request data from other services. -> Option DQuick Check:
API communication [OK]
- Trying to query other service databases directly
- Assuming shared connection pools exist
- Using database triggers for cross-service sync
Solution
Step 1: Identify data ownership
InventoryService owns stock data in its own database; OrderService cannot access it directly.Step 2: Determine communication method
OrderService must call InventoryService's API to get current stock info.Final Answer:
OrderService sends an API request to InventoryService to get stock information. -> Option AQuick Check:
API call for data [OK]
- Direct DB queries between services
- Duplicating data in multiple databases
- Relying on push updates to other service DBs
Solution
Step 1: Identify incorrect practice
Directly querying another service's database breaks independence and can cause stale or inconsistent data.Step 2: Understand correct communication
Services should communicate via APIs to keep data consistent and boundaries clear.Final Answer:
Services are directly querying each other's databases. -> Option BQuick Check:
Direct DB queries cause inconsistency [OK]
- Assuming shared schema is the problem
- Thinking async API calls cause inconsistency
- Believing separate DBs cause inconsistency
Solution
Step 1: Understand distributed transaction challenges
Two-phase commit is complex and reduces service independence, so it's rarely used in microservices.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.Final Answer:
Implement eventual consistency using event-driven communication and compensating actions. -> Option AQuick Check:
Event-driven eventual consistency [OK]
- Trying distributed two-phase commit in microservices
- Sharing a single database defeats independence
- Periodic data copying causes stale data
