Microservices Data Patterns: Database per Service, Saga, Outbox and CQRS
Once each service owns its data, transactions and queries break. Four patterns that put them back, with the failure each one prevents and the cost it adds.
On this page
The first week after a monolith is split goes well. The second week someone asks how to place an order that charges a card, reserves stock and sends an email across three services, and nobody can put it inside a transaction. The patterns below are the standard answers.
Three problems microservices introduce
| Problem | Why it appears |
|---|---|
| Data consistency | Data lives in many stores, so a business operation spans several, and they can briefly disagree |
| Attack surface | More services and endpoints to authenticate and protect (a gateway and mutual TLS help) |
| Database bottlenecks | The app tier scales out easily, but a shared or poorly partitioned data tier does not |
This article covers the data side, which is where most of the pain lives.
Pattern 1: database per service
Each service owns its data and exposes it only through its API or events. No service reads another’s tables.
You can enforce it softly (separate schemas with separate credentials in one server) or hard (separate servers). Start with the soft version if operations are a concern, as long as nothing reaches across the boundary.
Pattern 2: saga, for operations that span services
Take “place an order”: reserve stock, charge the card, confirm the order. No single transaction covers all three, so a saga runs them as a sequence of local transactions, each followed by an event, with an undo step for each.
Order created
-> Inventory: reserve stock (undo: release stock)
-> Payment: charge card (undo: refund)
-> Order: confirm (undo: cancel)
If Payment fails: run undo for Inventory, then mark the order cancelled
Two coordination styles:
| Style | How it works | Good for | Watch out for |
|---|---|---|---|
| Choreography | Services react to each other’s events, no central brain | Few steps, simple flows | Flow hard to see, cyclic dependencies |
| Orchestration | A coordinator tells each service what to do and tracks state | Many steps, complex branching | The orchestrator becomes a component to own and keep available |
A compensating action is a new business operation, not a database undo. Refunds take time, emails cannot be unsent, and in between, other users can see the intermediate state. Design the steps and the compensations as part of the product, including what the customer sees while it runs.
Pattern 3: transactional outbox, so events are not lost
Sagas depend on publishing events reliably. The trap is the dual write.
1. UPDATE orders SET status='paid' <- succeeds
2. publish OrderPaid to the broker <- process crashes before this
The database changed, nobody heard. Reverse the order and you can announce something that never committed. The outbox makes both facts atomic:
- Write the event in the same transaction
Insert the event into an
outboxtable in the same database and the same transaction as the business change. Both commit or neither does. - Relay it afterwards
A separate process reads new outbox rows and publishes them to the broker, either by polling or by reading the database change log. It marks them as sent.
- Consume idempotently
The relay can publish twice after a failure, so consumers must handle duplicates. Give every event a unique id and record which ones were processed.
Pattern 4: CQRS, so reads get their own model
Command Query Responsibility Segregation separates the model that handles writes (commands, with all the validation rules) from the models that serve reads (queries, shaped for screens). Updates flow from the write side to the read side by events.
It earns its keep when:
- A screen needs data from several services at once. Build a read model that joins them ahead of time, since a runtime join across services is not available.
- Read and write loads differ by orders of magnitude and should scale independently.
- Reporting or search queries would otherwise hammer the transactional store.
The cost is eventual consistency: after a write, the read model lags by a short time. Show the user their own change immediately, or read your own writes from the command side, so it does not feel broken.
Choosing, honestly
| You face | Use | Do not bother if |
|---|---|---|
| Teams blocked by a shared schema | Database per service | You have one team and one deployable |
| An operation touches several services | Saga | One service can own the whole operation |
| Lost or phantom events | Outbox | You do not publish events |
| A query needs data from many services, or reads dwarf writes | CQRS read model | A single service query is fast enough |
Do
Make every handler idempotent, give events unique ids, log saga state so stuck operations can be found, and define the compensation for every step before building the forward path.
Don't
Reach for two-phase commit across services, share a database “just for now”, or adopt all four patterns on day one. Each pattern adds moving parts that need monitoring and an owner.
Takeaway
Microservices trade one transaction for many independent ones, and these four patterns are how you earn coherence back: a clear data boundary, sagas for multi-step operations, an outbox for reliable events, and CQRS for queries that cross boundaries. Each costs consistency and complexity, so adopt them one at a time, driven by a failure you can name.
Frequently asked questions
Why should each microservice have its own database?
So services can change schema, scale and deploy independently, and so no other team can couple to your tables. Shared databases turn into a distributed monolith: any change needs coordination. The cost is that cross-service transactions and joins no longer exist and must be rebuilt with patterns.
What is the saga pattern?
A way to complete a business operation that spans several services without a distributed transaction. Each service does its local transaction and publishes an event. If a later step fails, earlier steps are undone by compensating actions, such as refunding a payment, so the system returns to a consistent state.
What problem does the transactional outbox solve?
Writing to your database and publishing a message are two separate operations, so a crash between them loses the event or publishes one for data that was never saved. The outbox writes the event to a table in the same local transaction, and a relay publishes it afterwards.
When is CQRS worth the complexity?
When read and write needs differ sharply: heavy reporting or search views, very different scaling, or complex write rules. It separates the model that changes data from the models that serve queries, at the price of eventual consistency between them. For simple CRUD it is over-engineering.



/* Comments */
Comments are offline right now — we reconnect automatically, nothing is lost.