Q12 · What patterns help maintain consistency across multiple services?
Back to 24 Architecture Questions.
The question is not “keep data in sync”. The axis it turns on: which service owns the fact, and how a reader learns it changed. Two services holding “the same” data are not a sync problem; they are an ownership question: which service is allowed to change it. Consistency across services is what you get after ownership is settled: one owner, and copies that are projections of the owner’s facts.
What the answer should contain
Assign every fact exactly one owner. The owner is chosen by domain ownership, not by which team has the table today. Everything else holding that data is a projection: a copy that is consistent only by a stated policy, never by hope.
Propagate by events, atomically with the commit. The owner appends the fact to a log as part of committing it (the outbox pattern: the business row and the event land in one local transaction). Consumers build and update their copies from the log, which is why the transport has to be a log and not a queue: a late consumer needs to re-read from its own position.
Know each copy’s freshness as a number. “Eventually consistent” is not a design; it is an apology. A copy carries its last-applied event timestamp, and readers can see how far behind it is. Staleness becomes a measured property with an alarm, not a surprise.
When a reader must be current, ask the owner. A targeted synchronous read of the one fact, from the owner, for the one use case that needs it (the payment-status check). This is a scalpel: syncing everything back to the owner recreates the monolith with extra network hops.
Two writers on one fact is a boundary bug. If two services can mutate the same fact, the service boundaries are wrong, the lost-update races are back, and no pattern will save the design until ownership is reassigned.
Invariants spanning services become sagas. “Order charged implies stock decremented” cannot be a distributed transaction in most realistic stacks; it becomes a saga of local steps with declared compensations, or the invariant moves inside one owner’s service, where a plain transaction can hold it.
Ideas worth mentioning out loud
- “Shared databases are consistency by accident: it works until the moment it silently doesn’t, and then two services own every fact.”
- The event log is also the rebuild path: a corrupted projection is rebuilt by replay, with no producer involvement at all.
- Read-your-writes across services is solved with a cursor: wait for the reader’s copy to reach the event the write produced, then serve.
Follow-ups to expect
- What about two-phase commit? It holds locks on every participant across network round-trips and makes the coordinator a failure domain, so in most stacks the short answer is “locks across a network for a benefit the business cannot see”.
- How do you find the owner? Domain boundaries: the fact belongs to the service whose language the fact is a sentence in.
- What if a consumer is down for an hour? That is the point of the log: it catches up from its own cursor, and staleness was visible the whole time.
Red flags in an answer
- A shared database used as the integration mechanism.
- Bidirectional sync between two services.
- Copies with no freshness measurement.
- “Eventually consistent” stated with no policy, no lag metric, and no rebuild path.
Sources
- Pattern: Transactional outbox, Chris Richardson, microservices.io: the mechanism that makes the fact and the event land atomically, which is the foundation every projection rests on.
- Designing Data-Intensive Applications, Martin Kleppmann, O’Reilly: chapter 11, event flows and derived data. The owner-versus-projection framing this answer uses is Kleppmann’s, argued across the book’s second half.