interviewpath
  • 0day streak
  • 10total XP
  • Level 1
0/6

Unit 6 · Databases · Lesson 9 of 10

Stream processing: event time, watermarks and replay

Stream processors compute continuously over unbounded, out-of-order event streams. Correct results require windowing by event time, watermarks that decide when a window is complete enough, explicit handling of late data, and checkpoints with replayable sources so state survives failures without double counting.
About 3 h 30 minFocus mode

Step 1: Learn the ideas

  • Bounded versus unbounded data; batch as a special case
  • Event time, processing time and the skew between them
  • Windows: tumbling, sliding, session; keyed state
  • Watermarks: perfect versus heuristic, idle sources
  • Late data: allowed lateness, side outputs, retractions
  • Triggers and accumulation modes
  • Checkpoints, barriers and replayable sources
  • Exactly-once state versus end-to-end exactly-once (transactional or idempotent sinks)
  • Stream-table duality, stream joins and state size

Why it matters

Product analytics, fraud detection, monitoring and feature pipelines for ML all compute over event streams. The hard part is rarely throughput; it is correctness. Events arrive late and out of order, machines fail mid-window, and a retry must not count a purchase twice. Interviewers probe whether you can say what a result means at the moment it is emitted and what happens when a late event arrives.

Core ideas

Two clocks. Event time is when something happened, carried in the event; processing time is when the system sees it. The gap varies with network delay, mobile clients reconnecting and backlogs after an outage. Grouping by processing time is simple but attributes events to the wrong period whenever the gap varies; event-time windows give results that do not depend on arrival timing.

Windows. Tumbling windows are fixed and non-overlapping (per minute); sliding windows overlap (the last 10 minutes, updated every minute); session windows are per key and close after a gap of inactivity. A windowed aggregation keeps keyed state for each open window.

Watermarks. A watermark at time t asserts that no more events with event time at or before t are expected. When the watermark passes a window's end, the window can fire. A perfect watermark needs complete knowledge of the source's ordering; most are heuristics, such as the largest event time seen minus a bound on out-of-orderness. A conservative watermark adds latency; an aggressive one emits incomplete results and produces more late data. One idle input partition can hold back the watermark of the whole job unless it is marked idle.

Late data. Events behind the watermark are late. You can drop them, keep window state for an allowed lateness and emit corrected results, or send them to a side output for separate repair. Corrections require consumers that can handle updated or retracted results.

Triggers and accumulation. The Dataflow model separates what is computed (the aggregation), where in event time (the windowing), when in processing time results are emitted (triggers: at the watermark, early speculative firings, late firings) and how successive results relate (discarding, accumulating, or accumulating with retractions). This vocabulary lets you state a pipeline's semantics precisely.

Fault tolerance. Stream processors checkpoint operator state periodically. Flink-style engines inject barriers into the streams; an operator snapshots its state once barriers from all its inputs have arrived, which yields a consistent global snapshot without stopping the pipeline. After a failure, state is restored from the last checkpoint and sources rewind to the offsets recorded with it, which requires a replayable source such as a durable log. This gives exactly-once effects on internal state. External effects need more: idempotent writes keyed by a deterministic ID, or transactional sinks that commit output together with the checkpoint. Otherwise replay produces duplicates downstream.

watermark = max_event_time_seen - 30s        # bounded out-of-orderness
window [12:00, 12:05) fires once watermark >= 12:05
event with time 12:03 arriving later: late -> update, side output or drop

Common misconceptions

  • "Exactly-once means each event is processed once." After failures events are reprocessed; exactly-once describes the effect on state, and on outputs only with suitable sinks.
  • "Watermarks guarantee completeness." Most are estimates, so late data must still be handled explicitly.
  • "Processing-time windows are fine if the system is fast." Backlogs, retries and client delays still move events between windows.
  • "Streaming replaces batch." Many pipelines pair a streaming path for freshness with batch recomputation for corrections; the Dataflow model treats batch as the bounded case of the same semantics.
  • "More frequent checkpoints are always better." They shorten replay after failure but cost I/O and can add latency.

How to approach it

Start with Streaming 101 and 102 for intuition, then read the Dataflow paper's sections on windowing, triggers and refinements. Implement one windowed aggregation in a real engine (the Flink training's Hourly Tips exercise is a good size) with an explicit watermark strategy, inject late events and observe which outputs change. In interviews, pin down the metric's semantics first (which time, how late data can be, whether results may be revised), then choose windows, watermark and sink, and tell the failure story end to end.

Suggested order

  1. 1Read DDIA Ch. 11 (Stream Processing) for logs as streams, stream-table duality, time and fault tolerance.
  2. 2Read Streaming 101, then Streaming 102, for windowing, watermarks, triggers and accumulation with diagrams.
  3. 3Read the Dataflow Model paper, focusing on the model sections (windowing, triggers, incremental processing).
  4. 4Read the Flink concept pages Timely Stream Processing and Stateful Stream Processing.
  5. 5Skim the asynchronous snapshots paper to understand barrier alignment and replay.
  6. 6Do the Flink training exercises, at least Windowed Analytics (Hourly Tips) and ProcessFunction and Timers (Long Ride Alerts).

Step 2: Check off what you can do

Tap each one you can genuinely do or explain. Each earns 10 XP.

Step 3: Go deeper

  • Designing Data-Intensive ApplicationsStart here

    O'Reilly Media (author's book site)

    Section: Ch. 11: Stream Processing

    Event logs, stream-table duality, reasoning about time, stream joins and fault tolerance in one chapter.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • The Dataflow Model: A Practical Approach to Balancing Correctness, Latency, and Cost in Massive-Scale, Unbounded, Out-of-Order Data ProcessingStart here

    Google Research

    The windowing, trigger and accumulation model that defines precise semantics for out-of-order streams.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Streaming 101 and Streaming 102: The world beyond batch

    O’Reilly Radar

    Intuition and diagrams for event time, watermarks and triggers before reading the paper; continue with Streaming 102.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Apache Flink concepts: timely and stateful stream processing

    Apache Flink documentation

    Section: Timely Stream Processing

    How a production engine defines event time, watermarks, lateness and windows; see also the stateful processing page.

    Open(opens in a new tab)Not checked, 5 Oct 2026
  • Lightweight Asynchronous Snapshots for Distributed Dataflows

    arXiv

    The barrier-based snapshot algorithm behind exactly-once state in Flink.

    Open(opens in a new tab)Link responded, 5 Oct 2026

Step 4: Practice

Work through the Flink training exercises with tests, then extend Hourly Tips: choose a bounded out-of-orderness watermark, inject artificially late events, and compare dropping them, allowed lateness with updated results, and a side output. Write down what each output row means at the moment it is emitted. If you do not want to set up Flink, do the same design on paper for per-user session counts and specify the sink’s idempotency key.

Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).

Step 5: Test yourself

Answer each one out loud or on paper before you finish the lesson.

  1. Question 1What does a watermark assert, and what happens to an event that arrives behind it?
  2. Question 2Which window type fits “active users per session”, and why?
  3. Question 3What is the difference between discarding and accumulating firings?
  4. Question 4Why does exactly-once processing need a replayable source?
  5. Question 5How can one idle partition stall a whole job’s output?
  6. Question 6What makes a sink idempotent, and when is a transactional sink needed instead?

You’re done when you can

  • Explain with a timeline why processing-time and event-time windows give different answers for the same events.
  • Design a windowed pipeline with watermark, lateness policy and sink for a given metric and defend each choice.
  • Explain in under three minutes how barrier checkpoints plus replay give exactly-once state, and what the sink must add.
  • Implement one windowed aggregation and show how a late event changes its output.

Finish the lesson

How confident do you feel?

Lesson complete!

+50 XP

Reviews will come up on the Practice tab so it sticks.

Your notes and study time

Log study time