Stream processing (Flink / Spark Structured Streaming)

Event time vs processing time, watermarks and allowed lateness, windowing (tumbling/sliding/session), stateful operators and checkpoints. Late data handling is the depth probe.

About 25 min read3-question knowledge check

The mental model

A stream processor computes over data that never ends. The core trick: it must answer "what happened between 14:00 and 14:05" while events from that window are still arriving late, out of order, and while the process itself can crash mid-count.

The analogy: grading exam papers mailed from around the world. Event time is the postmark (when the student finished); processing time is when the envelope lands on your desk. Papers arrive out of postmark order. You want per-day results by postmark, so you keep a folder (a window) per day. The question that defines streaming: *when do you seal a folder and publish grades?* You can't wait forever. So you track the mail: "the oldest postmark still arriving is roughly N days back" — that running estimate is the watermark. When the watermark passes a day's end, you seal that folder. A paper arriving after sealing is late data: you either reopen briefly (allowed lateness), file it in an exceptions tray (side output), or drop it — an explicit policy, never an accident. Your grade tallies are state, and photographing all folders periodically (checkpoints) is what lets you resume after a fire without regrading everything.

Why this exists: without event time and watermarks, results depend on network weather — a mobile client that buffered events for an hour would smear last hour's activity into this hour's numbers, and re-running the job would give different answers. Watermarks buy deterministic results over non-deterministic arrival, at the price of latency (you wait) and a late-data policy (you decide what "too late" means).

The two mainstream engines: Flink — true event-at-a-time streaming, rich state, fine watermark control, exactly-once via distributed snapshots. Spark Structured Streaming — micro-batches on the Spark engine, simpler ops for Spark shops, coarser latency (hundreds of ms to seconds) and simpler-but-blunter watermark semantics.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision