Captura de Cambios de Datos
CDC · CDC basado en log
Escrito por Darjus Kazakevic · revisado el
Turn every committed change in a database into an event, in commit order, without touching the app.
Data rarely stays in one store. The same orders feed a search index, a cache, a warehouse and other services. Writing to each one from the app is a dual write: a crash or a failed call between two writes leaves them out of step for good, and nothing notices. Change data capture (CDC) avoids it by treating the database as the only place the app writes.
Every database keeps a log of committed changes for its own recovery and replication: the WAL in Postgres, the binlog in MySQL, the oplog in MongoDB. A CDC connector, such as Debezium, reads that log like a replica would and publishes each row change as an event, usually to a topic in a log like Kafka. Consumers rebuild whatever they need from those events.
En esencia — The database already writes every change to a log. CDC reads that log and publishes it, so other systems follow the database without the app writing to them twice.
Cuándo recurrir a él
Recurre a él cuando
- Keeping a search index, cache or warehouse in step with a database
- Publishing events reliably, together with an outbox table
- Moving data to a new store while the old one stays live
Reconsidéralo cuando
- One consumer that can simply query the database on demand
- Sharing raw tables with other teams as a public contract
- A managed database that does not expose its change log
Conceptos clave
- Log-based CDC
- The connector reads the database's replication log and turns each committed row change into an event with the old and new values. It sees every change, including deletes, in commit order, and adds almost no load to the write path.
- Trigger and polling CDC
- Without access to the log, a trigger copies each change into a changes table, or a job polls for rows where updated_at is newer than the last run. Both work on any database, and both have gaps.
- Snapshot, then stream
- The log only holds recent changes, so a new consumer first needs the existing rows. The connector reads a consistent snapshot, notes the log position it matches, then streams from that position. Newer tools snapshot in chunks while streaming, so a large table does not hold the log back. A change can show up in both the snapshot and the stream, which idempotent consumers absorb.
- Ordering per key
- Changes to one row must arrive in order, or an old update lands after a new one. Connectors partition events by primary key, which keeps each row in order but gives no order across rows or partitions.
- Change event
- One row change: the operation, the row before and after, the source table and the log position. A delete often adds a tombstone, an event with a key and no value, so compacted topics can forget the row.
Reglas de diseño
- Publish an outbox, not your tables, to other teams
- Raw row changes expose your internal schema, so every column rename breaks a consumer. For events other services use, write an explicit event to an outbox table in the same transaction and let CDC publish that table. Keep raw table CDC for your own caches and indexes.
- Make every consumer idempotent
- Connectors deliver at least once: after a restart they replay from the last confirmed position. Apply changes as upserts keyed by primary key, and skip an event whose log position is older than the one already applied.
- Watch the slot, not only the connector
- The database keeps log segments until the consumer confirms them. A stopped connector means the log grows until the disk is full. Alert on retained log size, and cap it where the database allows.
- Plan schema changes with the consumers
- A dropped or renamed column changes every event after it. Use expand/contract on captured tables, and carry the schema with each event through a registry so consumers can tell versions apart.
Errores habituales
- Leaving an unused replication slot behind, so the database keeps WAL until the disk fills and writes stop.
- Assuming events arrive in one global order. Only changes to the same key are ordered.
- Expecting a transaction to arrive as one unit. A multi-row commit becomes several events that consumers see one by one.
- Losing old values on update and delete because the table only logs its primary key.