Module 01 · Lesson 6 of 11Free
Batch vs streaming: how fresh data needs to be
- Level
- Beginner
- Reading time
- 14 min
When you finish this page you can
- Work out how far behind a batch, micro-batch or streaming pipeline is, from its schedule and run time.
- Choose batch, micro-batch or streaming for a report from how quickly someone acts on it.
- Name what freshness costs: compute that never stops, and late and duplicate events to handle.
- Explain why the same events give the same total in batch and streaming, but at different times.
Builds on 2 lessons
Start with lesson 112 minChapters
Show transcript
0:00 It is the first evening of a festival sale at Kartly, an online store in India. At 20:00 a coupon code stops working. Shoppers tap “Apply coupon” and nothing happens. Orders start to fall. But Kartly’s data team loads its data once a night. Daniel leads finance reporting, and he reads the results the next morning. So the first person who could notice the problem sees it eleven hours late. Now the odd part. For Daniel’s report, that slow nightly load is actually perfect. He opens it once, in the morning. So this video asks one question. How fresh does each job need its data? And what does each extra step of freshness cost?
0:36 Data freshness is how far behind the real world your data is when someone reads it. Take one click. Measure the time from the click happening to the click sitting in a table you can query. Meera, Kartly’s data engineer, can work that out from two things. How long the data waits for a run to pick it up. And how long the run takes. Start with the nightly job. A click on Monday evening waits for the job to start. The job finishes in the small hours. Seven and a half hours after the click, it is in the table. Daniel reads at 07:00, so it is in time. Now run the job every five minutes. A click waits for the next run, then for the run itself. That is up to seven minutes behind. The wait shrinks when runs are closer together. The run time does not. Streaming goes further. Nothing waits for a schedule. Each click is handled as it arrives, so the lag is a few seconds. Next, hold each number up against what the reader needs. Daniel needs all of Monday by the next morning. The nightly job gets there with hours to spare. The festival dashboard has to be less than a minute behind. Only streaming can do that, or micro-batches that are very small.
1:39 Start with batch, which Meera already runs. On a schedule, a job starts up and reads one fixed chunk of data, say all of Monday. It calculates the answer one time, saves it, and shuts down. If a run fails, she simply runs it again for that same day. Streaming treats data as something that never finishes. Clicks keep coming. A stream processor runs all the time. It reads each click as it arrives. It keeps running totals, such as clicks per minute, in memory. A dashboard reads those totals. Micro-batch is the middle way. It is still a batch job, but it runs again every few seconds or minutes. Spark, a popular tool, does this by default. It treats a stream as a long series of tiny batch jobs. Its documentation says the delay can drop to 100 milliseconds.
2:24 Freshness also costs computing power. A batch job only needs enough for its deadline, then it is switched off. A streaming job never rests. It has to keep up with the busiest second of the day. At Kartly’s later size, the festival peak is about 22 times the average. So the stream must handle that load, or grow in time. The harder cost is correctness. Every click has two times. Event time is when the shopper tapped. Processing time is when the pipeline reads it. A phone on a train taps in the 20:00 minute, but only gets signal two minutes later. A batch run at night counts that click in its right minute. A stream may already have shown that minute without it. That is late-arriving data. So the stream has to pick. It can wait longer before publishing each minute. It can publish now and correct the number later. Or it can drop the click. Here the dashboard first showed 3 clicks for that minute, then fixed it to 4. The batch run got it right the first time. Flink, a stream tool, states the trade plainly. Counting by processing time is the fastest, but results can differ between runs. Counting by event time is correct, but adds delay while it waits for late clicks. You cannot have the lowest delay and the most correct answer together. The second problem is duplicates. When the app’s send times out, it sends the same click again. And after a crash, a reader may get some events twice. A batch job cleans this up with one query over the whole day. A stream must remember every event id it saw recently. At festival peak, ten minutes of ids is about 30 million. That is only an estimate, but it is memory a batch job never needs. Now picture the stream job restarting in the middle of the sale. It picks up from its last saved position, which is a little earlier. So it reads some minutes a second time, and adds them to counts it already wrote. The dashboard shows a spike. Marketing moves ad budget to a promotion that is not working. The fix is to make a replay harmless. Write each minute’s total so that writing it again replaces the old one.
4:12 A lot of engineers believe streaming is the modern choice and batch is the old one. Look at Daniel’s report, though. It has to be final, with every Monday click and order counted exactly once. A stream shows a running number, and late events keep changing it. So for him the nightly batch is simply the right tool. So should Meera move everything to streaming? No. Finance stays a nightly batch. Only the festival dashboard gets a streaming path, reading the same click events. That means two paths to run. So the dashboard is labelled live, and the morning batch is the official number. The right choice depends on the target. Say the dashboard could be 15 minutes behind. Then a micro-batch every few minutes is enough, and nothing has to run all day. But if a number must be under a second old, like a fraud check at checkout, only streaming works.
4:58 Here is the recap. One. Choose by how fresh each reader needs the data. Two. Freshness is waiting time plus run time. Three. Micro-batch is a batch job on a short schedule. Four. Streaming costs compute that never stops. Five. Streams must handle late and duplicate events, so the same total arrives sooner but can change. That gives Meera two kinds of pipeline. Each one needs its own tools. Think of a scheduler, a warehouse, a stream and a store for dashboards. Next up is the modern data stack.
A coupon breaks at 20:00 and nobody sees it
It’s 20:00 on the first evening of a festival sale at , an online store in India. A coupon code stops working. Taps on “Apply coupon” jump, and orders fall.
Kartly’s data team loads the store’s orders and click events once a night, from a read-only copy of the store’s PostgreSQL database. Daniel, who leads finance reporting, reads the results at 07:00. So the first person who could notice the broken coupon sees it 11 hours later.
On a normal day Kartly takes about ₹30 lakh of orders an hour (₹7.2 crore ÷ 24). A festival evening brings far more. Eleven blind hours is real money.
Yet the same nightly load is exactly right for Daniel’s report. This lesson answers one question: how fresh does each use of the data need to be, and what does each step of freshness cost?
Freshness is a number you can work out
is how far behind the real world the data is when someone reads it: the time from a click happening to that click being in a table you can query. Meera, Kartly’s data engineer, can work it out from two things: how long data waits before a run picks it up, and how long the run takes.
Meera changes the nightly job to run every 5 minutes. Each run takes about 2 minutes. Is the dashboard now 5 minutes behind?Show answer
Nightly batch: a click at 20:00 on Monday
Job starts 01:00 Tuesday, finishes about 03:30 → 7.5 hours behind
A click at 00:05 Monday waits almost a full day, so it is about 27 hours old when it lands. Every click of Monday is in the table by 03:30.
Micro-batch every 5 minutes
Up to 5 minutes waiting + 2 minutes running = at most 7 minutes behind
The wait shrinks with the interval. The run time doesn’t.
Streaming
Each click processed as it arrives → a few seconds behind
Nothing waits for a schedule. The lag is the time to move and process one event.
The 03:30 finish, the 2-minute run and “a few seconds” are this lesson’s scenario numbers, not measurements.
Now compare each number with what the reader needs. Daniel needs Monday complete by 07:00 Tuesday. The nightly batch meets that with 3.5 hours to spare. The festival dashboard needs to be under 1 minute behind. Only streaming, or very small micro-batches, can meet that.
Three ways to process the same clicks
Batch is what Meera already runs, from Introduction to data engineering. A wakes on a schedule and reads a bounded chunk of data: all of Monday. It computes the result once, writes it and stops. If a run fails, she runs it again over the same day.
Streaming treats the data as unbounded: clicks never “finish”. A runs all the time, reads each event as it arrives and keeps running results, such as clicks per minute, in memory. Stream processing explains the mechanism. At Kartly’s later size, clicks would flow through Kafka into Flink, with serving the dashboard.
Micro-batch sits between them. It is a batch job on a short schedule, every few seconds to every few minutes. Spark’s streaming engine works this way by default: it runs a stream as a series of small batch jobs, and its documentation states end-to-end latency as low as 100 ms 2.
Nightly batch
Streaming
| Batch | Micro-batch | Streaming | |
|---|---|---|---|
| Input per run | A whole day, complete | A few seconds to minutes | One event at a time |
| How far behind | Hours | About the interval plus run time | Seconds or less |
| Compute | Runs, then stops | Starts often, or stays on | Always on |
| After a failure | Rerun the day | Rerun the last slice | Resume from saved state |
What freshness costs
Compute that never stops
A batch job is sized for its deadline and switched off afterwards. A streaming job runs every hour of every day, and it must keep up with the busiest second.
Average rate at Kartly’s later size
200,000,000 clicks a day ÷ 86,400 s ≈ 2,300 clicks a second
About 200 million click events a day, from the course’s later modules.
Festival peak
50,000 clicks a second ÷ 2,300 ≈ 22× the average
The stream job must handle this, or scale up for it in time.
Hours of compute a year
Streaming: 24 h × 365 = 8,760 h. Nightly: 2 h × 365 = 730 h
Even on the same machines, streaming runs 12 times as many hours.
An estimate for this scenario. The 2-hour nightly run is assumed.
Late and duplicate events
The harder cost is correctness. Each click carries two times: its , when the shopper tapped, and its , when the pipeline reads it. A phone on a train taps at 20:00:50 and only gets a signal at 20:02:10.
A batch run at 01:00 sees that click in its right minute. A stream that published the 20:00 minute at 20:01 has already shown a number without it. This is , and the stream has three choices: wait longer before publishing each minute, publish and correct the number later, or drop the click.
Flink’s documentation states the tradeoff plainly. Counting by processing time gives the lowest latency but results that can differ between runs 1. Counting by event time gives correct results but adds latency while it waits for out-of-order events 1. A is how a stream decides it has waited long enough 1. Watermarks covers how to set one.
Duplicates are the second problem. If the app’s send times out, it sends the click again. Many event pipelines are set up for : after a crash, a consumer may read some events twice. Delivery guarantees explains why.
A batch job removes duplicates with one query over the whole day. A stream must remember every event id it has seen recently. Remembering 10 minutes of ids at the festival peak means holding 50,000 × 600 = 30 million ids, about 480 MB at 16 bytes each, before any overhead. That is an estimate, and it is memory the batch job never needs.
The dashboard counts two minutes twice
- At 20:14 during the festival sale, the machine running the stream job restarts.
- The job resumes from its last saved position, at 20:12, and reads those two minutes again.
- Its writes add to the per-minute counts instead of replacing them. At 50,000 clicks a second, about 6 million clicks are counted twice.
- The dashboard shows a spike on the coupon page. Marketing moves ad budget to a promotion that isn’t working.
The fix is to make a replay harmless: write each minute’s total so that writing it again replaces it. Tools can also promise exactly-once results, at a cost, as Delivery guarantees in Kafka pipelines shows. The Dataflow paper frames the whole design as balancing correctness, latency and cost 3: you can’t maximise all three.
Choosing for each reader of the data
Many engineers believe streaming is the modern way and batch is the old one. But Daniel’s report must be final: every Monday click and order, counted once. A stream reports a running number that late events keep changing. For him, the nightly batch is the better tool, not a legacy one.
Should Meera move Kartly’s pipelines to streaming?
Stream everything, including finance.
The festival dashboard is seconds behind. But Daniel’s report gains nothing from it: he reads it once, at 07:00, and needs it complete. Kartly pays for always-on compute and late-event handling on numbers that don’t need it.
Keep everything as one nightly batch.
One simple system, cheap and easy to rerun. The festival dashboard is 3.5 to 27 hours behind, so the broken coupon is found the next morning.
Choose per reader: batch for finance, streaming only for the festival dashboard.
Finance stays a nightly batch. Only the festival dashboard gets a streaming path, reading the same click events. Two paths to run, and the dashboard is labelled live: the morning batch is the official number.
Why this choice
The deciding question is how quickly someone acts on each number. Daniel acts once a day, so a few hours of lag cost nothing. The marketing team acts within minutes during a sale, so 11 hours of lag cost sales. Each path is chosen for its reader, and only the dashboard pays for freshness.
The choice changes with the target. If the dashboard could be 15 minutes behind, a micro-batch every 5 minutes would meet it without a job that never stops. If finance wanted an hourly revenue view, an hourly micro-batch would do. If a number had to be under a second old, such as a fraud check at checkout, only per-event streaming fits.
Running both paths at once
Running a batch path and a streaming path over the same events is sometimes called the lambda architecture. Its known cost is keeping two codebases that must compute the same thing. The kappa architecture keeps only the stream and recomputes history by replaying the log. Spark and Flink both narrow the gap by running one program over bounded and unbounded input.
Try it yourself
See the same total arrive at different times. You need Python 3.11 or later and DuckDB:
pip install duckdb==2.0.0Save this as freshness.py and run python freshness.py. Eight made-up Kartly clicks; click 4 comes from a phone that was offline for over a minute.
from datetime import datetime, timedelta
import duckdb
con = duckdb.connect() # in-memory database, nothing saved
con.sql("""
CREATE TABLE clicks AS
SELECT * FROM (VALUES
(1, TIMESTAMP '2026-11-10 20:00:05', TIMESTAMP '2026-11-10 20:00:06'),
(2, TIMESTAMP '2026-11-10 20:00:20', TIMESTAMP '2026-11-10 20:00:21'),
(3, TIMESTAMP '2026-11-10 20:00:41', TIMESTAMP '2026-11-10 20:00:42'),
(4, TIMESTAMP '2026-11-10 20:00:50', TIMESTAMP '2026-11-10 20:02:10'),
(5, TIMESTAMP '2026-11-10 20:01:02', TIMESTAMP '2026-11-10 20:01:03'),
(6, TIMESTAMP '2026-11-10 20:01:30', TIMESTAMP '2026-11-10 20:01:31'),
(7, TIMESTAMP '2026-11-10 20:01:58', TIMESTAMP '2026-11-10 20:01:59'),
(8, TIMESTAMP '2026-11-10 20:02:15', TIMESTAMP '2026-11-10 20:02:16')
) AS t(event_id, event_time, arrived_at)
""")
# Clicks per minute of event time, counting only clicks that have arrived.
PER_MINUTE = """
SELECT date_trunc('minute', event_time) AS minute, count(*) AS clicks
FROM clicks
WHERE arrived_at <= ?
GROUP BY minute
ORDER BY minute
"""
def report(now: datetime) -> None:
rows = con.execute(PER_MINUTE, [now]).fetchall()
counts = ", ".join(f"{minute:%H:%M} = {n}" for minute, n in rows)
total = sum(n for _, n in rows)
print(f"at {now:%d %b %H:%M}: {counts} (total {total})")
# Small windows: report at the end of every minute.
for i in range(3):
report(datetime(2026, 11, 10, 20, 1) + timedelta(minutes=i))
# Batch: one run the next morning, after everything has arrived.
report(datetime(2026, 11, 11, 3, 0))The output should look like this:
at 10 Nov 20:01: 20:00 = 3 (total 3)
at 10 Nov 20:02: 20:00 = 3, 20:01 = 3 (total 6)
at 10 Nov 20:03: 20:00 = 4, 20:01 = 3, 20:02 = 1 (total 8)
at 11 Nov 03:00: 20:00 = 4, 20:01 = 3, 20:02 = 1 (total 8)Look for two things. Both runs end at a total of 8, but the windowed run had all of it by 20:03, seven hours earlier. And the 20:00 minute changed from 3 to 4 after it was first shown: that is click 4 arriving late. This script re-counts the table each time to keep it short; a real stream processor updates its counts as each event arrives. Then add a second copy of click 5, as if the app had retried it, and change count(*) to count(DISTINCT event_id).
Where this shows up
- Design: a live sales dashboard for a festival sale builds the streaming path this lesson chose for the dashboard.
- Design: a clickstream pipeline for an online store moves click events as a stream instead of a nightly copy.
- Design: orchestrating a daily reporting pipeline keeps Daniel’s nightly batch on time.
Meera now has two kinds of pipeline, and each needs tools: a scheduler, a warehouse, a stream, a dashboard store. The next lesson, The modern data stack, maps which tool does which job.
Common mistakes
Ask what decision the number drives and how soon someone acts on it. A report read once a morning doesn’t need a stream.
Grouping by processing time puts the train passenger’s 20:00 click into 20:02. Group by event time, and decide what happens to late events.
Retries and restarts repeat events. Give every event an id, and make writes safe to repeat.
Check your understanding
A micro-batch runs every 10 minutes and each run takes 4 minutes. What is the worst-case lag, and does it meet a 15-minute target?Show answer
At 07:00 the live dashboard says Monday had 9,412,000 clicks. Daniel’s batch report says 9,418,000. Which is wrong?Show answer
Kartly wants a check that blocks a fraudulent payment before the order is confirmed. Batch, micro-batch or streaming?Show answer
A stream job restarts and the next minute’s count is twice the usual. What most likely happened, and what fixes it?Show answer
Summary
- Choose by how fresh each reader needs the data: finance by 07:00 means nightly batch; a festival dashboard under 1 minute means streaming.
- Freshness is waiting time plus run time: a nightly job is hours behind, a micro-batch about its interval, a stream seconds.
- Micro-batch is a batch job on a short schedule, and sits between the two.
- Streaming costs compute that never stops, sized for a peak about 22 times the average at Kartly’s later size.
- Streams must handle late and duplicate events while counting; batch sees the whole day at once, so the same total arrives later but final.
