stackbook.io
🔍
← Back to Syllabus
Day 4Level 2 — Data in Motion

Moving Data: CDC, Outbox, and Dual Writes

Learn: 45 minMap: 30 minDesign: 55 min

Core Concepts (5)

  • 1.Writing to two systems directly can silently diverge
  • 2.The outbox makes a database write and an event atomic
  • 3.Change data capture reads the log instead of the application
  • 4.Consumers must tolerate duplicates and reordering
  • 5.Backfills are a first-class operation

Map to Your Experience

Think of a time two systems disagreed about the same record. How did you find out, and which one did you trust?

Design Brief: Publish Order Events Reliably

Learn

Sooner or later, data created in one system must appear in others: a search index, a warehouse, a billing service. The naive way is for the application to write to the database and then publish a message. This is the dual write problem, and it fails in ways that are hard to see.

If the database commit succeeds and the publish fails, other systems never learn of the change. If the publish succeeds and the commit fails, they learn of something that never happened. A crash between the two steps creates exactly these gaps. No amount of careful coding closes them without a shared transaction.

The outbox pattern closes the gap by writing the event into an outbox table in the same local transaction as the business data. A separate relay reads the table and publishes the events, marking them as sent. The event exists if and only if the data change exists.

Change data capture takes a related route. Instead of asking the application to emit events, a connector reads the database's own transaction log and turns committed changes into a stream. This keeps application code simple and captures changes made by any tool, but it exposes your table structure to consumers, so a contract layer is wise.

Either approach delivers at least once, which means duplicates will happen. Consumers need idempotent handling, usually by tracking an event identifier or by making updates naturally repeatable. Also plan for backfills: when a consumer is new or wrong, you must replay history without disturbing live traffic.

Map

Find one place in your systems where an application writes to a database and also sends a message or calls another service. Describe what happens at each possible failure point between the two steps, and mark which failure would go unnoticed the longest. Share the result with a colleague and note where their reading differs from yours.

Design

Design the event path for the order service. Show the transaction, the outbox or capture mechanism, the relay, and each consumer. State how each consumer detects duplicates, how ordering is preserved for one order, and how you would rebuild the search index from scratch while new orders keep arriving. Keep the output to a single page that a peer could review in ten minutes without your help.

Requirements

  • •Show the write path end to end
  • •Explain how duplicates are handled
  • •Describe how a missed day is backfilled

Constraints

  • •No distributed transactions
  • •Consumers owned by other teams

Must Cover

atomicityidempotent-consumersbackfill

Trade-off Axis

delivery guarantees vs operational simplicity

Competencies Advanced

data state → L3distribution communication → L3

Ready to Design?

Open this day in the Design Studio. The studio follows this track: its days, concepts, patterns and trade-offs are loaded, and you keep one design across the whole track.

Open in Studio →