Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Courses/Asynchronous and Event-Driven Systems
Lesson 17 of 20

Stream processing: windows and watermarks

Compute continuous results over unbounded data that arrives late and out of order.

20 min read

Bounded and unbounded data

Batch jobs process a finished dataset: yesterday's logs, all of them, and the answer is final. Stream processors (Flink, Kafka Streams, Spark Structured Streaming) process data that never ends, updating results continuously as events arrive. 'Page views per minute, available within seconds' is a streaming question. A stream processor reads from partitioned topics, keeps running state such as counts per key, and writes results to another topic or store.

Event time vs processing time

Every event has two timestamps: when it happened (event time) and when your system saw it (processing time). They can differ by seconds on a busy pipeline and by hours for a phone that was offline on a plane. If you count clicks by processing time, a backlog or an outage moves activity into the wrong minute. Almost every business question is about event time, so windows should be based on it.

Windows

Aggregating an infinite stream requires cutting it into finite windows.

  • Tumbling windows: fixed and non-overlapping, such as each whole minute
  • Hopping (sliding) windows: fixed length that advance by a smaller step, such as the last 5 minutes recomputed every minute
  • Session windows: per user, closed after a gap of inactivity such as 30 minutes

Watermarks and late data

When can the 10:00 to 10:01 window be considered complete? A watermark is the processor's estimate that no more events older than time T will arrive, typically the latest event time seen minus an allowed delay such as 30 seconds. When the watermark passes a window's end, the window fires. Events that arrive after that are late: drop them, send them to a side output, or allow a lateness period during which the window updates its result. A longer delay gives more complete results; a shorter one gives faster results.

State, recovery and the batch layer

Running counts live in local state stores. Flink periodically checkpoints that state together with the input offsets, so after a crash it restores both and the results reflect each event exactly once. Many analytics systems still pair the stream with a nightly batch job over the raw events in a warehouse: the stream gives fast, approximately right numbers, and the batch recomputes the authoritative daily report.

Key takeaways

  • Aggregate by event time, not processing time
  • Pick tumbling, hopping or session windows to match the question
  • Watermarks trade completeness for latency; decide what happens to late events
  • Checkpoint state with offsets for correct recovery, and use batch for authoritative reports

Sign in to track lessons and earn badges.

CQRS: separate models for writes and readsSchema evolution for events

Challenge checkpoint

Course: 0/20 lessons · 0/10 challenges0%

Build it on the canvas, pass the review (70+) and mark it complete to count it toward the course badge.

  1. 9/10Design Clickstream AnalyticsIngest 200 000 events per second into a stream with event-time windows, plus batch reports in a warehouse.MAJORMedium
All 10 challenges →