System Design AI

The outbox pattern in System Design Interviews: When to Use It and What to Say

When you design a microservice that must publish events after persisting data, the simplest code often looks like this:

Java
saveOrder(order);
publishEvent(orderCreated);

If the publish fails after the database commit, the system ends up with an order that no consumer ever sees. The outbox pattern fixes that by storing the event in the same transaction that writes the order, then letting a separate process reliably forward it to the message broker.

The problem: write-then-publish inconsistency

In many interview prompts—e.g., design an e-commerce order service—the order service must:

  1. Record the order in a relational DB (the source of truth).
  2. Emit an "OrderCreated" event so downstream services (inventory, email, analytics) can react.

If the service crashes after step 1 but before step 2, the order is lost to the rest of the system. The same issue appears in:

  • User-profile sync – a profile service updates a DB and must push a change to a cache.
  • Audit logging – a financial service writes a transaction and must record an audit event.

The outbox pattern guarantees that either both actions happen or neither does, without requiring a distributed transaction.

How it works: step-by-step

  1. Add an outbox table – a simple table with columns like event_id, aggregate_id, payload, status.
  2. Write within a single DB transaction –
  • Insert the primary business row (order, profile, etc.).
  • Insert a matching outbox row containing the serialized event.
  1. Commit the transaction – the DB now holds both the business data and the event.
  2. Run a poller (or use DB triggers) – a background worker reads rows where status = 'PENDING'.
  3. Publish to the broker – the worker sends the payload to Kafka, SNS, etc.
  4. Mark the row as SENT – update the outbox row after a successful publish.
  5. Retry on failure – the worker can re-process rows that stay PENDING.

Because the write and the outbox entry are atomic, the system never loses an event. The poller decouples the latency of the broker from the user-facing request, keeping the API response fast.

Worked example: order service with Kafka

StepActionDB stateKafka state
1Begin transaction––
2Insert orders row (order_id=123)orders: {123,…}–
3Insert outbox row (event_id=1, payload=OrderCreated(123))outbox: {1,PENDING,…}–
4Commit transactionorders & outbox persisted–
5Poller reads outbox where status=PENDINGreads row 1–
6Publishes OrderCreated to Kafka topic orders–topic orders: message 1
7Updates outbox row status=SENToutbox: {1,SENT,…}–

If step 5 or 6 fails, the outbox row stays PENDING and the poller retries. No order is ever stored without a corresponding event.

Where it shows up in classic interview questions

Interview questionWhere the outbox appears
Design an e-commerce order serviceAfter persisting the order, before publishing to the event bus.
Design a social-media feed systemWhen a new post is saved and an "ActivityCreated" event must be sent to the recommendation service.
Design a real-time analytics pipelineIn the ingestion service that writes raw events to a DB and must forward them to a streaming platform.

In each case, the outbox sits right after the write, before the asynchronous consumer chain.

Trade-offs and the alternative interviewers love to probe

AspectOutbox patternChange-Data-Capture (CDC)
ComplexitySimple table + poller; easy to reason about.Requires a log-based connector; can be opaque.
LatencyAdds a poll interval (often a few seconds).Near-real-time if the log is streamed instantly.
Duplication riskMust handle idempotent publishing; duplicate rows possible.CDC connectors typically deliver at least once, so consumers still need to be idempotent.
Storage overheadExtra rows until they are marked SENT.No extra tables; relies on DB's write-ahead log.
Operational burdenNeed a reliable poller and monitoring of PENDING rows.Need to manage connector deployment and schema evolution.

Interviewers often ask: "What if the poller crashes?" or "How do you guarantee exactly-once delivery?" Good answers mention idempotent consumer design, deduplication keys, and using the outbox row's primary key as the message ID.

30-second spoken version

"The outbox pattern solves the classic write-then-publish problem by storing the event in the same DB transaction that writes the business record. A background worker then reads pending outbox rows and pushes them to the message broker, marking them sent after a successful publish. This guarantees that either both the data and the event exist or neither does, without needing a distributed transaction. It adds a little latency and storage overhead, but the simplicity and reliability make it a go-to solution in many microservice designs."

Common candidate mistakes

  • Skipping the transaction – writing the business row and the outbox row in separate calls defeats the purpose.
  • Assuming the poller is instantaneous – forgetting to discuss the poll interval or how you handle back-pressure.
  • Ignoring idempotency – not addressing how downstream consumers avoid processing duplicate events.
  • Mixing up the direction – describing the outbox as a queue that the service reads from, rather than a store that the service writes to.
  • Over-engineering – proposing a full two-phase commit when the simple outbox would suffice.

Avoid these pitfalls by explicitly walking through the transaction, the poller, and the retry logic.

How to practice this

  1. Study the Transactional Outbox building block in the System Design AI library: one diagram of the mechanism, with its write-up.
  2. Draw your own canvas for a different scenario (e.g., profile sync) and label where the outbox sits.
  3. Explain the pattern aloud using the 30-second script, then answer follow-up questions about latency, deduplication, and alternatives.

Questions people ask

What is the main benefit of the outbox pattern?

It guarantees atomicity between persisting business data and emitting an event, without requiring a distributed transaction.

When should I prefer CDC over the outbox?

Choose CDC if you already have a robust log-shipping pipeline and need sub-second propagation; the outbox is simpler when you control the service code and can tolerate a few-second delay.

How do you handle duplicate events from the outbox?

Use the outbox row’s primary key as a unique message ID, and make downstream consumers idempotent by checking that ID before processing.

Can the outbox pattern be used with relational and NoSQL databases?

Yes. In relational DBs you create a dedicated table; in NoSQL stores you can use a separate collection or a dedicated bucket that is written in the same request.