Skip to main content
← PROBLEMS / ARCHITECTURE
An end-to-end domain learning journey

Scaling the Transactional
Outbox with CDC.

Under 15,000+ writes/sec, polling a PostgreSQL transactional outbox table with SELECT ... FOR UPDATE SKIP LOCKED and DELETE/UPDATE generates massive MVCC dead tuples, starves autovacuum, bloats B-tree indexes, and stalls event relay.
Start the journey5 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

If latency budgets leave no room for synchronous retries, replace SQL polling on high-throughput outbox tables with PostgreSQL WAL Logical Replication (Debezium `pgoutput`), or pair time-partitioned outbox tables with `DROP`/`TRUNCATE` rotation to eliminate MVCC bloat.

TL;DR30-second simplified summary
▾

Core Mechanics: Writing payment ledger rows and outbox_events in one ACID transaction eliminates dual-write bugs. However, when 8 relay workers poll outbox_events every 50 ms using `SELECT ...

Visual & Structural Flow: Depicts FOR UPDATE SKIP LOCKED Polling vs. Debezium pgoutput (Ledger Tx (15k/s) -> outbox_events Heap -> 8x SQL Pollers -> WAL + Debezium); Append-Only Partitioned Outbox + Sequential WAL CDC (Payment Service (20k/s) -> Postgres WAL (pgoutput) -> Partitioned outbox_events -> Debezium -> Kafka (<50ms)).

Trade-Offs & Production: Aggressive autovacuum_vacuum_cost_limit cannot reclaim dead tuples pinned by a 15-minute read-replica or analytics xmin snapshot. Sustain 20,000 outbox inserts/sec with < 50 ms p99 Kafka relay lag: Append-Only outbox_events (Zero UPDATE/DELETE): Insert events in the same ACID transaction as the ledger, and never update or delete rows.

Failure Modes & Application: The CDC-backed Transactional Outbox is the gold standard for atomic DB-to-Kafka publishing: Payment Ledgers & Order State Machines: Guarantees every committed balance change emits an ordered domain event.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
Transactional Outbox Pattern
The Transactional Outbox pattern eliminates dual-write split-brain by inserting domain events into an outbox table inside the exact same ACID database transaction as the business state change, then relaying them asynchronously to Kafka/RabbitMQ.
Full walkthrough · 5 stages
01
STAGE 01 · WHY IT MATTERSL4 · FUNDAMENTALS

Why SQL polling breaks PostgreSQL MVCC at 15,000+ outbox writes/sec.

Why this matters: The mechanism: understand how Why SQL polling breaks PostgreSQL MVCC at 15,000+ outbox writes/sec works from first principles.

Writing payment ledger rows and outbox_events in one ACID transaction eliminates dual-write bugs.

However, when 8 relay workers poll outbox_events every 50 ms using `SELECT ...

02
STAGE 02 · IMPACTL5 · APPLIED DESIGN

Dead-tuple explosion, autovacuum starvation, and bloated B-trees.

Why this matters: The problem: see what breaks without Dead-tuple explosion, autovacuum starvation, and bloated B-trees under real concurrency or scale.

PostgreSQL MVCC never overwrites rows in place—every UPDATE or DELETE creates dead tuples:

  1. 2x Dead-Tuple Churn: 15k/s inserts + 15k/s updates/deletes generate 30k tuple versions/sec that cannot be vacuumed while any analytics xmin snapshot is open (15 min).
  2. Index & Lock Thrashing: The status = 'PENDING' B-tree bloats to gigabytes of dead entries, blowing past 40,000 IOPS.
SQL Polling Collapse vs. WAL CDC

FOR UPDATE SKIP LOCKED Polling vs. Debezium pgoutput

Ledger Tx (15k/s)outbox_events Heap8x SQL PollersWAL + Debezium1. INSERT outbox + ledger (ACID)2. Naive: SKIP LOCKED + UPDATE/DEL3. Dead tuples + B-tree bloat (>40k IOPS)4. Append-only INSERT -> seq WAL5. Debezium pgoutput (<50ms p99)

Replacing SQL polling with WAL-based CDC (Debezium pgoutput) eliminates UPDATE/DELETE dead tuples and polling IOPS.

Engineering Decision Framework
Problem
How to implement Scaling the Transactional Outbox with CDC without hitting unbounded tail latency, state divergence, or operational fragility.
Naive Solution
Use a single unpartitioned path with unbounded retries and synchronous global coordination.
Why It Fails
Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
Alternatives
Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
Engineering Decision
If latency budgets leave no room for synchronous retries, replace SQL polling on high-throughput outbox tables with PostgreSQL WAL Logical Replication (Debezium pgoutput), or pair time-partitioned outbox tables with DROP/TRUNCATE rotation to eliminate MVCC bloat.
Trade-Offs
Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
Decision Exercise · Make the Call

When Scaling the Transactional Outbox with CDC experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?

03
STAGE 03 · NAIVE SOLUTIONL5 · APPLIED DESIGN

Why tuning autovacuum or adding partial indexes is not enough.

Why this matters: The problem: see what breaks without Why tuning autovacuum or adding partial indexes is not enough under real concurrency or scale.

Think of Why tuning autovacuum or adding partial indexes is not enough as the intuition layer for Scaling the Transactional Outbox with CDC: The problem: see what breaks without Why tuning autovacuum or adding partial indexes is not enough under real concurrency or scale.

Aggressive autovacuum_vacuum_cost_limit cannot reclaim dead tuples pinned by a 15-minute read-replica or analytics xmin snapshot. Meanwhile, polling every 50 ms still wastes thousands of random heap/index IOPS competing directly with payment transactions.

Lifecycle & Failure Recovery

Why tuning autovacuum or adding partial indexes is not enough

poll batchsink successpoison / timeoutAppended to LogLeased to ConsumerOffset CommittedMoved to DLQ
04
STAGE 04 · ENTERPRISE SOLUTIONL6 · SCALE & TRADE-OFFS

Enterprise solution: Append-Only Partitioned Outbox + Debezium pgoutput.

Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.

Sustain 20,000 outbox inserts/sec with < 50 ms p99 Kafka relay lag:

  • Append-Only outbox_events (Zero UPDATE/DELETE): Insert events in the same ACID transaction as the ledger, and never update or delete rows.
  • Debezium pgoutput WAL Streaming: Read sequential Write-Ahead Log records directly from the replication slot (zero polling queries, zero lock contention), using OutboxEventRouter SMT to partition Kafka by aggregate_id for per-aggregate ordering.
  • Hourly Range Partitions + DROP TABLE: Partition outbox_events by created_at (hourly/daily) and drop expired partitions in O(1) metadata time—bypassing VACUUM completely.
Zero-Bloat Outbox Architecture

Append-Only Partitioned Outbox + Sequential WAL CDC

Payment Service (20k/s)ACID ledger + outbox INSERTPostgres WAL (pgoutput)Sequential log; zero pollingPartitioned…Hourly partitions; DROP…Debezium -> Kafka…Keyed by aggregate_id

1.Zero Dead Tuples: No UPDATE/DELETE means zero MVCC bloat even with 15-min analytics xmin snapshots.

2.Sequential WAL Read: Debezium tails the WAL sequentially instead of random B-tree index scans.

3.DROP Partition Rotation: Hourly range partitions are dropped instantly without triggering VACUUM.

Append-only partitioned outbox tables paired with Debezium WAL CDC eliminate MVCC bloat and sustain 20k+ events/sec.

05
STAGE 05 · SYSTEM DESIGN APPLICATIONSL7 · STAFF / PRINCIPAL

Where CDC-backed outboxes power event-driven platforms.

Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

The CDC-backed Transactional Outbox is the gold standard for atomic DB-to-Kafka publishing:

  • Payment Ledgers & Order State Machines: Guarantees every committed balance change emits an ordered domain event.
  • Saga Orchestrators: Atomically persists saga state transitions and outgoing command events.
  • Microservice Event Buses: Decouples OLTP throughput from downstream consumer lag.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why does an append-only partitioned `outbox_events` table paired with Debezium `pgoutput` outperform `SELECT ... FOR UPDATE SKIP LOCKED` polling at 15,000+ writes/sec?

Recommended Learning Flow