Stream processing
medium 30 minStream processing handles events one after another as they arrive, without waiting for the data to end.
See it run
Follow events through a running job: windows, event time, state per key, and what happens when the job crashes.
Operations
One pass through
Clicks arrive, are put in a window, counted, and the result is sent on. Then it starts again.
Step 1 of 6
Events arrive
topic
click · page view · purchase
A click, a page view and a purchase are appended to the topic. The job has not read them yet.
Step 1: Events arrive
for event in topic: # never ends w = window_of(event.time) state[w] += 1 # group and add for done in closed(watermark): sink.write(done, state.pop(done))- Result after
- Window + wait
- Keeps
- One number per key
The job never finishes. It only moves forward.
In the real world
The same stream processing doing real jobs. Pick one and step through it.
The database behind a job gets slow. Follow the pressure back to the log.
Step 1 of 4
Normal
1,000 events a second in, 1,000 out.
Step 1: Normal
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Counting by arrival time
criticalA phone that was offline sends yesterday's clicks now. They land in the wrong minute, and a replay gives other numbers.
Wrongwindow = now() // 60Rightwindow = event.time // 60State that never shrinks
criticalOne entry per user for ever fills the disk, and every snapshot gets slower.
Wrongseen[user] = TrueRightseen[user] = Trueexpire(user, after="1 hour")Inserting results after a restart
majorA replay sends the same result again. An insert makes two rows.
WrongINSERT INTO counts ...Rightupsert(key=(window, user), value=count)One bad event stops everything
majorThe job crashes, restarts, reads the same event and crashes again.
Wronge = parse(raw) # raisesRighttry: e = parse(raw)except ValueError: dead_letter(raw)
Interview tips
How to explain it
- Data that never ends, handled as it comes.
- Windows cut it into pieces that can be counted.
- State plus position, saved together, survive a crash.
Always say
- Event time or processing time, and why.
- How late events may be, and what happens after that.
- How the sink copes with the same result twice.
Watch in production
- Lag in the log, in seconds.
- Watermark delay.
- State size and snapshot time.
- Late events and dead letters.
Do not use it when
- Nobody acts on the result for hours.
- A nightly query over files is enough.
- The team cannot run something day and night.