Batch or stream processing
easy 26 minBatch 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
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
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
criticalYou pay for a system that runs day and night, with state and late events, for a report read once a day.
Wrongevery_report = stream_job()Rightdaily = nightly_job() # ask who acts on itA batch job that appends
criticalA retry after a failure adds the day a second time.
WrongINSERT INTO report SELECT ...Rightreplace_partition(day, total(day))Choosing the day by arrival
majorPayments that arrive after midnight belong to yesterday, and the report is closed.
Wrongwhere arrived_on = yesterdayRightwhere happened_on = yesterdayand rerun the last 3 daysNo raw copy behind a stream
majorWhen the log expires, nothing can be recomputed.
Wrongtopic -> job -> countsRighttopic -> 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.