Rana Maninder Singh
Lesson 8 of 12Real-Time Data Engineering

Change Data Capture (CDC)

Lesson opener

In one line: CDC reads the database log of committed changes. It does not replace the database.

Why it matters: Northline's warehouse database commits a pick of 2 for FX-100. A polling job that reads the table every minute can miss a change that was applied and then overwritten, or it can double-count if it subtracts the new balance from the old balance incorrectly. The log has the change itself. Figures are illustrative.

Before this lesson: Lesson 7. You can reject a bad event and skip a repeated id.

What you will be able to do

Prerequisites

Debezium's architecture is the connector model (https://debezium.io/documentation/reference/stable/architecture.html). The MySQL connector reads the binary log (https://debezium.io/documentation/reference/stable/connectors/mysql.html). This lesson does not run a connector. It does not claim the warehouse stops being the system of record.

The log, then the topic

Plain English: A committed change is already in the database log. Change data capture reads that log. For MySQL, the connector documented above uses the binary log. Replication here means the connector publishes those changes into Kafka. It is not a second writable copy of the warehouse.

A snapshot is the initial consistent read of the table, used so the topic is not empty before the log tail starts. After the snapshot, the connector streams new commits. Do not run a second snapshot because a consumer was slow. You will republish history.

Transactional consistency in this lesson means: apply changes for one primary key in log order, and do not apply a change you have already applied. Cross-partition order is not guaranteed, so the Kafka key is the primary key. Then one row's updates stay in one partition while the partition count is unchanged.

A replay can deliver the same change again. Store the source log position (the file and position, or the equivalent the connector puts on the event). If you already applied that position for that key, skip it. Do not add the quantity a second time. Do not describe the whole path through Spark and Power BI as one database transaction. It is not.

Teacher walkthrough: one row, one position

All figures are illustrative.

The table stock has primary key sku. The log says FX-100 changed from 80 to 78 at position binlog:100.

  1. Publish a Kafka record keyed by FX-100.
  2. The value includes the new quantity and binlog:100.
  3. The apply step writes 78 only if binlog:100 is new for that SKU.
  4. Validation: a second delivery of binlog:100 stays at 78. A later position binlog:140 with quantity 77 advances.
def apply_change(state, change):
    current = state.get(change["sku"], {"qty": None, "position": None})
    if current["position"] == change["position"]:
        return state
    state[change["sku"]] = {"qty": change["qty"], "position": change["position"]}
    return state

state = {}
change = {"sku": "FX-100", "qty": 78, "position": "binlog:100"}
apply_change(state, change)
apply_change(state, change)
assert state["FX-100"]["qty"] == 78
apply_change(state, {"sku": "FX-100", "qty": 77, "position": "binlog:140"})
assert state["FX-100"]["qty"] == 77

A second workplace case

The snapshot says FX-100 is 80 at position snap:1. The tail's first change is binlog:100 to 78. You apply the snapshot first, then the tail. You do not subtract 78 from 80. The change event already carries the new quantity.

def after_snapshot_and_tail(snapshot_qty, tail_qty):
    return tail_qty

assert after_snapshot_and_tail(80, 78) == 78

Failure example: the poll subtracted a balance from itself

Symptom: A minute job read 78, remembered 80, and published a delta of 2, then did it again on the next poll because it compared with a stale 80.

Diagnosis: The job invented a delta from two table reads. It did not read the log. The root cause is polling balances instead of applying log positions.

Correction: Consume the connector's change, which already says the new quantity, and skip a source position you have applied. Keep the warehouse as the system of record.

Validation: Two deliveries of binlog:100 leave quantity 78, not 76.

def apply_qty(applied, position, qty):
    if position in applied:
        return applied[position]
    applied[position] = qty
    return qty

applied = {}
assert apply_qty(applied, "binlog:100", 78) == 78
assert apply_qty(applied, "binlog:100", 78) == 78

Guided practice: key and order

Scenario: Two changes for FX-100, positions binlog:100 (qty 78) and binlog:140 (qty 77), and one change for FX-200. Figures are illustrative.

Hints:

Complete solution:

The key is the SKU. Both FX-100 changes share a key and therefore one partition while the count is stable, so their order in that partition holds. FX-200 is independent. Store binlog:100 after you write 78.

def kafka_key(change):
    return change["sku"]

assert kafka_key({"sku": "FX-100"}) == kafka_key({"sku": "FX-100", "position": "binlog:140"})
assert kafka_key({"sku": "FX-200"}) != "FX-100"

Independent practice

Pick one table you would capture.

Acceptance criteria:

Common mistakes

  • Polling the table and inventing deltas.
  • Using a null Kafka key, so one row's updates split across partitions.
  • Starting another snapshot to "catch up."
  • Adding quantities from change events that already contain the new balance.

Quick check

  1. What does the MySQL connector in this lesson read?
  2. What is the snapshot for?
  3. What is the Kafka key?
  4. Why must a repeated binlog:100 not change 78?
  5. Does CDC make Kafka the system of record?

Answers:

  1. The binary log of committed changes.
  2. A starting copy before the tail.
  3. The primary key, here the SKU.
  4. That position was already applied.
  5. No. The warehouse database remains the system of record.

Quiz alignment

The quiz asks you to keep log order per key, to skip a repeated source position, and to refuse a second snapshot as a catch-up.

Lesson summary

CDC publishes committed log changes. Snapshot, then tail. Key by the primary key. Apply a source position once. The database still owns the row.

Bridge to the next lesson

The next lesson is what a person watches: a live KPI that reads a serving table, and the age you allow before the number is stale. Bring the serving quantity and its source position.