Aller au contenu

Leçon 16 · Loading Data

Incremental Loads and Change Data Capture

Load only what changed: watermarks with lookback windows, upserts, deletes, change data capture, and applying a change feed safely in runnable SQL.

Reloading every table from the beginning works while data is small. Later it becomes slow and expensive. An incremental load processes only what is new or changed. It saves work, but you take on new ways to be wrong: you can miss rows, apply them twice or apply them in the wrong order.

Ways to find changes

Method How it works Watch out for
Increasing ID Take rows with an ID above the last one Misses updates to old rows
Last-modified timestamp Take rows changed since the last run Needs a reliable column; hard deletes are invisible
Snapshot comparison Compare today's full copy with yesterday's Costly for big tables
Log-based CDC Read the database's change log More setup, permissions and operations
Triggers The source writes changes to a table Adds work to source transactions

Watermarks and lookback

A watermark (or high-water mark) records how far you have processed, for example the latest updated_at seen. The next run asks for newer rows.

A strict "greater than the watermark" rule has a trap. A transaction can start before your extract but commit after it, carrying an earlier timestamp. Rows with the same timestamp can also arrive in two batches. A common remedy is a lookback window: re-read a little before the watermark and load with an upsert (update if the key exists, otherwise insert), so repeated rows do no harm.

SQL Watermark, lookback and upsert

Press Run (or Ctrl + Enter) to see the result here.

The strict rule finds only orders 2 and 4. The lookback also catches order 3. The final WHERE keeps an older version from overwriting a newer one.

A lookback only protects against delays shorter than the window. Choose it from how your source behaves, and reconcile with the source from time to time. Also save the new watermark only after the data is committed, ideally in the same transaction. If you save it first and the load fails, those rows are skipped.

Deletes

A row that is physically deleted from the source does not show up in a "changed since" query. Options include:

  • The source uses soft deletes (a flag and timestamp).
  • CDC from the log emits delete events.
  • You periodically compare the sets of keys.

Decide what the warehouse does with a delete: mark the row inactive, close its history, or really remove it, for example to meet an erasure request.

Change data capture

CDC produces a stream of change events, typically with the operation (insert, update or delete), the key, the new values and a position in the source log. Debezium is a well-known open-source CDC project.

Delivery is often at least once, so you may see the same event twice. Apply events idempotently, using the log position to order them for each key.

SQL Apply a change feed with duplicates and deletes

Press Run (or Ctrl + Enter) to see the result here.

Product 2 ends as deleted although the update arrived last, because position 105 is later than 104. Because deleted rows keep their position, a replayed older event cannot bring them back.

This gives the current state. To keep full history, process every change in order into a Type 2 dimension instead of keeping only the latest.

Full refresh still matters

Keep a way to rebuild a table completely. You need it after logic changes, after missed data, or when incremental state is corrupted. Check that the source or staging layer still holds enough history to do it.

Choosing a method

Start from the source, not from the technique you like best.

  • Small table? A full extract is often the best choice. It is easy to understand and it notices deletes.
  • Append-only events? An increasing ID or ingestion position may be enough. Confirm that rows really are never updated, and that IDs become visible in order. IDs assigned by concurrent transactions can appear out of order.
  • Rows that are updated, with a trustworthy timestamp? Use a watermark with a lookback, plus a plan for deletes. Ask whether bulk fixes made directly in the database also update the timestamp.
  • Low latency, or every change needed? Consider log-based CDC. Someone must then operate it, watch its lag and handle schema changes.

The first load and the handover

An incremental pipeline needs a starting point. The usual approach takes an initial full snapshot and then applies changes from a known position. The danger is the gap between the two. If the change stream starts after the snapshot was read, changes made in between are lost. Capture a position first, take the snapshot, then apply changes from that position. Some changes are then applied twice, which is harmless when applying is idempotent. CDC tools often have features for this; read how yours does it.

Monitor the lag

Watch how far the warehouse is behind the source, both in time and, with CDC, in unprocessed events. Also watch for silence. A feed that delivers nothing may mean a quiet night or a broken connection. A heartbeat, such as a small regular change in the source, helps to tell them apart. Check, too, how long the source keeps its change log. If your consumer is stopped for longer than that, the missing changes cannot be read, and you may need a new snapshot.

Common mistakes

Do not take the watermark from the pipeline server's clock and compare it with source timestamps. Clocks and time zones differ. Base it on values from the source, or on a source log position.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why add a lookback window to a timestamp watermark?

    Rereading a short overlap catches slightly delayed commits, within the window's size.
  2. 2. Which change is invisible to a simple changed-since query?

    A row that no longer exists cannot be returned by the query.
  3. 3. Events may be delivered more than once. What should applying them be?

    Replaying the same event should leave the same result.
  4. 4. When should the new watermark be saved?

    Saving it early risks skipping rows after a failure.

Summary

  • Incremental loading saves work but needs a reliable way to detect changes.
  • Pair watermarks with lookback windows, upserts and periodic reconciliation.
  • Handle deletes explicitly.
  • Apply CDC events idempotently and in log order, and keep a rebuild path.

Next: Loading Fact Tables

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.