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

23. Streaming: event time, windows, watermarks, exactly-once; Lambda and Kappa

A stream is data that never ends, arriving out of order. Grouping it needs windows over the time things happened, not the time they arrived; a watermark is the system's estimate of how far event time has got, which decides when a window is complete and what counts as late. Delivery guarantees say how often each event takes effect, and Lambda and Kappa are two ways to keep a stream's answers right.

Late events landing in windows as the watermark moves
Playback speed

The clip plays nine claim events in the order the pipeline receives them. Each has the time the loss was reported in the claims portal (event time) and the time it reached the pipeline (processing time). It drops each event into a ten-minute window by event time, while a second row groups them by arrival and gets different totals. A watermark follows behind the latest event time seen, five minutes back. When the watermark passes a window's end, the window closes. One event arrives after its window has closed: it is late, and dropped, unless the pipeline allows lateness or corrects later.

The intuition: the post arrives out of order

Harbour Mutual's claims portal sends every reported claim to a pipeline that keeps a running total of claims per ten minutes, for the operations dashboard. Most events arrive within a minute of happening. Some do not: a phone on a train loses its signal, a broker's system sends a batch at the end of the hour. An event that happened at 09:09 can arrive at 09:21.

If the dashboard groups by arrival time, that claim is counted in the wrong ten minutes, and the totals depend on network delays rather than on what happened. If it groups by when the claim happened, it must decide how long to keep the 09:00 window open: forever, and it never shows a final number; not at all, and every late event is lost. Every streaming system makes that decision, explicitly or by accident.

Remember Group by event time, close windows when the watermark passes them, and decide in advance what happens to data that arrives later.

The theory: time, windows, watermarks, guarantees and architectures

Event time is when something happened in the world; processing time is when the system observed it. The gap between them, the skew, varies from event to event, so events arrive out of event-time order. Akidau and colleagues' Dataflow model, the basis of Apache Beam and influential on Spark and Flink, separates four questions: what is computed (the aggregation), where in event time (the windows), when in processing time results are emitted (triggers and watermarks), and how later results relate to earlier ones (accumulation).

A window groups an unbounded stream into finite pieces. Tumbling windows are fixed and do not overlap (09:00 to 09:10, 09:10 to 09:20). Sliding (hopping) windows have a fixed length and start more often than their length, so they overlap (ten minutes, every five). Session windows are cut by gaps in activity, so their length depends on the data: one per burst of a customer's clicks.

A watermark is a moving assertion: "no more events with event time earlier than W are expected". A common heuristic sets W to the latest event time seen minus an allowed delay. When the watermark passes a window's end, the window can be finalised and emitted. Events whose window has already closed are late data: the system drops them, or, if it allows lateness, updates the window and emits a correction. A slow watermark waits longer and is more often right; a fast one is quick and more often wrong. There is no setting that is both.

Nine claim events grouped by event time, by processing time, and by event time with a five-minute watermark (run on Spark 4.0.1 with Delta Lake 4.0.1 in CI)
CREATE SCHEMA bronze;

CREATE TABLE bronze.claim_events (claim STRING, event_time TIMESTAMP, arrival_time TIMESTAMP, amount INT) USING DELTA;

INSERT INTO bronze.claim_events VALUES
  ('C1', TIMESTAMP '2026-10-06 09:01:00', TIMESTAMP '2026-10-06 09:01:00', 100),
  ('C2', TIMESTAMP '2026-10-06 09:04:00', TIMESTAMP '2026-10-06 09:05:00', 200),
  ('C3', TIMESTAMP '2026-10-06 09:08:00', TIMESTAMP '2026-10-06 09:14:00', 150),
  ('C4', TIMESTAMP '2026-10-06 09:12:00', TIMESTAMP '2026-10-06 09:13:00', 300),
  ('C5', TIMESTAMP '2026-10-06 09:17:00', TIMESTAMP '2026-10-06 09:18:00', 120),
  ('C6', TIMESTAMP '2026-10-06 09:09:00', TIMESTAMP '2026-10-06 09:21:00', 80),
  ('C7', TIMESTAMP '2026-10-06 09:23:00', TIMESTAMP '2026-10-06 09:24:00', 50),
  ('C8', TIMESTAMP '2026-10-06 09:19:00', TIMESTAMP '2026-10-06 09:25:00', 70),
  ('C9', TIMESTAMP '2026-10-06 09:26:00', TIMESTAMP '2026-10-06 09:29:00', 40);

SELECT CAST(window.start AS STRING) AS window_start, SUM(amount) AS total
FROM bronze.claim_events
GROUP BY window(event_time, '10 minutes')
ORDER BY window_start;

SELECT CAST(window.start AS STRING) AS window_start, SUM(amount) AS total
FROM bronze.claim_events
GROUP BY window(arrival_time, '10 minutes')
ORDER BY window_start;

WITH placed AS (
  SELECT event_time, arrival_time, amount, window(event_time, '10 minutes') AS w
  FROM bronze.claim_events
),
watermarked AS (
  SELECT w, amount,
         MAX(event_time) OVER (ORDER BY arrival_time ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) - INTERVAL 5 MINUTES AS watermark
  FROM placed
)
SELECT CAST(w.start AS STRING) AS window_start, SUM(amount) AS total
FROM watermarked
WHERE watermark IS NULL OR w.end > watermark
GROUP BY w.start
ORDER BY window_start;
Totals per ten-minute window from the three queries
WindowBy event timeBy processing timeEvent time, watermark 5 minutes
09:00530300450
09:10490570490
09:209024090

Grouped by arrival, the totals describe the network, not the claims. With the watermark, claim C6 (which happened at 09:09 and arrived at 09:21) finds its window already closed and is dropped: the first window's total is short by 80 until a correction is allowed.

Delivery guarantees and exactly-once At-most-once delivery never repeats an event and may lose some; at-least-once never loses one and may repeat some. Exactly-once is about effect, not delivery: each event changes the result once, although it may be delivered and processed more than once after a failure. It is built from replayable sources (a log with offsets), checkpoints that store the offsets and the state together, and idempotent or transactional sinks that make a repeated write harmless. End to end it holds only if every link in the chain does.

Two architectures keep a stream's answers right over time. Lambda (Marz) runs a batch layer that periodically recomputes everything from the full history beside a speed layer for fresh results; Kappa (Kreps) keeps one stream processor over a replayable log and recomputes by replaying it. Pages 28 and 29 take each apart on Harbour Mutual's claims: the serving merge and the cost of two code paths, and a replay with its retention, schema and capacity limits.

Why it matters

The operations dashboard and the fraud checks of the combined company read streams; the board's figures read tables. Choosing event time, the watermark's delay and the handling of late data is choosing how wrong a number may be, and for how long. Chapter 30 lets you watch Lambda's two paths drift apart and Kappa's single replayed stream converge.

Exactly-once matters wherever a stream updates money: a claim payment counted twice is not an approximation.

Where it goes wrong

  • Windows by processing time. Totals that change when the network is slow describe the pipeline, not the business.
  • No policy for late data. Late events vanish silently and nobody knows the totals are short.
  • Exactly-once assumed. A sink that appends on retry turns at-least-once delivery into double counting.
  • Two code paths. Lambda's batch and speed layers drift apart with every change made to only one of them.
  • A log that forgets. Kappa cannot replay what the log no longer keeps.

Check yourself

Choose an answer to each question, then press Check.

1. A claim happened at 09:09 and reached the pipeline at 09:21. Which is its event time?
2. What does a watermark assert?
3. How does a Kappa architecture recompute results after a bug fix?
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.

  • T. Akidau et al., "The Dataflow Model: A Practical Approach to Balancing Correctness, Latency, and Cost in Massive-Scale, Unbounded, Out-of-Order Data Processing", Proceedings of VLDB, 2015Event and processing time, windows, watermarks, triggers and accumulation as four separate questions.
  • T. Akidau, S. Chernyak and R. Lax, Streaming Systems (O'Reilly, 2018)Watermarks as heuristics, late data, and exactly-once processing in practice.
  • J. Kreps, "Questioning the Lambda Architecture" (2014)The Kappa architecture: one stream processor over a replayable log.
  • N. Marz and J. Warren, Big Data: Principles and Best Practices of Scalable Realtime Data Systems (Manning, 2015)The Lambda architecture's batch, speed and serving layers.
  • Apache Spark 4.0 documentation: Structured Streaming Programming GuideEvent-time windows, watermarks and the end-to-end exactly-once guarantees of the engine run in CI.

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: Lambda architectureKappa architectureEvent timeWindowWatermarkReplayExactly-once processing

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.