Volume 7 · Chapter 4 — Microservices and System Design
Data and Consistency in Microservices
4.1 Introduction
Data ownership per service is what makes microservices independent, but it creates a hard problem: a business operation that spans several services can no longer be a single database transaction. This is where eventual consistency, the saga pattern, and reliable event propagation come in. For a bank, where correctness of money movement is paramount, handling cross-service data consistency correctly is critical - and it’s a deep senior interview topic because it separates people who understand distributed data from those who assume a single database everywhere.
4.2 Database per Service and Its Consequence
As established, each microservice owns its own database, which gives independence but eliminates the option of one ACID transaction across services. Within a single service, you still get full ACID transactions on its own database. But an operation that spans services - say a transfer that debits an account in the accounts service and records a payment in the payments service - cannot be wrapped in one database transaction, because they’re separate databases owned by separate services. This is the central consequence of the database-per-service rule, and it forces you to achieve consistency across services differently, through coordination and eventual consistency rather than a single atomic transaction.
4.3 Eventual Consistency
Across services, you typically accept eventual consistency: after an operation, the different services’ data becomes consistent after a short delay rather than instantly. When the payment service records a payment and publishes an event, the notification and ledger services update slightly later as they consume it - so for a brief window the system isn’t fully consistent, but it converges. This is a deliberate trade: you give up the immediate strong consistency of a single database in exchange for the independence and resilience of separate services. The design implication is that the system and its user experience must tolerate brief inconsistency - showing “payment processing” rather than assuming instant global truth - and that correctness-critical invariants within a single service still use that service’s ACID transactions.
4.4 The Saga Pattern
For a multi-step operation spanning services that must maintain consistency, the saga pattern coordinates it as a sequence of local transactions, each in one service, with compensating actions to undo prior steps if a later one fails. Instead of one distributed transaction, a saga executes step by step - each service does its local ACID transaction and triggers the next - and if a step fails, the saga runs compensating transactions to reverse the already-completed steps, restoring consistency. For example, a transfer might reserve funds, then record the payment, then confirm; if confirmation fails, a compensating action releases the reserved funds. A saga thus provides consistency across services without a distributed transaction, at the cost of designing compensations and handling the intermediate states - the standard way to keep multi-service workflows correct.
sequenceDiagram
autonumber
participant Acc as Accounts
participant Pay as Payments
participant Led as Ledger
Acc->>Acc: local txn: reserve funds
Acc->>Pay: proceed
Pay->>Pay: local txn: record payment
Pay->>Led: proceed
alt ledger succeeds
Led->>Led: local txn: post entry - saga complete
else ledger fails
Led-->>Pay: failure
Pay->>Pay: compensate: void payment
Pay-->>Acc: failure
Acc->>Acc: compensate: release reserved funds
end 4.5 Reliable Events and Idempotency
Sagas and cross-service consistency depend on reliably propagating events between services, which needs care. The outbox pattern ensures a service’s state change and the event it must publish are atomic: the event is written to an outbox table in the same local transaction as the state change, then reliably published, so the database update and the event can never diverge (you never update state but lose the event, or publish an event for a change that rolled back). Consumers must be idempotent because messages can be redelivered - processing the same event twice must not double-apply it, achieved with idempotency keys. Together, the outbox pattern (atomic state-plus-event) and idempotent consumers (safe redelivery) make event-driven cross-service consistency reliable, which is essential when the events drive financial state.
4.6 Banking Production Scenario
A bank keeps each service’s data private and uses events to maintain consistency across them. Within a service, money operations use that service’s ACID transactions. A cross-service transfer is coordinated as a saga: reserve funds in accounts, record the payment in payments, post to the ledger - each a local transaction - with compensating actions (release the reservation, void the payment) if a later step fails, so the transfer is consistent without a distributed transaction. Events are published via the outbox pattern so a state change and its event are atomic, and every consumer is idempotent so redelivered events never double-apply. The user sees “processing” during the brief eventually-consistent window. This keeps money correct across independent services - the defining challenge of distributed financial data.
4.7 Troubleshooting: Cross-Service Data Problems
Distributed data bugs are serious and specific. A partially-completed multi-service operation (one service updated, another not) means there’s no saga with compensations - add one so failures trigger compensating actions that restore consistency. State and events diverging (a change made but its event lost, or vice versa) means the outbox pattern isn’t used - adopt it so state and event are atomic. Double-applied effects from redelivered events mean consumers aren’t idempotent - add idempotency keys. Bugs from assuming instant consistency across services mean the design didn’t account for eventual consistency - make the UX and logic tolerate the brief window. Attempting a single transaction across services simply won’t work with database-per-service - use a saga. The discipline is ACID within a service, sagas with compensation across services, the outbox pattern for reliable events, and idempotent consumers.
Since each microservice owns its data, how do you keep a multi-service operation consistent?
Because each microservice owns its own database, a multi-service operation can’t be wrapped in a single ACID transaction, so I achieve consistency through eventual consistency, the saga pattern, and reliable event propagation. The starting point is the consequence of database-per-service: within a single service I still get full ACID transactions on its own database, but an operation spanning services - like a transfer that debits an account in the accounts service and records a payment in the payments service - involves separate databases owned by separate services and therefore can’t be one atomic transaction. Across services I typically accept eventual consistency: after the operation, the services’ data becomes consistent after a short delay rather than instantly, converging as events are consumed. That’s a deliberate trade of immediate strong consistency for the independence and resilience of separate services, and it means the system and its user experience must tolerate a brief window - showing “processing” rather than assuming instant global truth. For a multi-step operation that must stay consistent, I use the saga pattern, which coordinates it as a sequence of local transactions - each a normal ACID transaction in one service - with compensating actions to undo earlier steps if a later one fails. Instead of one distributed transaction, the saga proceeds step by step, and if a step fails it runs compensating transactions to reverse the completed steps and restore consistency - for example, a transfer reserves funds, records the payment, and posts to the ledger, and if the ledger step fails, compensations void the payment and release the reserved funds. This gives consistency across services without a distributed transaction, at the cost of designing compensations and handling intermediate states. Underpinning this, I make event propagation reliable with the outbox pattern - writing the event to an outbox in the same local transaction as the state change, then publishing it, so the state update and the event can never diverge - and I make every consumer idempotent using idempotency keys, because messages can be redelivered and processing one twice must not double-apply it, which is critical when events drive financial state. So the answer is: ACID transactions within each service, eventual consistency across services that the design tolerates, the saga pattern with compensating actions to keep multi-step cross-service operations consistent without a distributed transaction, and the outbox pattern plus idempotent consumers to propagate the events reliably. That combination is how you keep money movement correct across independent services, which is the defining hard problem of distributed data and exactly what a bank must get right.
Chapter Revision Notes
- Database-per-service gives independence but removes the option of one ACID transaction across services.
- Across services you accept eventual consistency (data converges after a short delay); design the UX/logic to tolerate it.
- Saga pattern: a multi-step operation as a sequence of local transactions with compensating actions to undo on failure - consistency without a distributed transaction.
- Outbox pattern makes a state change and its event atomic so they can’t diverge; idempotent consumers make redelivery safe.
- ACID within a service, sagas + compensation across services, outbox + idempotency for reliable events - especially for money.