Dual Writes (and how to avoid them)
Problem
A service needs to update two systems as part of one logical operation — the classic example is: append an event to the event store and publish it on Service Bus so downstream consumers see it. Or: update a projection and publish an integration event. Or: write to Cosmos and call an external API.
If the two writes are not coordinated, the system can end up in an inconsistent state:
- Event store updated, message not sent → downstream never learns of the change.
- Message sent, event store update fails → downstream acts on an event that never happened.
Distributed transactions across Azure PaaS + external systems are not available (or are expensive and brittle). The pattern is: don’t do dual writes — turn one of the writes into a consequence of the other.
Solution
Two canonical approaches ION uses:
1. Transactional Outbox
- Write both the domain change and an outbox record (the message to publish) in the same transaction to the same store.
- A relay reads outbox records and publishes to Service Bus, then marks them delivered.
- On crash / retry, unpublished records are re-picked up — combined with idempotent consumers (Idempotent Write) this is at-least-once with guaranteed eventual delivery.
Storage options in ION: the event store itself (Cosmos) — the event is the outbox; a relay reads change feed and publishes.
2. Event Sourcing — the event is the fact
In event-sourced services, the event append IS the write. Consumers subscribe to the change feed / integration stream; there is no separate “update DB + publish message”. This is the preferred ION pattern per ADR-0006.
3. Change Feed Fan-out
- Cosmos DB Change Feed is the source of publication.
- A publisher process reads the change feed and emits events onto Service Bus.
- The write is single (Cosmos append); publication is a derived side-effect with checkpoint tracking.
When to use
- Any time a service is tempted to update two systems and hope both succeed.
- Specifically: event store + message bus, projection + event, domain state + external API call.
Not: trivial single-store updates with no external observable.
Consequences
Positive
- Eventual consistency guaranteed under crashes / retries.
- No 2PC dependency.
- Combines cleanly with idempotent consumers, schema evolution, and event-sourced replay.
Negative
- Latency between the write and downstream visibility (typ. ms–s).
- Requires a durable outbox / change-feed reader with checkpointing.
- Debugging “why hasn’t the event landed?” adds a hop.
Failure modes
- Trying to skip the outbox and publish inside the domain transaction — reintroduces the dual-write problem.
- Non-idempotent consumers downstream — outbox at-least- once semantics corrupt them.
- Lost checkpoint in the change-feed reader — either re-publishes (idempotency saves you) or misses events (data loss).
References
references/Design Pattern- Handling Dual Writes.md- ADRs: 0006, 0008, 0011
- Related: Idempotent Write, ECST, DLQ
Applied in
- Watchtower incident bounded context — Cosmos change feed → Service Bus fan-out
- Cogs — Temporal activity + event emit via outbox
- All projections — derived from event store change feed, never dual-written
- Ticket bounded context — event store + ServiceNow sync via change-feed reader