Skip to content
← All posts
  • June 30, 2026
  • 6 min read
  • #design-systems

The two writes that were never one write

A microservice that updates its database and publishes an event isn’t doing one thing. It’s doing two things and hoping nothing happens in between.

A customer got charged twice. The database showed one order. The queue showed two events.

A microservice that updates its database and publishes an event isn’t doing one thing. It’s doing two things and hoping nothing happens in between.

Priya found this out on a Tuesday morning, staring at a support ticket that said a customer had been charged twice for the same order. The order service logs showed exactly one INSERT. The payment queue showed exactly two messages.

Her first instinct was to blame the queue. SQS doesn’t lose messages, but it does redeliver them, so maybe a consumer crashed mid-processing and the retry caused the double charge. That’s a real failure mode and worth ruling out. She checked the consumer logs. No crash, no retry, no redelivery. The order service had published the event twice, on purpose, because the code published a message every time it ran, and it had run twice. The database had not written the order twice. The state was already inconsistent before the message ever left the building.

That’s the actual shape of the dual-write problem, and it’s worth being precise about it because the obvious mental model is wrong. The obvious mental model is: “the database and the queue are both reliable, so if I write to both, I’m fine.” Both can be reliable on their own and the combination can still be broken, because nothing guarantees they happen together.

Why “just write to both” doesn’t compose

Here’s the code Priya inherited, simplified:

def create_order(order):
    db.orders.insert(order)
    queue.publish("OrderCreated", order)

Two operations, two systems, no shared transaction. A microservice often needs to perform two distinct operations as a single atomic unit: update its local database state, then publish an event to a message broker. The challenge is that the database and the message broker don’t share a single transaction coordinator.

That gap between the two lines is where every dual-write bug lives. AWS’s own architecture guidance walks through both directions of failure plainly: if the database update succeeds but the event notification fails, downstream services never find out about the change and the system drifts out of sync. If the database update fails but the notification still goes out, a service downstream may act on a change that never actually happened.

Priya’s bug was the second kind, with a twist: the code ran twice (a duplicate request, a retried Lambda invocation, doesn’t matter which), the database deduplicated the insert correctly, but nothing stopped the publish line from firing both times. The database was consistent. The event stream was not. The payment service had no way to know the second event was a re-run of the first.

This is the part that surprises people: you don’t need a crash to get this bug. You need two systems that don’t share a notion of “this happened once.”

The three things people reach for first, and why they’re each missing something

Two-phase commit. The textbook answer to “make two writes atomic” is a distributed transaction. Two-phase commit protocols can theoretically solve this, but they introduce performance overhead from multiple round trips and locks, real complexity in coordinating every participant, and availability problems, since if any participant fails, the entire transaction blocks. Many modern systems don’t even support it, and most message brokers, including SQS and Kafka, aren’t 2PC participants at all. This option is mostly theoretical for the database-plus-broker case.

Retry until it works. If the publish fails, retry it. This handles transient network blips, but it doesn’t handle the case Priya hit: the publish didn’t fail, it just ran an extra time because the calling code ran an extra time. Retries assume the failure is in the call. Sometimes the failure is in deciding to make the call.

Best-effort, fix it later. Write to the database, fire the event, and reconcile any drift with a nightly batch job. This works until someone asks why a customer got charged twice three days before the reconciliation job runs.

None of these are wrong exactly. They’re each solving a narrower problem than the one that’s actually there: the database and the broker need to agree on whether something happened, not just on whether each of them, individually, did their job.

The pattern that actually closes the gap

The fix that’s held up across most production systems doesn’t try to make two systems transactional together. It makes the second write happen inside the first one.

Instead of sending the message directly to the broker, you save the message to a table in the same database as your business data. Since the orders table and the outbox table are in the same database, you can wrap writes to both in a single ACID transaction. A separate process then reads the outbox table and relays each row to the real message broker.

The trick is that the database transaction is the only thing that needs to be atomic, and databases are already good at that. You’re not making the database and the broker agree. You’re making the database agree with itself, and then handing the broker problem to a process that can fail and retry as many times as it needs to, because it’s idempotent by construction: it’s just relaying rows that already exist.

If the transaction fails partway through, neither the order row nor the outbox row exists, so there’s nothing to publish and nothing to roll back inconsistently. If the transaction succeeds, both rows exist together, and the relay process will eventually deliver the event, possibly more than once if it crashes mid-send, but that’s now a duplicate-delivery problem, which is solvable with an idempotency key on the consumer side. That’s a much easier problem than the one Priya started with, where the source of truth itself was ambiguous.

For Priya’s bug specifically, this would have caught it for free. The duplicate request still would have triggered the handler twice, but the database’s own unique constraint on order ID would have rejected the second insert, the second outbox row would never have been written, and the relay would have had nothing to send. The two writes failing together, instead of independently, is the entire point.

Where this gets more complicated than the diagram

The relay process polling the outbox table is itself a thing that can go down, lag, or duplicate. Order of notification matters too: messages need to go out in the same order the database was updated, which is straightforward with one outbox table and one relay worker, and much less straightforward once you shard the database or run multiple relay instances for throughput.

There’s also a second way to build this that skips the outbox table entirely. Some databases support change data capture, where you read the database’s own commit log instead of writing to a second table. DynamoDB Streams and Postgres logical replication (via tools like Debezium) both work this way. You get the same atomicity guarantee, because you’re not writing anywhere extra at all, but you take on a new dependency: your event pipeline now needs to understand the database’s internal change format, and CDC lag, while normally small, is a real number you should measure rather than assume.

Neither version makes the duplicate-delivery problem disappear. At-least-once delivery from the broker side is the tradeoff you’re accepting in exchange for not losing events, so the consumer still needs to be idempotent. The outbox pattern moves where the hard problem lives. It doesn’t make it vanish.

When dual writes are actually fine

Not every database-plus-broker write needs this. If the event is advisory, like a cache invalidation hint or an analytics ping where an occasional miss just means slightly stale data, the cost of building and operating an outbox table and a relay worker is probably not worth it. The pattern earns its complexity when a downstream system makes a decision based on the event, money moves, an order ships, an account gets provisioned, and a missed or duplicated event causes a problem a human has to clean up.

Priya’s team added the outbox table to the order service and nowhere else in their stack, on purpose. The notification service that sends “your order shipped” emails still does a plain dual write, because a customer getting that email zero or two times is a minor annoyance, not a support ticket about a duplicate charge.