AlgoViz

Stream processing

medium 30 min

Stream 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

Our systemeventsreadresultsnapshotUsers3 new eventsKafka topic3 waitingStream jobidleDashboardemptyCheckpointnone yet

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

Our systemeventsreadresultsnapshotUsersKafka topiclag 0Stream job1,000 a secondDashboard1,000 a secondCheckpointnone yet

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

    critical

    A phone that was offline sends yesterday's clicks now. They land in the wrong minute, and a replay gives other numbers.

    Wrong
    window = now() // 60
    Right
    window = event.time // 60
  • State that never shrinks

    critical

    One entry per user for ever fills the disk, and every snapshot gets slower.

    Wrong
    seen[user] = True
    Right
    seen[user] = Trueexpire(user, after="1 hour")
  • Inserting results after a restart

    major

    A replay sends the same result again. An insert makes two rows.

    Wrong
    INSERT INTO counts ...
    Right
    upsert(key=(window, user),       value=count)
  • One bad event stops everything

    major

    The job crashes, restarts, reads the same event and crashes again.

    Wrong
    e = parse(raw)   # raises
    Right
    try: 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.