Batch vs stream processing
The same orders, counted two ways: a batch job that wakes up on a schedule and sums a whole window, and a stream job that adds each order the moment it arrives. Watch how far behind each dashboard is, then a late order and a crash.
What you'll take away
- Work out how far behind a batch dashboard runs: every 15 minutes, up to 17 behind
- Explain how event time, a watermark and allowed lateness decide when a window closes
- Make a replay after a crash safe by writing each window's total as an upsert
- Pick batch for reports and billing, and stream for fraud checks and live stock
The 1-minute version
Tap a point and the picture below plays that step.
Try 0 minutes of lateness and the late order is dropped. Try an hourly batch and watch its yard fill up. Then crash with an add sink: a bar on the stream's roof turns red
A sales dashboard is computed by a batch every 15 minutes, run two minutes after each window ends. How far behind the truth can it be?
The picture: the phones on the plaza place the orders, and at the corner each order goes two ways. Up the left street, the batch job keeps it as a crate in its yard, a row for each quarter hour, until it wakes two minutes after the quarter ends and takes the whole row in at once. Up the right street, the stream job adds each order the moment it gets there. The four bars on each roof are the quarter hours' totals: gold while the stream's window is still open, blue once it is closed or summed, red when something was counted twice. The amber light is the late order. Amounts and times are made up.
Compare them all: Batch · Stream
The same orders in every tile, including the late one from a phone that lost signal, counted four ways: batch every hour, every 15 minutes, micro-batch every minute, and a stream. See how far each dashboard trails the truth, and whether it counts the late order.
Use it when Reports read once a day, end-of-month billing, lots of data you want counted cheaply on machines that run only for the job
Skip it when Anything you must know now, like fraud or stock running out
A model: each batch runs two minutes after its window ends and counts only the orders that have arrived; the stream allows 5 minutes of lateness. Order amounts and times are made up. Press any tile to see it large above, with when it fits.
Which to pick
| Batch | Stream | |
|---|---|---|
| Results | After each run: minutes to a day behind | Seconds or less behind |
| Runs | On a schedule, then stops | All the time |
| Late data | Easy: run it again | Watermarks, allowed lateness, corrections |
| Fixing a bug | Fix and re-run the days affected | Fix and replay from the log, if you kept it |
| Good for | Reports, billing, training data | Fraud checks, alerts, live stock and prices |
Count by when it happened
Group events by event time, not by when they reached you, or a phone that was offline for ten minutes moves sales into the wrong hour. That's what makes late events a question at all: a window has to close some time, and something can still turn up after.
Plan for replays
Many streams only promise each event at least once, so after a crash some come again. Make the sink take repeats safely: write each window's total instead of adding to it, or remember the ids you've done.
In real code
-- The batch job: every 15 minutes, two minutes after its window ends, sum the window's orders that have arrived.
CREATE TABLE orders (id INTEGER PRIMARY KEY, happened_at TEXT NOT NULL, arrived_at TEXT NOT NULL, baht INTEGER NOT NULL);
INSERT INTO orders VALUES
(1, '2026-11-26 10:01:12', '2026-11-26 10:01:15', 100),
(2, '2026-11-26 10:03:30', '2026-11-26 10:03:33', 200),
(3, '2026-11-26 10:04:06', '2026-11-26 10:04:09', 300),
(4, '2026-11-26 10:07:48', '2026-11-26 10:07:51', 400),
(5, '2026-11-26 10:09:54', '2026-11-26 10:09:57', 500),
(6, '2026-11-26 10:12:18', '2026-11-26 10:12:21', 600),
(7, '2026-11-26 10:14:00', '2026-11-26 10:14:03', 700),
(8, '2026-11-26 10:16:36', '2026-11-26 10:16:39', 800),
(9, '2026-11-26 10:18:12', '2026-11-26 10:18:15', 900),
(10, '2026-11-26 10:21:42', '2026-11-26 10:21:45', 100),
(11, '2026-11-26 10:23:18', '2026-11-26 10:23:21', 200),
(12, '2026-11-26 10:25:00', '2026-11-26 10:25:03', 300),
(13, '2026-11-26 10:27:24', '2026-11-26 10:27:27', 400),
(14, '2026-11-26 10:29:30', '2026-11-26 10:33:00', 500),
(15, '2026-11-26 10:31:48', '2026-11-26 10:31:51', 600),
(16, '2026-11-26 10:34:12', '2026-11-26 10:34:15', 700),
(17, '2026-11-26 10:36:54', '2026-11-26 10:36:57', 800),
(18, '2026-11-26 10:38:30', '2026-11-26 10:38:33', 900),
(19, '2026-11-26 10:41:00', '2026-11-26 10:41:03', 100),
(20, '2026-11-26 10:44:24', '2026-11-26 10:44:27', 200),
(21, '2026-11-26 10:47:42', '2026-11-26 10:47:45', 300),
(22, '2026-11-26 10:51:12', '2026-11-26 10:51:15', 400),
(23, '2026-11-26 10:55:36', '2026-11-26 10:55:39', 500),
(24, '2026-11-26 10:58:18', '2026-11-26 10:58:21', 600);
-- order 14 happened at 10:29:30 on a phone that was offline; it arrived at 10:33:00
-- the run at 10:32: the 10:15–10:30 window, as much of it as has arrived
SELECT 'run at 10:32, window 10:15', sum(baht), count(*) FROM orders
WHERE happened_at >= '2026-11-26 10:15:00' AND happened_at < '2026-11-26 10:30:00'
AND arrived_at <= '2026-11-26 10:32:00';
-- the night's re-run: every window again, with everything that arrived
SELECT 'window ' || strftime('%H:', happened_at) || printf('%02d', CAST(strftime('%M', happened_at) AS INTEGER) / 15 * 15) AS w,
sum(baht), count(*)
FROM orders GROUP BY w ORDER BY w;
sqlite3 :memory: < batch.sqlite.sql (checked with the sqlite3 CLI, 3.54)It printed:run at 10:32, window 10:15|2700|6 window 10:00|2800|7 window 10:15|3200|7 window 10:30|3300|6 window 10:45|1800|4
The same 24 orders in a table. The run at 10:32 sums the 10:15–10:30 window as far as it has arrived, and misses the late order; the night's re-run groups every window again and gets it. Really run with sqlite3; below is what it printed.
Common questions
What's the difference between batch and stream processing?
Batch collects data and runs on a schedule: simple, always safe to run again, but its results are a run behind. Stream handles each event as it arrives: results almost at once, but the job runs all the time and you look after state, watermarks and replays.
With a batch every 15 minutes, how far behind can the dashboard be?
Up to about 17 minutes: an order that comes in just after a window closes waits for the whole next window, plus the two minutes before the job runs. To be less behind, run more often (micro-batch) or switch to a stream.
Event time vs processing time: what's the difference?
Event time is when it really happened, like the tap on Buy. Processing time is when your system got it. They drift apart with slow networks, offline phones or a backed-up queue, and grouping by arrival puts sales in the wrong hour.
What's a watermark?
The job's mark of how far it has seen in event time, usually the latest event time seen minus the lateness you allow. Once it passes a window's end, that window closes and its total goes out. Anything for it after that is a late event.
What happens to an order that arrives after its window has closed?
With no lateness allowed it's dropped (or set aside), and that window's total comes up short. Allow more lateness and it still counts, at the cost of totals that come out later. A batch has it easier: just run that window again.
Why can a replay after a crash push the total too high?
Many streams only promise at-least-once: after a crash the job restarts from its checkpoint and reads those events again. If the sink does total = total + ?, they're added twice. Write each window's whole total instead (an upsert), remember the ids you've done, or use a sink that supports exactly-once.
What is micro-batch?
A batch that runs very often, every second to every minute; Spark Structured Streaming works this way by default. You keep batch-style code, but the results are still at least one run behind. It sits between batch and stream.
When should I use batch, and when stream?
Batch for reports, billing and training data: anything that can be minutes or a day behind. Stream for what you must know within seconds: fraud checks, alerts, live stock and prices. A small team that only needs reports is far better off with batch.
Next lesson
IdempotencyLevel: Intermediate
A replay after a crash counted the same orders twice; idempotency is how a write happens once however often it's sent.