Theory · Page 22 of 34 · Platforms and data in motion

22. Integration patterns: batch, CDC, events, APIs and sharing

Data moves between systems in five main ways: in scheduled batches (ETL or ELT), as captured changes read from a database's log (CDC), as events on a stream, on request through an API, or not at all, shared in place. Each has its own delay, cost and coupling, and the choice depends on how fresh the data must be and who should carry the burden.

One change delivered five ways, with its delay and its cost
Playback speed

The clip follows one change: a customer's new email address in Harbour Mutual's CRM. Delivered by a nightly batch, it reaches the warehouse the next morning. Read from the CRM database's log by change data capture, it arrives within seconds, without touching the application. Published as an event, every subscriber hears of it at once. Asked for through an API, it is always current, but only for the caller who asks, when it asks. Shared in place, the reader queries the producer's own table and nothing is copied. Each delivery is shown with its delay, its cost and how tightly it ties the two sides together.

The intuition: post, newsletter, phone call or a shared folder

A customer gives Harbour Mutual a new email address. The claims system, the warehouse, the marketing platform and Kestrel's portal all need to know. There are only a few ways to tell them. Collect the day's changes and send them in one parcel tonight. Watch the CRM's own record of changes and pass each one on as it happens. Announce it once, on a channel everyone who cares listens to. Let anyone who needs the address phone the CRM and ask. Or give them a window onto the CRM's own table.

Each is right somewhere. The warehouse is happy with tonight's parcel; the fraud check on a claim submitted an hour later is not. Phoning the CRM is always current, and if every system phones for every claim, the CRM falls over. The choice is a trade between how fresh the data must be, what it costs, and how much each side depends on the other.

Remember Choose how data moves by how fresh it must be, what it may cost, and how tightly producer and consumer may be tied.

The theory: five patterns and their trade-offs

Batch integration moves data on a schedule, in bulk, either transformed on the way (ETL) or loaded raw and transformed inside the target (ELT), which keeps the raw data for replay; pages 24 and 25 work both through by hand. Batches are simple, efficient and late by design: the delay is the schedule. Extracting by query ("rows changed since yesterday") also needs a reliable change timestamp, and it misses deletes.

Change data capture (CDC) turns a database's changes into a stream. Log-based CDC reads the database's own write-ahead or transaction log (PostgreSQL's logical decoding, SQL Server's change data capture, MySQL's binlog), where every committed insert, update and delete is already recorded in order, with its position, the log sequence number (LSN). It adds almost no load to the source, sees deletes, needs no change to the application, and delivers in seconds. The alternatives, triggers that copy each change into a table, or polling with timestamps, are heavier or blind to deletes.

A change stream is applied to a copy by compaction: for each key, only its latest change counts, in LSN order. An update replaces the row; a delete removes it; an insert adds it. Applying the same stream twice gives the same result if the apply is idempotent, which is what makes a replay safe.

A change log compacted to each key's latest change and applied to the customers table (PostgreSQL)
WITH change_log (lsn, op, customer_id, email) AS (
    VALUES (101, 'U', 1, 'first.change@example.com'),
           (102, 'D', 2, NULL),
           (103, 'U', 1, 'second.change@example.com'),
           (104, 'I', 9001, 'new.customer@example.com')
),
latest AS (
    SELECT DISTINCT ON (customer_id) customer_id, op, email
    FROM change_log
    ORDER BY customer_id, lsn DESC
)
SELECT
    (SELECT COUNT(*) FROM customers) AS rows_before,
    (SELECT COUNT(*) FROM customers c
     WHERE NOT EXISTS (SELECT 1 FROM latest l WHERE l.customer_id = c.customer_id AND l.op = 'D'))
    + (SELECT COUNT(*) FROM latest WHERE op = 'I') AS rows_after,
    (SELECT COUNT(*) FROM latest) AS changes_applied,
    (SELECT email FROM latest WHERE customer_id = 1) AS email_of_customer_1

The log holds 4 changes for 3 keys. Customer 1 changed twice: only the later email, second.change@example.com, survives. Customer 2 is deleted and customer 9001 inserted, so the table goes from 40 rows to 40.

Event streaming publishes facts as they happen ("customer 1 changed her email") to a durable, ordered log such as Kafka or Azure Event Hubs, which keeps them for a period and lets any number of consumers read at their own pace. The producer does not know its consumers: publish-subscribe decouples them. The difference from CDC is intent: CDC exposes a table's internal changes; an event is a business fact designed for others, with its own schema and contract (page 17). The two combine in the outbox pattern: the application writes its event to an outbox table in the same transaction as its change, and CDC publishes the outbox.

An API answers requests: the consumer asks for the current address when it needs it. It is always current and moves only what is asked for, but it couples the consumer to the producer's availability and speed, and it suits single records, not analytics over millions. Finally, sharing in place copies nothing: the producer grants the consumer read access to its own table (Delta Sharing, a warehouse's data sharing, a lakehouse catalogue's shared tables). It avoids pipelines and stale copies, at the price of depending on the producer's storage and its access control (page 16).

Patterns, compared Batch ETL/ELT: hours of delay, cheap per row, loosely coupled; for reporting and history. Log-based CDC: seconds, light on the source, coupled to the source's schema; for keeping copies in step. Events: seconds, a durable log in between, decoupled by a contract; for reacting to business facts. API: current on request, coupled at run time; for single records in an operation. Sharing: no copy, coupled to the producer's platform; for partners and other domains reading large tables. Hohpe and Woolf catalogued the messaging patterns behind several of these in 2003.

Why it matters

The merger multiplies the flows: every system of Kestrel's that stays alive must exchange data with Harbour Mutual's for months. Choosing CDC for the policy and claims tables, events for business facts such as a claim being reported, APIs for the claims portal's lookups and sharing for the actuaries' large extracts gives each flow the freshness it needs, at a cost someone has agreed to.

Every copy a pattern makes is a replica (page 21): it lags, it can drift, and it needs lineage (page 12) to be trusted.

Where it goes wrong

  • Polling for changes. "Updated since yesterday" misses deletes and rows whose timestamp was not set.
  • Events that are table rows. Publishing raw CDC as business events ties every consumer to the source's internal schema.
  • APIs for analytics. Calling an API per record to build a report overloads the producer and takes all night.
  • Non-idempotent applies. Replaying a change stream that appends instead of merging duplicates rows.
  • Copies nobody counts. Each integration makes a copy; without an inventory, nobody knows how many exist.

Check yourself

Choose an answer to each question, then press Check.

1. What does log-based change data capture read?
2. Which weakness of query-based extraction ("rows changed since yesterday") does log-based CDC avoid?
3. What does sharing data in place avoid?
Answer every question first.

Sources

The works this page draws on, summarised in its own words rather than quoted. Read them for the full argument.

  • G. Hohpe and B. Woolf, Enterprise Integration Patterns (Addison-Wesley, 2003)Messaging, publish-subscribe and the patterns of moving data between applications.
  • M. Kleppmann, Designing Data-Intensive Applications (O'Reilly, 2017), chapters 10 and 11Batch processing, change data capture and event streams, and logs as the link between them.
  • J. Kreps, "The Log: What every software engineer should know about real-time data's unifying abstraction" (2013)The ordered log as the common structure of databases, replication and stream processing.
  • PostgreSQL 18 documentation: Logical DecodingHow committed changes are read from the write-ahead log, in order, with their log sequence numbers.
  • Delta Sharing protocol (an open protocol of the Delta Lake project)Sharing tables in place, with access granted by the producer and no copy.

Practise it

The chapters that put this theory to work on Harbour Mutual and Kestrel:

The tools to try it in:

  • Stream lab · Run one claims feed as batch, Lambda and Kappa side by side, inject a late correction and replay the log.

Terms taught here: Change data captureBatchEvent streaming

An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Cloud Data Architecture Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.