Patterns
35 items
35 items
Capturing database changes as events
Change Data Capture (CDC) tracks and captures changes made to database records, publishing them as events for downstream consumption. Instead of polling databases or dual-writing, CDC reads the database's transaction log to capture every INSERT, UPDATE, and DELETE. This enables real-time data synchronization, event-driven architectures, cache invalidation, and building materialized views. Tools like Debezium, AWS DMS, and database-native features make CDC accessible. CDC is essential for keeping data consistent across systems without tight coupling.
Reading transaction logs (WAL, binlog) captures all changes exactly as they happened. No missed updates. No race conditions. Single source of truth.
Writing to database AND cache/queue creates inconsistency on failures. CDC writes to database only; changes flow automatically to other systems.
Database changes become events. Other services react to changes without polling. Loose coupling between services.
Why not dual-write?
python# DANGEROUS: Dual-write pattern def update_user(user_id, data): database.update(user_id, data) # Step 1 cache.invalidate(user_id) # Step 2 - fails? kafka.publish(user_changed) # Step 3 - fails? # If step 2 or 3 fails, systems are inconsistent!
CDC solves this: Write to database only. CDC captures change and publishes to all consumers.