CDC, outbox & projections
Senior loops punish naive dual-write (update DB + fire-and-forget cache/search). This page is the standard fix vocabulary: CDC, transactional outbox, and projections.
Quick mental model
| Pattern | Idea | Failure mode you avoid |
|---|---|---|
| Dual-write (app) | Two writes from handler | Partial success—DB ok, cache/index not |
| Transactional outbox | Same DB txn writes business row + outbox row | At-least-once relay must be idempotent downstream |
| CDC | Log tail of DB changes (WAL/binlog) → bus/stream | Operational complexity; schema drift handling |
Transactional outbox (microservices default story)
- Begin transaction.
- Persist domain change +
outboxrow (topic,payload,schema_version). - Commit (atomic).
- Relay process publishes to broker (Kafka/SNS/SQS/Rabbit) and marks outbox sent or deletes with tombstone policy.
- Consumers process idempotently (natural key / dedupe table).
Node.js: relay can be separate worker process; use SELECT FOR UPDATE SKIP LOCKED class patterns for concurrent relays where supported.
CDC (change data capture)
Sources: Postgres logical replication, Debezium on Kafka Connect, DMS, RDS native feeds—vendor-specific.
Flow: WAL/binlog → capture → stream → warehouse, Elasticsearch, analytics.
Pros: No second write in request path; fewer partial states.
Cons: lag, ordering per partition, schema evolution, ops burden.
┌────────┐ WAL / binlog ┌──────────┐ ┌─────────────┐
│ OLTP │ ─────────────────► │ CDC │ ──► │ Warehouse / │
│ (RDS) │ (ordered log) │ connector│ │ search proj.│
└────────┘ └──────────┘ └─────────────┘Projections & read models
Projection = materialized view for a read path (search doc, cache warm list, dashboard aggregates).
Rules staff state aloud:
- Rebuild path if projection drifts (replay from outbox or CDC).
- Version events; tolerate duplicate delivery.
- Backpressure consumers when downstream slow.
When to choose which
| Situation | Bias |
|---|---|
| Same DB, must publish with write | Outbox |
| Many downstream replicas, infra owns pipe | CDC |
| Low volume, simple invalidation | Synchronous delete cache key + accept latency (still watch races) |
Diagram — compare three integration shapes
See also
Last updated on
Spotted something unclear or wrong on this page?