CDC là kỹ thuật theo dõi và capture mọi thay đổi (INSERT, UPDATE, DELETE) trong database và stream những thay đổi đó ra các system khác gần như real-time – thay vì polling database định kỳ.
- Cách hoạt động phổ biến nhất: Log-based CDC đọc database transaction log (WAL trong PostgreSQL, binlog trong MySQL) – non-intrusive, không ảnh hưởng production write performance, capture tất cả changes kể cả DELETE.
- Debezium là open-source CDC platform phổ biến nhất, kết nối với PostgreSQL/MySQL/MongoDB và stream changes vào Kafka.
- Use cases: cache invalidation (khi DB thay đổi → invalidate Redis cache ngay lập tức); data synchronization (sync dữ liệu từ OLTP sang data warehouse real-time thay vì batch ETL hàng đêm); microservices event sourcing (DB change → event); Elasticsearch sync (full-text search index luôn up-to-date); audit logging.
- Lợi thế so với polling: lower latency (seconds thay vì minutes/hours), ít load hơn cho DB, không miss delete events.
- Outbox Pattern là cách đảm bảo CDC reliability: write changes vào outbox table trong cùng transaction, CDC đọc từ outbox.
CDC is the technique of tracking and capturing every change (INSERT, UPDATE, DELETE) in a database and streaming those changes to other systems in near real-time — instead of periodically polling the database.
- The most common approach: Log-based CDC reads the database transaction log (WAL in PostgreSQL, binlog in MySQL) — non-intrusive, does not impact production write performance, and captures all changes including DELETEs.
- Debezium is the most popular open-source CDC platform, connecting to PostgreSQL, MySQL, and MongoDB and streaming changes into Kafka.
- Use cases: cache invalidation (when the DB changes → immediately invalidate the Redis cache); data synchronization (sync data from OLTP to a data warehouse in real-time instead of nightly batch ETL); microservices event sourcing (DB change → event); Elasticsearch sync (keeping full-text search indexes up to date); audit logging.
- Advantages over polling: lower latency (seconds instead of minutes or hours), less load on the database, and no missed DELETE events.
- The Outbox Pattern ensures CDC reliability: write changes to an outbox table in the same transaction, and CDC reads from the outbox.