Data Across Services
Question: when each service owns its data, how do you keep data consistent across services, answer queries that need data from several of them, and publish changes without losing them?
This question appears as soon as a design has more than one service with its own storage. With a single database, one ACID transaction and one SQL join handle these questions. Splitting the data removes both options.
Forces
- Independent change: a service whose schema is private can change it without coordinating with other teams. This pushes toward private data. See boundaries.
- Consistency: an invariant spanning two services can no longer be enforced by one local transaction. Decide what each operation needs to observe. See consistency and transactions.
- Query latency and availability: a query that calls several services waits for the slowest and fails when any of them fails. A local copy removes the calls but serves data that may be stale.
- Simplicity: a shared database is the simplest arrangement to build and the hardest to change later.
Who owns the data
Ownership matters more than physical separation: the rule is that no other service reads or writes the tables directly.
Before choosing a coordination pattern, check the boundary. If an invariant must hold atomically across two services in most operations, the data may belong in one service.
Operations that change several services
Coordinating a saga:
- Choreography: each participant reacts to the events of the others. There is no central component, but the flow exists only implicitly across services.
- Orchestration: an orchestrator tells each participant what to do next. The flow is explicit in one place, at the cost of a component that knows every step.
Structure of a saga:
- Compensatable steps can be undone.
- The pivot step decides whether the saga will complete.
- Retriable steps after the pivot must eventually succeed.
Ordering the steps this way limits how much needs compensation.
Missing isolation: Richardson describes sagas as ACD. Every step either completes or is compensated, but there is no isolation: another request can see or overwrite intermediate state. He lists countermeasures, among them:
- a semantic lock: a status such as
PENDINGthat marks a record as in use; - commutative updates;
- rereading a value before overwriting it.
Queries that need several services
The scalability reading notes also discuss CQRS as a scaling technique. They tie it to event sourcing and stress hiding the separate models behind the service interface. Use it where read and write needs differ enough to justify the extra moving parts.
A stated position against replicas: some organizations rule that a consuming domain must not keep a copy of another domain’s state, and must ask the system of record instead. That rule rules out CQRS views and command-side replicas that span domains, and accepts the latency and availability coupling of synchronous queries in exchange for a single source of truth.
Representing change
Describe the purpose of an event and the state it represents before choosing the mechanism. The original messaging collection also distinguishes:
- event-carried state transfer: events carry enough state for consumers to maintain a copy. This is the mechanism behind replicas and CQRS views.
- integration events: events published across a boundary, as distinct from events used inside one domain.
- workflow events: events that drive the steps of a process.
Event sourcing is a question of how state is persisted and reconstructed. Keep it distinct from the choice of queue or broker in messaging.
Publishing changes reliably
The dual-write problem: a service commits a change to its database and then publishes a message. If the process crashes or the broker is unavailable between the two, the change exists but nobody hears about it. Publishing first has the opposite problem.
Event sourcing avoids the dual write: appending to the event store is both the state change and the publication.
The URL-shortener NFR draft works through an outbox in detail. The write service records each new link and a link_created event in one transaction, and a relay propagates it to the read path.
Leads to
- Sagas and replicas communicate by messaging, so you need delivery semantics and ordering. See communication and messaging.
- At-least-once publication leads to idempotent consumers.
- Views and replicas introduce staleness: specify what each operation may observe in consistency.
- Frequent cross-service invariants suggest revisiting the boundaries.
Seen in cases
- URL shortener: outbox for read-path propagation; different PACELC choices for the read and write paths.
- Ticketmaster: the reservation flow can be read in saga terms. A
RESERVEDstatus with an expiry works as a semantic lock, the external payment is the pivot, and expiry releases the ticket when checkout is abandoned. The walkthrough itself does not use the term. - AI and RAG: the RAG runtime layers prefer an authoritative system of record over document retrieval for facts that exist as rows. They give the index freshness targets, versioning, and invalidation. Reading the index as a CQRS-style derived view is an interpretation, not the note’s own framing.
Gaps
- A worked saga with compensations and isolation countermeasures; no case uses one explicitly yet.
- Choosing between choreography and orchestration in a concrete flow.
- Event schema design and evolution; ordering guarantees per aggregate.
- A case that needs CQRS or a command-side replica.
- Two-phase commit, XA, and sagas with the outbox are covered in the transactions notes; no case here compares them on a concrete flow yet.
Sources
- Database per service, shared database, saga, command-side replica, API composition, CQRS, domain event, event sourcing – microservices.io.
- Transactional outbox, transaction log tailing, polling publisher – microservices.io.
- Eventuate and Eventuate Tram: a framework implementing these patterns. Example applications.
- Richardson 2019: chapter 3 (transactional messaging), 4 (sagas), 5 (aggregates and domain events), 6 (event sourcing), 7 (API composition and CQRS).
- Newman 2021: chapter 6 (workflow, sagas, orchestration and choreography). Newman 2020: chapter 4 (decomposing the database).
Library sources
- Richardson 2019, Microservices Patterns: With Examples in Java – chapters 3-7
- Newman 2021, Building Microservices: Designing Fine-Grained Systems, Second Edition – chapter 6
- Newman 2020, Monolith to Microservices: Evolutionary Patterns to Transform Your Monolith – chapter 4
Further reading:
- Ford et al. 2022, Software Architecture: The Hard Parts
- Kleppmann and Riccomini 2026, Designing Data-Intensive Applications, Second Edition
- Wlaschin 2018, Domain Modeling Made Functional – chapter 12, “Bounded Contexts Must Own Their Data Storage” (from a separate database down to a namespace in a shared one; reporting as its own context fed by events) and “Command-Query Separation” (CQRS, separate read stores); chapter 6, “Consistency” (between contexts: asynchronous messages, with reconciliation or compensation instead of two-phase commit); on Who owns the data, Queries that need several services and Operations that change several services