What is the Outbox Pattern and how does CDC support it?

Topic: microservices, Data Management. Level: Hard.

Answer

The **dual-write problem**: updating the DB and publishing to a broker in two separate steps can leave them inconsistent if one fails. The **Transactional Outbox** solves it by writing the business row and an `outbox` event row in the *same local transaction*. A separate relay then publishes outbox rows to the broker. **Change Data Capture (CDC)** (e.g. Debezium tailing the DB transaction log) reads committed outbox inserts and streams them to Kafka — no polling, low latency, and the write is atomic. This gives at-least-once delivery, so consumers must be idempotent. ```java @Transactional public void placeOrder(Order o) { orderRepo.save(o); // business row outboxRepo.save(new OutboxEvent( // SAME transaction "Order", o.getId(), "OrderPlaced", toJson(o))); // No broker call here — CDC/Debezium tails the outbox table // and publishes committed rows to Kafka. } ```