← Lessons

quiz vs the machine

Gold1430

System Design

The Outbox Pattern Revisited

Making the database write and the event publish atomic without a distributed transaction.

5 min read · core · beat Gold to climb

The dual write problem

A service often must update its database and publish an event. Doing both as two separate calls is a dual write. If the database commits but the publish fails, consumers never learn about the change. The outbox pattern removes the gap.

How the outbox works

  • In the same database transaction that changes business data, insert the event into an outbox table.
  • Because both writes share one transaction, they commit or fail together.
  • A separate relay reads new outbox rows and publishes them to the broker, then marks them sent.

Why it is reliable

  • The atomic local write guarantees that if data changed, the event exists.
  • The relay retries publishing, giving at least once delivery.
  • Because of retries, consumers may see duplicates, so they must be idempotent.

The relay can poll the table or tail the database change log for lower latency.

Key idea

The outbox writes the event in the same transaction as the data, then a relay publishes it, turning an unsafe dual write into one reliable atomic step.

Check yourself

Answer to earn rating on the learn ladder.

1. What problem does the outbox pattern solve?

2. Because the relay retries publishing, consumers must be