What you'll learn: By the end of this lesson you will be able to describe a batch pipeline, a stream pipeline, the Lambda architecture and the Kappa architecture, explain why Lambda's duplicated logic is its main cost, state what a Kappa replay needs from the log, and choose a pipeline for a ShopStream analytics dashboard.
A restaurant counts its takings twice. At closing the owner adds up every receipt for the exact figure. During the evening a waiter keeps a quick, sometimes wrong tally on a notepad. The notepad answers "how are we doing right now?" and the receipts answer "what was the truth?". The architectures below differ in how they answer both with the least duplicated work.
The stream processing lesson taught windows and late events inside one stream job, and the data warehouses and lakes lesson taught the batch side. This lesson is about the architecture that joins them.
Verified on 2026-12-01 against Nathan Marz's Lambda article, Jay Kreps's article on the Lambda architecture, the Apache Flink 2.3 execution-mode page and the Apache Kafka 4.2 documentation. The only code on this page is a small Node script, which was run.
The core idea
Every analytics number is a function of an append-only log of events. The architectures differ in how often, and with how many programs, that function is applied. A batch job applies it to everything at intervals. A stream job applies it to each event as it arrives. Lambda runs both and merges them. Kappa runs only the stream job and re-runs it from the start of the log when the logic changes.
Three illustrations run through the lesson:
- Minimal example: six order events, summed per store by one rule, then by two copies of it.
- Realistic example: a ShopStream store dashboard of revenue per store.
- Failure example: a refund bug fixed by a replay that the log can no longer serve.
Batch and stream, side by side
A batch pipeline reads a bounded dataset and writes a result: simple and easy to re-run, but as old as its last run. A stream pipeline updates results continuously: fresh, but it must handle state, ordering and late events.
| Batch | Stream | |
|---|---|---|
| Input | Bounded set, such as one day of files | Unbounded flow of events |
| Result freshness | As old as the last run | Seconds, in principle |
| Fixing a bug | Re-run over the data | Needs a replay, or the old results stay wrong |
| Hard parts | Scheduling, backfills, big recomputes | State, time, late events, exactly-once effects |
Lambda: two paths and a merge
Marz described a design with a batch layer that stores the master dataset as "an append-only set of immutable facts" and precomputes views over all of it, a speed layer that covers only the recent window the batch layer has not yet processed, and a serving step where a query reads the batch view and the real-time view and merges them. The batch results are a few hours old; the real-time results are temporary, because "everything the realtime layer computes is eventually overridden by the batch layer".
Figure 1. Lambda. How to read this: the same events feed two programs. The query layer adds the batch view for everything before a cutoff to the speed view for everything after it. When the batch job catches up, its numbers replace the speed view's.
The cost is that the transformation logic exists twice, in two systems. Kreps put it this way: maintaining code "that needs to produce the same result in two complex distributed systems" is as painful as it sounds, and he did not think the problem was fixable. He called the operational complexity of running two systems "universally agreed on by everyone doing it".
Kappa: one path and a replay
Kreps proposed the alternative later named Kappa. Keep the full input in a log that supports several readers, with retention long enough for your reprocessing window. To change the logic, start a second instance of the job from the beginning of the retained log, writing to a new output table. When it catches up, switch readers to the new table, then stop the old job and delete the old table. Keeping both tables briefly allows an instant revert.
Figure 2. Kappa. How to read this: only one program exists at a time per version. Reprocessing is a second run of the same job, not a different batch system.
Figure 3. A Kappa cutover. How to read this: readers are never moved until the new table has caught up, which is also what lets you compare both tables first.
What the log must provide
Kafka's documentation says time- or size-based retention discards "old log data", while log compaction guarantees Kafka retains "at least the last known value for each message key". A compacted topic is a snapshot of final values, not the full history, so it cannot replay a sum from first principles; a replay needs long retention or an archive of the raw events. Kafka's Streams reset tool resets input offsets (by default to the beginning) and deletes internal topics, but does not reset output topics or local state, and all instances must be stopped, so a side-by-side replay is a different procedure.
Closing the gap: one engine for both
Duplication shrinks when one engine runs the same program over both kinds of input. Flink offers STREAMING, BATCH and AUTOMATIC modes; BATCH "can only be used" for bounded jobs, and a bounded job produces "the same final results regardless of the configured execution mode". Only intermediate output differs: streaming emits incremental updates, batch one final result.
Illustration 1: the minimal example
Six illustrative events (amounts in cents) are summed per store. The rule is paid minus refunds. The Lambda paths use a batch cutoff at offset 3. The script was run with Node 26.
const applyEvent = (view, e) => ({
...view,
[e.store]: (view[e.store] ?? 0) + (e.type === "refund" ? -e.cents : e.cents),
});
// batchView = log.filter((e) => e.offset < 3).reduce(applyEvent, {})
// speedSame = recent.reduce(applyEvent, {}); speedDrifted ignores refunds
Running the full script printed:
full recompute {"s1":1100,"s2":800}
lambda, same rule {"s1":1100,"s2":800}
lambda, drifted speed {"s1":1100,"s2":900}
kappa, v1 (buggy) v1 {"s1":1300,"s2":900}
kappa, after replay v2 {"s1":1100,"s2":800}
With one shared rule, Lambda matches a full recompute. When the speed layer's copy forgets refunds, store s2 reads 900 instead of 800 until the batch run catches up. In the Kappa lines the buggy job's table is wrong for the whole history, and a replay into a new table corrects all of it.
Choosing: a decision flow
to replay all history?"} Q2 -->|yes| K["Stream job, Kappa-style replay"] Q2 -->|no| L["Lambda: batch for truth,
stream for freshness, one engine if possible"]
Figure 4. A first-pass choice. How to read this: ask about freshness first, then about the log. Lambda is the exit for when neither a stale answer nor an unreplayable log is acceptable.
Trade-offs
| Design | Gain | Cost | Wrong when |
|---|---|---|---|
| Batch only | Simplest, easy re-runs | Stale answers | Users need current numbers |
| Stream only, no replay | Fresh | A logic bug stays in history | Numbers feed money decisions |
| Lambda | Fresh and exact; works if the log is short | Two programs, two systems, drift | Two teams cannot keep them equal |
| Kappa | One program, one framework | Needs long retention and temporary double storage | The log cannot hold the history |
Build it
This lesson has no lab. The script in Illustration 1 is a teaching model, not a repository lab, and has no golden file.
In practice: a ShopStream store dashboard
This is a realistic scenario, not a case history. It is a composite on the ShopStream reference project, and every number in it is an illustrative assumption.
This is the realistic example.
The situation
ShopStream (500,000 orders a day at full scale, which is about 5.8 per second on average, illustrative) shows each store manager a dashboard of revenue per store per hour. Today it reads a warehouse table loaded overnight, so managers see yesterday. They want the last hour, and finance still needs the exact day.
Starting architecture
Options considered
| Option | Gain | Cost | Wrong when |
|---|---|---|---|
| A. Lambda: keep the nightly job, add a stream job | Exact day plus fresh hour | Refund and cancel rules written twice | Rules change often |
| B. Kappa: one stream job over the order-event log, with the warehouse fed from the same events | One rule set; replays fix bugs | Retention must cover the replay window | Events are not complete or kept |
The decision
Option B. The revenue rule (paid minus refunds, minus cancelled orders) lives in one stream job over the order events. The log keeps 90 days (illustrative); older history comes from the warehouse, which is loaded from the same events for finance. A fix is shipped as a new job version that replays into a new table and is switched over after a comparison.
Rollout and verification
Run the stream job beside the nightly load for two weeks, compare each store-hour with the warehouse and explain every difference, then switch the dashboard and keep the old table for a week.
The 2 a.m. failure
A store manager reports negative revenue. The job ignored a new refund event type added by the payments team. The fix is ready, but a replay needs 120 days of events and the log holds 90.
First checks: the log's retention against the oldest event the replay needs; which event types the job's rule covers; whether the warehouse has the missing days. Mitigation: replay the retained 90 days, and rebuild older days from the warehouse. Follow-up: a contract test that fails when a new event type has no rule.
Architect's notes
- Decide the replay window first. It sets retention, storage cost and how far back a bug can be fixed.
- Put the rule in one place. If two copies exist, test them against one fixture.
Common mistakes
- Assuming a compacted topic can replay history.
- Cutting over before the new job has caught up.
- Running Lambda "for safety" when one engine and a long log would do.
Key takeaways
- Analytics numbers are functions of an immutable event log; architectures differ in how often and how many times the function runs.
- Lambda pairs a batch layer for exactness with a speed layer for freshness and merges at query time; its cost is the same logic in two systems.
- Kappa uses one job and replays the retained log into a new table to change logic.
- Compaction keeps the last value per key, not the history, so replays need retention or an archive.
Practice exercises
Exercise 1 (30 min). Extend the script's log with a cancelled-order event, then update the Lambda copies and observe which one breaks.
Exercise 2 (1 hour). For three ShopStream metrics (orders per minute, daily revenue, monthly store ranking), choose batch, Lambda or Kappa and state the replay window.
Related reading
- Stream processing basics: windows, state and late events.
- Data warehouses and lakes: the batch side and ETL.
- Kafka internals: the log, retention and compaction.
- Event streaming: the log as a system component.
Official documentation
- Nathan Marz, How to beat the CAP theorem: the batch, real-time and serving design.
- Jay Kreps, Questioning the Lambda Architecture: the case for replaying one job.
- Apache Flink documentation, execution mode (batch and streaming).
- Apache Kafka documentation, design: log compaction and Streams application reset tool.