30. Lambda and Kappa: architectures for streaming
Lambda runs a fast streaming layer beside a slow batch layer that overwrites it every night; Kappa runs one streaming job over a log it can replay. Both can be right in the end; they differ in when, at what cost, and in how many places the logic lives.
- Theory 23: Streaming: event time, windows, watermarks, exactly-once; Lambda and Kappa
- Theory 26: Event-at-a-time streaming: state, event time and backpressure
- Theory 27: Micro-batch: a stream as small batches
- Theory 28: The Lambda architecture: two paths and their cost
- Theory 29: The Kappa architecture: one path, replayed
- Theory 32: How to choose and combine pipeline patterns
The clip reads the same claims log as a nightly batch, as Lambda and as Kappa, event by event, into fifteen-minute windows. The adjuster's correction to C-103 arrives at 09:56, after its window has closed: Lambda's speed layer and Kappa both drop it, and their first window disagrees with the truth. Kappa's corrected job then replays the log from the first event and agrees with the truth at once; Lambda waits for the nightly batch at 23:00, whose batch layer recomputes every window and overwrites the speed layer.
The idea: one log, two ways to read it
Both architectures start from an event log: every claim event kept in order, with the time it happened (event time) and the time it was read. A streaming job sums events into windows of event time and keeps each window open until its watermark (the latest event time seen, less the allowed lateness) passes the window's end; an event that arrives later is dropped.
- Lambda runs two layers: a speed layer that streams for fresh, approximate figures, and a batch layer that recomputes everything from the log every night and wins. Late data is fixed at night. The price: the same logic written twice, in two code paths that drift apart.
- Kappa runs one streaming job. To fix a mistake, or take in a correction after its window closed, you replay: start the corrected job from the first event in the log into a new view, and switch readers to it when it has caught up. The price: the log must be kept long enough, and a replay takes time and compute.
A worked example: C-103's late correction
The feed has 12 events in 4 windows. Before any fix, the streaming views are right in 3 of 4 windows: the correction was dropped. Kappa's replay makes it 4 of 4 before the batch; Lambda reaches 4 of 4 only after the batch at 23:00.
| Architecture | Right before the batch | Right after it | Events read | Cost | Code paths |
|---|---|---|---|---|---|
| Batch | 0 / 4 | 4 / 4 | 12 | 5.2 | 1 |
| Lambda | 3 / 4 | 4 / 4 | 24 | 30.4 | 2 |
| Kappa | 4 / 4 | 4 / 4 | 24 | 26.4 | 1 |
A replay is not free. Harbour Mutual's claims log carries about 120,000 events a day; kept for 30 days on 4 partitions, each read at 500 events a second, a replay reads 3,600,000 events in about 30 minutes, about 2.0 compute units.
CREATE TABLE claim_latest (claim STRING NOT NULL, event_time TIMESTAMP NOT NULL, amount INT NOT NULL, version INT NOT NULL) USING DELTA;
-- For every micro-batch read from the log (the batch is the view `updates`):
MERGE INTO claim_latest AS t
USING (
SELECT claim, event_time, amount, version FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY claim ORDER BY version DESC) AS rn FROM updates
) WHERE rn = 1
) AS s
ON t.claim = s.claim
WHEN MATCHED AND s.version > t.version THEN UPDATE SET event_time = s.event_time, amount = s.amount, version = s.version
WHEN NOT MATCHED THEN INSERT (claim, event_time, amount, version) VALUES (s.claim, s.event_time, s.amount, s.version);
-- The serving view:
SELECT DATE_FORMAT(window(event_time, '15 minutes').start, 'HH:mm') AS window_start, SUM(amount) AS total
FROM claim_latest
GROUP BY window(event_time, '15 minutes')
ORDER BY window_start;Why it matters for Alex
The fraud team wants claims scored in minutes, finance wants figures it can sign, and adjusters correct amounts days later. Alex has to say where corrections land, how long the log is kept, and whether the company can afford two versions of the same logic. When batch is enough, saying so saves a streaming platform nobody needed.
Where it goes wrong
- Lambda's two code paths. A fix made in the batch layer and forgotten in the speed layer: the dashboard and the morning report disagree, every day.
- Kappa without the log. A replay needs the events: keep the log only seven days and a correction from last month cannot be replayed.
- Lateness set by hope. Windows that close in five minutes for corrections that come in hours: the stream is wrong until someone replays it.
- Streaming what batch would do. A monthly report built on a stream: more to run, more to break, and no reader who needed it sooner.
Exercises
The lab checks each one against facts written down in advance, so any answer the brief allows passes. A pass is kept in this browser.
A late correction, handled both ways
The adjuster's correction to C-103 arrives at 09:56, long after its window closed. Harbour Mutual's dashboard must show the right totals: Kappa's view before the nightly batch, Lambda's after it, with the speed layer running the same code as the batch layer and never counting a claim twice.
The lab checks that:
- The late correction stays in the feed.
- Kappa's view is right in every window before the nightly batch.
- Lambda's view is right in every window after the nightly batch.
- The speed layer runs the same code as the batch layer.
- No view ever counts a claim twice.
Batch, Lambda or Kappa
Which architecture fits the requirement? Pass all 3 situations to complete it.
A monthly list of large claims for the reinsurer. Amounts are often corrected during the month; nobody needs the list before month end. The team runs no streaming today.
How long does the replay take?
Harbour Mutual's claims log carries 120,000 events a day, and each partition is read at 500 events a second during a replay. Estimate how long each replay takes.
On Azure
The streaming parts on Azure:
- Azure Event Hubs is the log: partitions, consumer groups, and retention of up to seven days on the Standard tier and up to ninety on Premium and Dedicated. For longer, Event Hubs Capture writes the events to the lake, and a replay reads them from there.
- Fabric Real-Time Intelligence: Eventstream reads Event Hubs and routes events, Eventhouse stores them in KQL databases for queries in seconds, and Activator acts on patterns as they happen.
- Databricks Structured Streaming runs the Kappa job on Delta tables: a checkpoint remembers where it is in the log, and a replay is the corrected job started with a new checkpoint from the earliest offset (or from the captured files).
Batch is still the right answer for monthly and quarterly figures, for anything a finance team signs, and wherever nobody acts on the data faster than once a day.
Take it to work
Streaming architecture choice
Decide between batch, Lambda and Kappa for one feed.
The feed
- Who reads it, and how fresh it must be
- How late corrections arrive
The log
- Where events are kept, and for how long
The architecture
- Batch, Lambda or Kappa, and the reason
Corrections
- Allowed lateness
- Replay: how long it takes and what it costs
Book of record
- Which view finance signs
Tools in this chapter
- 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: Lambda architectureKappa architectureEvent timeWindowWatermarkReplay