# Change Data Capture (CDC)

[Based on the article](https://medium.com/event-driven-utopia/a-gentle-introduction-to-event-driven-change-data-capture-683297625f9b)

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FgR6bgtOWFltZSoqAj3aW%2FScreenshot%202022-09-23%20at%2010.06.11.png?alt=media&#x26;token=17682eac-e4db-4ba5-84b1-fa21fe5d08d3" alt=""><figcaption></figcaption></figure>

Modern applications need to keep their data in multiple places, in a redundant and denormalised manner.

* One datastorage in the system has to be a source of truth (aka **System of Records**)
* Other storages will accept these data in a transformed way (**Derived data**)
* All data in all these storages has to be in sync

The CDC process has three stages.

## Change detection

There are few options to detect the changes done to the source database.

1. Polling the LAST\_UPDATED column of tables periodically to detect changes.
2. Database triggers to capture row-level operations.
3. **Watch the database transaction log for changes**.

Polling is slow; it puts more load on the source database. Also, the implementation requires you to add dedicated columns to the source. Triggers deliver the results in real-time. But they are resource-intensive and can drag the database.

Watching the transaction log of the database is fast and does not impose a performance impact. Many CDC systems today have adopted this approach for their implementations.

### How does a log-based CDC system work? <a href="#id-04e4" id="id-04e4"></a>

Users and applications make changes to the source database in terms of inserts, updates, and deletes. The database records these changes in the transaction log in the order of their occurrence.

CDC system watches the transaction log for any changes and propagates them into the target system while preserving the change order. The target system then replays the changes to update its internal state.

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FPjpx4wUHopx3u5LWf6pP%2FScreenshot%202022-09-23%20at%2010.16.15.png?alt=media&#x26;token=a0380b11-bc9e-4d18-8ef9-81a3c4bb7774" alt=""><figcaption></figcaption></figure>



### Requirements for a production-grade CDC system <a href="#id-5690" id="id-5690"></a>

A production-grade CDC system should satisfy the following needs.

* **Message ordering guarantee** — The order of changes MUST BE preserved so that they are propagated to the target systems as is.
* **Pub/sub** — Should support asynchronous, pub/sub style change propagation to consumers.
* **Reliable and resilient delivery** — At-leat-once delivery of changes. Cannot tolerate a message loss.
* **Message transformation support** — Should support light-weight message transformations as the event payload need to match with the target system’s input format.

[Event-Driven Architecture (EDA)](https://en.wikipedia.org/wiki/Event-driven_architecture) seems an ideal fit to achieve the above system requirements. EDA brings to the table the asynchrony, loose coupling, and scalability. By combing the EDA features, we can rethink the CDC architecture as follows.

### Make it event driven

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FwmdtLdugARfefPyAavm0%2FScreenshot%202022-09-23%20at%2010.18.02.png?alt=media&#x26;token=5f556a44-3b66-4bc6-a9d9-c8052a0396b2" alt=""><figcaption></figcaption></figure>

The transaction log mining component captures the changes from the source database. It converts them into events and publishes them to the message bus. That happens in real-time while changes are made to the source database.

## Change capture

Change events are then written to the message bus. It provides highly scalable, and reliable change event storage while preserving the order of received events. Also, depending on the implementation, the message bus can provide at-least-once or exactly-once delivery guarantees.

Change events are usually written to a topic so that any interested consumers can subscribe to receive updates.

A message broker like RabbitMQ or ActiveMQ can provide transient event storage while a streaming platform such as Kafka or Kinesis provides a durable event streaming capability. Choosing either of them should be use case driven. You can refer to my [earlier post](https://medium.com/event-driven-utopia/comparing-enterprise-messaging-and-event-streaming-e714f7b5fc40) about their difference.

## Change propagation

Any downstream application can subscribe to the above topic to receive change events. Usually, an intermediate component consumes the events, applies a light-weight transformation to the event payload, and publishes it to the target system. For example, a connector reads events from the topic, applies transformation, and updates a search index.

Depending on the use case, consumers can do event-driven consumption or streaming consumption.

## Advantages <a href="#ee43" id="ee43"></a>

The event-driven CDC approach adds the following benefits over traditional ETL and polling-based solutions.

* Changes are detected, captured, and propagated in **real-time** as they happen. That enables downstream consumers to act upon changes quickly. Compared to traditional batch-oriented systems, that is a huge gain.
* The **loosely coupled** nature allows adding or removing components to the architecture with minimum impact. Source and target systems can be upgraded or replaced without affecting each other.
* The message bus in the middle provides **reliable delivery of change events**. Also, it can buffer incoming events if the rate of event production is higher than consumption. That will be beneficial for slow consumers.
* Unlike polling and trigger-based methods, **no performance impact on the source system**.

## Use cases <a href="#b4a4" id="b4a4"></a>

* Cache invalidation
* Search index building
* Database migration (version upgrade, migrate to a different vendor, on-premise to the cloud)
* Offline analytical processing
* Data synchronisation in Microservices

## Tools in the market <a href="#id-0214" id="id-0214"></a>

There are both open source and commercial tools available in the CDC market. Many open-source tools are flexible enough to co-exist with popular messing systems and target systems whereas commercial tools sometimes ask you to buy their entire platform.

Mentioned below are some renowned open-source tools in the market.

### **Debezium** <a href="#id-9574" id="id-9574"></a>

[Debezium](https://debezium.io/) is an open-source CDC platform built on top of Apache Kafka. Debezium has connectors to pull a change stream from databases like PostgreSQL, MySQL, MongoDB, Cassandra, and send that to Kafka. Kafka Connect is used as connectors for change detection and propagation.

Even though Debezium utilises Kafka in its architecture, it offers other deployment options to cater to different infrastructure needs. Debezium can be used as a standalone server (with Debezium server), or you can embed it into your application code as a library. Visit [here](https://debezium.io/documentation/reference/architecture.html#_debezium_server) for more information on that.

### **Maxwell** <a href="#id-8ef8" id="id-8ef8"></a>

[Maxwell](https://maxwells-daemon.io/) reads MySQL binlogs and writes row updates as JSON to Kafka, Kinesis, or other streaming platforms. Maxwell has low operational overhead, requiring nothing but MySQL and a place to write.
