---
title: "Chapter 04: Transactional Outbox & Change Data Capture (CDC) Architecture | TinyCTO Distributed Systems Canon"
description: "Eliminating the deadly dual-write anti-pattern: storing events atomically in local database transactions, tailing the Write-Ahead Log (WAL) via Debezium CDC, and streaming into event brokers without drift."
image: "https://tinycto.tv/assets/distributed-systems/distributed_systems_manuals_og.jpg"
canonicalUrl: "https://tinycto.tv/distributed-systems/manuals/04-transactional-outbox-cdc"
locale: "en"
---

# Chapter 04: Transactional Outbox & Change Data Capture (CDC) Architecture

> **Canonical Distributed Systems Engineering Field Manual**
> **Theorem Citation**: Chris Richardson (Microservices Patterns) / Debezium Specification | **Read Time**: 20 min read | **Maturity Target**: SCALED

Eliminating the deadly dual-write anti-pattern: storing events atomically in local database transactions, tailing the Write-Ahead Log (WAL) via Debezium CDC, and streaming into event brokers without drift.

# Transactional Outbox & Change Data Capture (CDC) Architecture

## Executive Summary
In microservice architectures, updating a local database and publishing a corresponding event to an event broker is an error-prone distributed operation. If the application crashes between the database commit and the broker produce call, data corruption occurs. The Transactional Outbox pattern combined with Change Data Capture (CDC) mathematically eliminates this "dual-write" failure mode without expensive two-phase commit (2PC) protocols.

## 1. The Dual-Write Vulnerability
Consider the naive implementation:
```
BEGIN TRANSACTION;
  UPDATE accounts SET balance = balance - 100 WHERE id = 42;
COMMIT;
kafkaProducer.send("AccountDebited", account); // FAILS IF BROKER UNREACHABLE OR PROCESS CRASHES
```
The database update succeeded, but the event was never published. Alternatively, if the message is sent *before* committing, a database rollback leaves an orphaned event in Kafka.

```mermaid
sequenceDiagram
    autonumber
    participant App as Order Service
    participant DB as PostgreSQL (ACID)
    participant WAL as Write-Ahead Log (WAL)
    participant Debezium as Debezium CDC Engine
    participant Kafka as Apache Kafka

    App->>DB: 1. BEGIN TRANSACTION
    App->>DB: 2. INSERT INTO orders (...)
    App->>DB: 3. INSERT INTO outbox_events (id, topic, payload)
    App->>DB: 4. COMMIT TRANSACTION (Atomic Local Commit)
    DB->>WAL: 5. Append Mutations to WAL
    Note over Debezium: Non-Blocking WAL Tailing<br/>via pgoutput plugin
    WAL->>Debezium: 6. Stream Logical Decoding Events
    Debezium->>Kafka: 7. Produce to Topic with Idempotent EOS (acks=all)
```

## 2. PostgreSQL Logical Decoding & Debezium Pipeline
1. **Atomic Local Outbox Insertion:** The outbox record is inserted in the exact same relational transaction as the business entity.
2. **PostgreSQL WAL Streaming:** The PostgreSQL \`pgoutput\` plugin decodes mutations from the Write-Ahead Log into JSON/Avro structures.
3. **Debezium Offset Tracking:** Debezium commits Kafka message offsets only after receiving broker acknowledgments, guaranteeing at-least-once delivery with zero data loss.
4. **Outbox Partition Routing:** The event payload defines the Kafka destination topic and partition key directly from relational columns.


### Core Concepts & Consistency Models

- `Dual-Write Elimination`
- `Transactional Outbox`
- `Change Data Capture (CDC)`
- `PostgreSQL WAL`
- `Debezium Engine`

### Canon Surfaces & Navigation

- **Manuals Library**: https://tinycto.tv/distributed-systems/manuals
- **18 Reference Architectures**: https://tinycto.tv/distributed-systems/architectures
- **Topology & Sizer Wizard**: https://tinycto.tv/distributed-systems/wizard
- **Technology & Consensus Matrix**: https://tinycto.tv/distributed-systems/matrix

```json
{
  "@context": "https://schema.org",
  "@type": "TechArticle",
  "headline": "Transactional Outbox & Change Data Capture (CDC) Architecture",
  "description": "Eliminating the deadly dual-write anti-pattern: storing events atomically in local database transactions, tailing the Write-Ahead Log (WAL) via Debezium CDC, and streaming into event brokers without drift.",
  "inLanguage": "en",
  "educationalLevel": "Advanced",
  "proficiencyLevel": "Expert",
  "url": "https://tinycto.tv/distributed-systems/manuals/04-transactional-outbox-cdc"
}
```
