Stream Processing Basics
From messages crossing a pipe to state over time
Stream processing turns raw events into state: windowing groups events in time, keyed state tracks per-entity aggregates, and event-time handling decides how late data behaves. These three ideas distinguish real stream processors from message routers.
What you will learn
- Distinguish stateless routing from stateful processing
- Explain tumbling, sliding and session windows
- Use watermarks to define lateness handling
What is this?
Stream processing computes on events while they're in motion: windowing buckets events by time (tumbling — fixed disjoint, sliding — overlapping, session — idle-gap based), keyed state holds per-key aggregates across events, and event-time semantics with watermarks decide when windows close and how late data is handled. Without these, you have message routing, not stream processing.
Why do you need it?
- Business questions are time-bucketed: 'orders in the last 5 minutes', 'sessions ending', 'active users this hour'.
- Keyed state unlocks per-user/per-device reasoning — counts, sessions, flows.
- Event-time processing keeps the numbers correct when events arrive late or out of order.
- Windowing is how 'real-time analytics' and 'continuous alerts' actually stay meaningful.
Mental model
Sorting mail by delivery time
Windowing + watermark
You sort letters into the slot matching when they were sent (event time). A watermark is like a printed 'all letters up to 10:00 should have arrived': when it passes, you seal that slot (close the window) and count what's inside. Late letters after seal are processed via the late-data path (allowed lateness).
How it works
- A window assigns each event to a bucket based on its timestamp and the window type.
- Tumbling windows are fixed-length, disjoint (0-5, 5-10); sliding overlap (every 1 min, length 5); session split on idle gaps.
- Each window aggregates its events (count, sum, top-N) emitted when windows close.
- Watermarks advance event-time progress; events older than the watermark may be dropped or sent to a late-data path.
- Keyed state stores per-key values between events, enabling session spreads, running totals, and per-user logic.
Tumbling, sliding and session windows.
In production
- Session windows on clickstreams bound each user's browsing session by 30-min idle gaps.
- Tumbling 5-min windows track website error counts and page an on-call when p95 SSE > 1000.
- Sliding 1-min windows give a rolling 'orders in the last hour' number for a live revenue tracker.
Best practices
- Bucket on event time (when it happened) and set a watermark matched to your arrival skew.
- Bound keyed state: cap keys and TTLs so unbounded distinct keys don't sink memory.
- Choose the smallest window your SLA needs — smaller windows = more updates, more cost.
- Handle late data explicitly: allowed-lateness, or a correction/compaction path.
Common mistakes
- Windowing on processing time — numbers drift by ingestion delay and jitter.
- Infinite session windows that never close (no idle-timeout).
- State keyed by a high-cardinality unbounded value (event_id) → OOM.
- Dropping late events silently, then under-counting a critical metric.
Common issues
Metrics are consistently 5 minutes behind the wall clockWindowing on ingestion time, or watermark too pessimistic
Window on event time with a watermark of real lateness; tune separate monitoring.
Memory explodes on a session-window jobUnbounded keys or sessions never closing due to a huge idle timeout
Cap session gap, key cardinality and state TTL thresholds.
Late events create numbers that drift backward on re-runAllowed-lateness reprocessing without a correction/compaction step
Emit corrected totals, or exclude late data and document the cutoff.
Debugging tips
- Check event time vs processing time to diagnose 'numbers always lag' complaints.
- Inspect the watermark vs the newest event time — wide gaps mean late-path hazards.
- State growth → look at keys cardinality and TTL settings.
- Double-counting across windows → inspect window overlap (sliding data counted twice by design).
Interview checkpoint
Hands-on lab
- Classify a stream of events into tumbling, sliding and session windows on event time
- Observe how window type changes bucket membership and emitted counts
- Apply a watermark to decide when late events stop entering a closed window
Implement Tumbling, Sliding and Session Windows
35 min · intermediate
Quick quiz
A 5-minute tumbling window with a 2-minute watermark finalizes an event-time 12:03 window at roughly...
Sliding windows can count the same event in more than one window.