AlgoViz

Batch or stream processing

easy 26 min

Batch processing collects a finite set of data and handles it all at once.

See it run

The same payments handled in two ways: when answers appear, what each one costs, and how mistakes are repaired.

Operations

The same nine payments

Both systems receive T1 to T9. One collects until the day ends, the other answers after every three.

Step 1 of 5

T1 to T3 arrive

Our systemShopspaymentsDay folder3 storedBatch jobasleepDaily reportemptyKafka topicT1 to T3Stream jobrunningLive numbers1 result

payments so far

T1 · T2 · T3

The folder collects them. The stream job handles them and writes result 1.

Step 1: T1 to T3 arrive

def batch(day):                       # runs at 02:00    write_report(total(read_all(day)))def stream(topic):                    # always running    for window in windows(topic, every=3):        update_live(total(window))
Answer after
Day or seconds
Effort
One or two systems

The streamed answers come early and in pieces. The batch answer comes once and is complete.

In the real world

The same batch or stream doing real jobs. Pick one and step through it.

A nightly job grows with the business.

Step 1 of 4

Year one

Our systemShopspaymentsDay folder100 GBBatch job1 hourDaily reportready 03:00Kafka topicunusedStream jobnoneLive numbersnone

The job starts at 02:00 and takes 1 hour. The report is ready at 03:00.

Step 1: Year one

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Streaming because it is modern

    critical

    You pay for a system that runs day and night, with state and late events, for a report read once a day.

    Wrong
    every_report = stream_job()
    Right
    daily = nightly_job()   # ask who acts on it
  • A batch job that appends

    critical

    A retry after a failure adds the day a second time.

    Wrong
    INSERT INTO report SELECT ...
    Right
    replace_partition(day,                  total(day))
  • Choosing the day by arrival

    major

    Payments that arrive after midnight belong to yesterday, and the report is closed.

    Wrong
    where arrived_on = yesterday
    Right
    where happened_on = yesterdayand rerun the last 3 days
  • No raw copy behind a stream

    major

    When the log expires, nothing can be recomputed.

    Wrong
    topic -> job -> counts
    Right
    topic -> raw filestopic -> job -> counts

Interview tips

  • How to explain it

    • Batch: collect, process, one result.
    • Stream: each event, again and again.
    • Latency against throughput per machine.
  • Lake and river

    • A lake is still: you can measure all of it.
    • A river flows: you measure as it passes.
    • Most companies have both.
  • Ask first

    • Who acts on the answer, and when?
    • Must it be complete?
    • How far back must we be able to recompute?
  • Watch

    • Batch: the time it ends, and its margin.
    • Stream: lag, watermark delay, state size.
    • Both: the difference between their totals.