Batch vs Streaming
Lesson opener
In one line: When nightly batch is enough versus when streaming is justified.
Why it matters: Streaming systems cost more operational complexity; batch is usually enough for reporting.
Before this lesson: Data Analysis with Python / Python basics
Before you start
| Resource | Link |
|---|---|
| Previous | What data engineers do |
| Next | Storage formats |
| Prerequisites | Data Analysis with Python or Python basics |
| Time | ~55 minutes |
What you will learn
- When nightly batch is enough versus when streaming is justified
- Latency, cost, and correctness trade-offs for Indian product teams
- How to model a micro-batch window in pure Python
- Watermarks, late events, and why real-time is rarely truly instantaneous
Pinned libraries for this course
Install the course pins once and reuse them across lessons:
pip install -r docs/dataeng/requirements.txtPinned in docs/dataeng/requirements.txt:
pandas==2.2.3
numpy==2.1.3
pyarrow==18.1.0Pin versions so teammates in Bengaluru, Pune, and Mumbai reproduce the same numbers. This lesson uses the Python standard library plus the pins above where shown. Each Python fence re-imports everything so scripts/test_course_code.py can run blocks in isolation.
Choose the pace that matches the decision
A Mumbai payments PM wants "real-time fraud flags." A Bengaluru warehouse lead needs inventory every morning before pick waves. A Pune growth analyst refreshes cohort charts once a day. Batch processes data in finite chunks on a schedule. Streaming processes events continuously (or in tiny micro-batches) as they arrive.
WHY Streaming systems cost more operational complexity; batch is usually enough for reporting.
WHEN Use streaming when the decision latency must be seconds/minutes and you can afford the ops tax.
Demo 1 — Classify workloads
# Offline demo: batch vs streaming labels for workplace jobs
jobs = [
{"name": "gst_invoice_export", "max_delay_min": 24 * 60, "mode": "batch"},
{"name": "upi_fraud_score", "max_delay_min": 1, "mode": "streaming"},
{"name": "seller_payout_recon", "max_delay_min": 12 * 60, "mode": "batch"},
{"name": "live_courier_eta", "max_delay_min": 2, "mode": "streaming"},
]
batch = [j["name"] for j in jobs if j["mode"] == "batch"]
stream = [j["name"] for j in jobs if j["mode"] == "streaming"]
print("batch:", batch)
print("stream:", stream)
assert "gst_invoice_export" in batch
assert "upi_fraud_score" in streamIf leadership says "everything real-time," ask which decision breaks if the number is six hours old.
Latency, completeness, and cost
Batch jobs can re-read yesterday's full file and recompute. Streaming must decide what to do with late events — a courier ping that arrives after the window closed. Completeness and low latency fight each other.
WHY Late data is normal on Indian mobile networks and festival traffic spikes.
WHEN Design watermarks before you promise sub-minute dashboards to executives.
Demo 2 — Micro-batch windows
# Offline demo: assign events to 5-minute micro-batches (IST naive demo)
from datetime import datetime, timedelta
def window_start(ts: datetime, minutes: int = 5) -> datetime:
discard = timedelta(
minutes=ts.minute % minutes,
seconds=ts.second,
microseconds=ts.microsecond,
)
return ts - discard
events = [
datetime(2026, 9, 18, 10, 0, 12),
datetime(2026, 9, 18, 10, 3, 40),
datetime(2026, 9, 18, 10, 5, 1),
]
windows = [window_start(e) for e in events]
print(windows)
assert windows[0] == windows[1]
assert windows[2] != windows[0]Demo 3 — Late event vs watermark
# Offline demo: drop events older than watermark
from datetime import datetime, timedelta
def accept(event_ts: datetime, watermark: datetime) -> bool:
return event_ts >= watermark
wm = datetime(2026, 9, 18, 10, 0)
on_time = datetime(2026, 9, 18, 10, 2)
late = datetime(2026, 9, 18, 9, 50)
print(accept(on_time, wm), accept(late, wm))
assert accept(on_time, wm) is True
assert accept(late, wm) is False
print("watermark:", wm.isoformat())[!important] Dropping late events silently can undercount GMV; route them to a side table and alert.
Batch patterns that feel "live enough"
Many Bengaluru teams run batch every 15–60 minutes and call it near-real-time. That is honest if SLAs match. Prefer simple schedules you can explain to on-call over a fragile stream that nobody understands at 3 a.m.
Demo 4 — Schedule next run
# Offline demo: next batch run after a cadence
from datetime import datetime, timedelta
def next_run(last: datetime, every_minutes: int) -> datetime:
return last + timedelta(minutes=every_minutes)
last = datetime(2026, 9, 18, 6, 0)
print(next_run(last, 30))
assert next_run(last, 30) == datetime(2026, 9, 18, 6, 30)
assert next_run(last, 60) == datetime(2026, 9, 18, 7, 0)Demo 5 — Throughput sketch
# Offline demo: rough events/sec capacity check
def needed_eps(events_per_day: int, peak_factor: float = 3.0) -> float:
return (events_per_day / 86_400) * peak_factor
eps = needed_eps(5_000_000, 3.0)
print(round(eps, 2))
assert eps > 100
assert needed_eps(86_400, 1.0) == 1.0Demo 6 — Decide batch or stream
# Offline demo: decision helper for mode selection
def choose_mode(max_delay_min: int, team_stream_ready: bool) -> str:
if max_delay_min <= 5 and team_stream_ready:
return "streaming"
return "batch"
print(choose_mode(1, True))
print(choose_mode(1, False))
print(choose_mode(180, True))
assert choose_mode(1, True) == "streaming"
assert choose_mode(180, True) == "batch"Indian workplace framing
UPI settlement files often land in batch windows. Live courier ETAs need streaming or aggressive micro-batch. Festival sales (Diwali, Big Billion–style events) amplify both volume and late arrivals — load-test your batch first; only then add streaming for the slice that truly needs it.
WHY Complexity during peak weeks is the most expensive time to learn a new paradigm.
WHEN In architecture review with product, finance, and on-call represented.
Honest SLAs beat buzzwords
If a Mumbai executive dashboard refreshes hourly, say hourly. Calling it real-time sets a trap for on-call when a vendor batch is the true source. Prefer written SLAs in IST minutes. During festival peaks, protect batch completion first; only then expand streaming for fraud or live ETA slices that justify the complexity for Bengaluru product and Pune data teams.
WHY Misnamed latency expectations create false incidents.
WHEN In the product requirements doc before vendor selection.
Explain it to a stakeholder
"Real-time" is a product word. Latency is an engineering word. Ask the Mumbai payments lead: what decision changes if the fraud score is one minute late versus one hour late? Ask the Bengaluru warehouse lead: does pick-wave planning need second-level updates, or is a 30-minute batch enough before the morning shift? Write the answers as SLAs in minutes. Then choose batch, micro-batch, or streaming to match — not to impress a slide deck.
Streaming is justified when (1) the decision window is short, (2) events arrive continuously, and (3) the team can operate watermarks, late data, and replay. If any of those fail, prefer a reliable batch that finishes by a clock everyone trusts in IST. Many Pune growth dashboards that claim live data are really fifteen-minute batches with a bold font.
WHY Misnamed latency creates false incidents and wrong tool spend.
WHEN In architecture review with product, finance, and on-call represented.
Demo 7 — Cost sketch for batch vs stream ops
# Offline demo: rough monthly ops-cost sketch in INR (toy numbers)
def monthly_cost(mode: str, base_inr: int = 40_000) -> int:
if mode == "streaming":
return base_inr * 3
if mode == "micro_batch":
return int(base_inr * 1.5)
return base_inr
print("batch", monthly_cost("batch"))
print("micro", monthly_cost("micro_batch"))
print("stream", monthly_cost("streaming"))
assert monthly_cost("streaming") > monthly_cost("batch")
assert monthly_cost("micro_batch") == 60_000Use toy costs only to force the conversation. Replace with your cloud bill later.
Festival weeks and late events
Diwali and large sale events increase both volume and late mobile events. Load-test batch first. Keep a side table for late arrivals instead of silently dropping GMV. Document whether late events revise yesterday's gold table or only appear in a corrections feed. Hyderabad and Chennai courier pings often arrive after a five-minute watermark — plan for that before promising live ETAs.
WHY Peak weeks are the worst time to debut a fragile stream.
WHEN At least one sprint before the festival calendar peak.
Micro-batch as a bridge
Many Bengaluru product teams land on micro-batch first: run the same batch code every five to fifteen minutes. That reuses validation and load logic while shrinking latency. Streaming can wait until event volume and decision latency both demand it. Document the cadence in IST so Pune on-call knows whether a missed run is five minutes late or five hours late.
If a Mumbai executive dashboard only needs hourly refresh, resist rewriting the stack for second-level updates. Spend the engineering time on contracts, quarantine, and idempotent loads instead.
Verified against pandas 2.2.3, numpy 2.1.3, pyarrow 18.1.0 documentation, September 2026.
Test yourself
- What is the main difference between batch and streaming processing?
- Give one Indian workplace job that fits batch well.
- Why might a team choose micro-batch instead of true streaming?
- What does a watermark help decide?
- True or false: late events never happen on mobile networks.
- In Demo 2, why do the first two events share a window?
- What does
choose_mode(1, False)return and why? - Name one cost of streaming beyond cloud spend.
- Which pin file lists
pandas==2.2.3for this course? - True or false: calling a 60-minute batch 'real-time' is always accurate.