Simple definition#
- Transactional outbox: write the business change and the event to the same database in one transaction, then publish the event from the database to the broker.
- Change data capture (CDC): read a database’s transaction log and stream every change as events.
- Idempotent consumer: a consumer that gives the same result whether a message arrives once or several times.
Together they give effectively-once processing without distributed transactions.
The problem#
A service saves an order and then publishes OrderPlaced. If it crashes between the two, the order exists but no one hears about it. If it publishes first and the commit fails, everyone hears about an order that does not exist. Retries make it worse: brokers and networks deliver at least once, so duplicates will happen.
The transactional outbox#
BEGIN;
INSERT INTO orders (id, customer_id, total, status) VALUES ('ORD-42', 'C-7', 125.00, 'PLACED');
INSERT INTO outbox (id, aggregate_type, aggregate_id, type, payload, created_at)
VALUES ('8f3c…', 'order', 'ORD-42', 'OrderPlaced', '{"orderId":"ORD-42",...}', now());
COMMIT;
Both rows commit or neither does. A relay then publishes outbox rows to Kafka.
Two ways to relay#
| Polling publisher | Log-based CDC (Debezium) | |
|---|---|---|
| How | A job selects unpublished rows, publishes them, then marks or deletes them | Reads the DB log (PostgreSQL WAL, MySQL binlog, Oracle LogMiner or XStream, SQL Server CDC) |
| Latency | Poll interval | Sub-second |
| DB load | Queries on the outbox table | Minimal (reads the log) |
| Ordering | Careful with concurrent pollers | Commit order preserved |
| Ops | Simple | Run Kafka Connect + Debezium; manage replication slots |
Debezium (Debezium 3.x) has an Outbox Event Router transformation. It routes each outbox row to a topic based on aggregate_type, uses aggregate_id as the Kafka key and puts the payload in the record value. The outbox table can be cleaned immediately or by retention.
CDC beyond the outbox#
CDC also streams every table change for replication, cache invalidation, search indexing and lakehouse ingestion. Raw table CDC exposes your internal schema as a contract, so prefer the outbox for integration events and raw CDC for data replication into platforms you own.
Idempotent consumers#
Delivery is at least once, so consumers must tolerate duplicates.
| Strategy | How | Good for |
|---|---|---|
| Processed-message table (inbox) | Insert event_id into a table with a unique key in the same transaction as the business change; skip on conflict |
General purpose |
| Natural idempotency | Upsert by business key; “set status = SHIPPED” not “toggle” | State updates |
| Version check | Apply only if event.version > current.version |
Out-of-order delivery too |
| Conditional write | UPDATE ... WHERE status = 'PENDING' |
State machines |
-- Inbox / processed-message table, same transaction as the business change
INSERT INTO processed_events (consumer, event_id, processed_at)
VALUES ('ledger-posting', :eventId, now())
ON CONFLICT DO NOTHING; -- 0 rows inserted = duplicate, so skip the business logic
Keep processed IDs at least as long as the redelivery window (the topic retention or replay horizon).
Idempotency keys for APIs#
Clients also retry, after timeouts or double-clicks. For non-idempotent operations such as POST /payments:
flowchart TB
R["POST /payments<br/>Idempotency-Key: K"] --> Q{"Key K seen?"}
Q -->|"no"| S["store K + request hash, status IN_PROGRESS"] --> P["process"] --> SAVE["store response for K"] --> OK["return response"]
Q -->|"yes, completed, same hash"| RET["return stored response"]
Q -->|"yes, in progress"| CONF["409 / retry later"]
Q -->|"yes, different payload"| ERR["422 key reuse error"]
The IETF Idempotency-Key HTTP header draft standardises this behaviour. Payment APIs such as Stripe and Adyen, and open-banking APIs, already use it.
Ordering#
- Key events by aggregate ID, so all events for one order share a partition and stay in order.
- Include a per-aggregate sequence or version in each event. Consumers ignore stale or duplicate versions and can detect gaps.
- Do not depend on order across aggregates.
Common mistakes#
Quick check#
How does the outbox pattern solve the dual-write problem? The state change and the event are written in one local DB transaction. A separate relay publishes the event, so neither can exist without the other.
Polling publisher vs CDC? Polling is simple but adds DB load and latency. CDC reads the transaction log with low latency and preserves commit order, but needs Debezium and Connect operations.
What makes a consumer idempotent? Recording the processed event ID in the same transaction as the business change, or applying naturally idempotent, versioned updates.
What should an API do when an Idempotency-Key is reused with a different payload? Reject the request (for example with HTTP 422), because the key identifies one specific request.
How do you keep per-order event ordering? Use the order ID as the Kafka key and include a version per aggregate.
Summary#
- Outbox: state and event in one transaction; a relay publishes. CDC with Debezium is the scalable relay.
- Consumers are at-least-once, so make them idempotent in the same transaction.
- Use idempotency keys on APIs; key events by aggregate; version them.
References#
- Chris Richardson, Pattern: Transactional outbox.
- Debezium, Outbox Event Router.
- IETF, The Idempotency-Key HTTP Header Field (draft).
- Spring, Spring Modulith event publication registry.