Database Per Microservice
When multiple microservices read and write the same relational database, their schemas, query loads, and deployment schedules couple together at the storage layer. The database-per-service pattern gives each microservice exclusive ownership of its own datastore and exposes that data only through APIs or asynchronous events.
Why Shared Databases Couple Services
In a shared database, every service queries tables owned by other teams. That direct table access creates three failure modes as a system grows:
- Schema breakage: Renaming
products.pricetounit_price_centsin Product Service breaks any SQL query in Order Service that joins onproducts. Teams must coordinate releases across services before migrating a column. - Noisy-neighbor contention: Heavy analytical queries in Recommendation Service consume CPU, memory, and disk IOPS on the same host that serves checkout writes. Latency spikes in one workload spill directly into unrelated user paths.
- Coarse hardware scaling: Scaling storage capacity or compute requires resizing the entire shared cluster, even when only one service's traffic or data volume grew.
Enforcing the API Boundary and Polyglot Persistence
Under database-per-service, no service connects to another service's database directly. Order Service owns Order DB, Product Service owns Product DB, and Recommendation Service owns Rec DB. When Order Service needs product details, it calls Product Service's API instead of querying products tables. Because external callers depend only on the API contract, Product Service can rename columns, add indexes, or change its storage engine without breaking callers.
Private ownership also enables polyglot persistence. Polyglot persistence is the practice of selecting a different storage engine for each service based on its access pattern:
- Order Service uses a relational database (such as PostgreSQL) on fast SSDs for ACID transactions and strict schema validation.
- Product Service uses a document database (such as MongoDB) to store variable, nested product attributes without sparse columns.
- Recommendation Service uses a graph or analytical store (such as Neo4j) on a high-memory host (for example, 64 vCPU and 256 GB RAM) sized specifically for traversal queries.
Click ▶ Rename products.price and ⚡ Run Recommendation Scan in Shared Database mode below to watch schema breakage and CPU contention hit Order Service. Then switch to Database Per Service and repeat both actions to compare the blast radius.
Cross-Service Queries: API Composition vs. Local Read Models
Separating databases removes cross-service SQL joins. Displaying a customer's recent orders alongside current product titles now requires data from both Order DB and Product DB. Two patterns solve this read path:
- API Composition: An aggregator (or Order Service itself) fetches the order rows from
Order DB, collects the referencedproduct_idvalues, calls Product Service's batch endpoint (GET /v1/products?ids=...), and joins the results in memory. Batching is essential because sequential per-item calls turn a single page load into an N+1 network fan-out, turning roughly . API composition stays strongly consistent with Product Service, at the cost of higher read latency and runtime availability coupling to every service in the fan-out. - Local Read Model (CQRS): Product Service publishes product change events through Change Data Capture (CDC) or a message broker. Order Service consumes those events and maintains a local denormalized projection table of
(product_id, title, price). Reads run as fast local joins with no runtime dependency on Product Service, at the cost of eventual consistency (typically milliseconds to seconds of replication lag) and extra storage.
Cross-Service Consistency and Operational Overhead
Splitting tables across databases also removes cross-service foreign keys and single-database ACID transactions. In a shared database, a foreign key prevents deleting a product row while open orders reference it. Across separate databases, referential checks and multi-service writes (such as checkout spanning Order, Payment, and Inventory) must be coordinated in application logic using patterns like the Saga pattern or asynchronous reconciliation.
Operational cost scales with the number of datastores. Each separate database cluster needs its own backup schedule, failover testing, monitoring alerts, connection-pool tuning, and major version upgrades. Splitting one primary-plus-two-replica database into five per-service clusters grows the fleet from , and adding multiple database engines multiplies the operational expertise a team must maintain.
| Dimension | Shared Database | Database Per Service |
|---|---|---|
| Schema coupling | High — table changes can break other services | Low — schema is private behind an API contract |
| Cross-entity reads | Single SQL JOIN | API composition or local read-model projection |
| Multi-entity writes | Single local ACID transaction and foreign keys | Saga pattern or eventual consistency |
| Workload isolation | Shared CPU, memory, and IOPS | Dedicated hardware and storage engine per service |
| Operational overhead | Low — one cluster to back up and upgrade | High — N clusters and potentially multiple engines |
When to Split and When to Keep a Shared Database
Database-per-service pays off when multiple teams deploy services independently, when workloads have sharply different hardware or engine needs (such as OLTP checkout versus graph recommendations), or when noisy-neighbor isolation protects revenue-critical paths.
For a small team (for example, three to eight engineers) maintaining a tightly coupled domain, starting with a single relational database avoids distributed-transaction and multi-cluster overhead. Teams can still prepare for a future split by placing each module's tables in its own logical schema with separate database credentials, forbidding cross-schema joins in code review, and moving a schema onto a dedicated host only when scaling pressure or team autonomy justifies the cost.
Key idea. The database-per-service pattern eliminates schema coupling and noisy-neighbor contention by giving each service a private datastore accessed only through APIs or events, enabling polyglot persistence and independent scaling. The trade-offs are losing cross-service SQL joins and ACID foreign keys—resolved through API composition, local CQRS read models, or sagas—and multiplying database operational overhead across clusters.
Further reading
- Pattern: Database per service — Chris Richardson (microservices.io) — catalog of private-tables, schema-per-service, and server-per-service isolation levels.
- Building Microservices, Ch. 4–5 — Sam Newman — practical guidance on splitting monolithic databases safely.
- MonolithFirst — Martin Fowler — why small teams typically defer physical service and database splits until domain boundaries stabilize.