Database per Service Pattern in Microservices
On this page
- Separate services, one shared database
- The database per service rule
- Replacing cross-service joins: API composition vs read models
- Living without cross-service transactions
- Reporting across services
- A bonus: the right store per service
- Migrating from a shared database to database per service
- Check yourself
Separate services, one shared database
A team splits its monolith into services, but they all still read and write the same database. It seems efficient. Then the order team renames a column, and the reporting service, the shipping service and a nightly job break. Nobody could deploy independently after all; the database schema tied them together. That's why microservices usually follow database per service.
The database per service rule
Each service owns its data store. Other services never query or write its tables directly. They go through its API or consume its events. The service can change its schema, add indexes or even switch database products without coordinating with anyone, as long as its API and events stay compatible.
"Own database" doesn't have to mean a separate server. Separate schemas or databases on a shared server work, as long as access is enforced, for example with separate credentials per service.
Replacing cross-service joins: API composition vs read models
"Show a customer's orders with product names and thumbnails" used to be one SQL join. Now orders live in the order service, product details in the catalog service. Two standard replacements:
API composition. The caller (or a BFF, Day 18) fetches the orders, collects the product ids, calls the catalog service once with all of them, and merges the results. Simple and always current. It costs extra calls and latency, and fails or degrades if the catalog service is down.
A replicated read model. The order service keeps a local table of the product fields it needs (name, thumbnail URL), updated from the catalog's ProductUpdated events. Reads are local and fast, and work when the catalog service is down. The copy is eventually consistent, and you write the projection code.
| API composition | Replicated read model | |
|---|---|---|
| Freshness | Always current | Slightly behind |
| Latency | Extra calls per read | Local query |
| Depends on other service at read time | Yes | No |
| Effort | Low | Event handling and backfill |
Often the cleanest answer is a snapshot at write time: the order stores the product name and price as they were when ordered (Day 21), which is what order history should show anyway.
Living without cross-service transactions
No foreign keys between services, no transaction across two services' data. Instead:
- keep data that must change atomically inside one service (a reason to draw boundaries carefully, Day 52),
- use sagas for multi-service workflows (Day 49),
- publish events through an outbox so data and events stay in step (Day 50),
- tolerate and reconcile references that briefly point to something deleted, for example an order referencing a discontinued product.
Reporting across services
Finance still wants "revenue by product category by region", which spans orders, catalog and customers. Don't let a reporting service query every service's database; that recreates the shared schema through the back door. Stream each service's changes into a data warehouse with change data capture or events (Day 29), and join there. Reporting reads a copy designed for it, and services stay free to change their own schemas.
A bonus: the right store per service
Because each store is private, each service can pick what fits (Day 24): orders in PostgreSQL, the catalog in a document store, search in OpenSearch, sessions in Redis. Teams can do this without a company-wide database migration.
Migrating from a shared database to database per service
Teams rarely start clean. A pragmatic path: assign each table one owning service; route other services' writes through the owner's API first; then replace their reads with APIs or read models; finally move the tables into the owner's database. Do it one table group at a time, alongside the strangler fig approach (Day 59).
Two things get harder once data is spread out. A restore no longer gives you one consistent moment across the whole system, because each service's database is backed up separately; services must tolerate references to records another service has lost and reconcile them. And a "delete my data" request must reach every service holding that customer's data, so keep a list of which services hold personal data and publish a deletion event they all handle.
Check yourself
Services become coupled through the schema, so one team's change can break others and deployments must be coordinated, which removes the independence services were meant to provide.
Compose via APIs (fetch orders, then batch-fetch products from catalog), keep a local read model of product fields fed by catalog events, or snapshot the name on the order when it's placed.
No. Separate schemas or databases on a shared server work if access is enforced so each service can only reach its own data.
A warehouse fed by change data capture or events from each service, where cross-service joins happen without coupling anyone to another service's schema.
One service at a time: give it ownership of its tables, route every other service's access through its API or events, then move the tables to its own schema or database. Change data capture or dual writes can keep copies in sync during the transition.