Streaming vs Batch Processing

Batch processes data that has piled up, on a schedule: simple, cheap, complete, but with latency from minutes to a day. Streaming processes each event as it…

Does Bookshop's owner need the sales figures this very second, or will tomorrow morning do? The answer decides how the data is processed. Batch processing handles data that has already piled up, on a schedule, say every night. Stream processing (streaming) handles each event as soon as it arrives. It's like washing up. The batch way: plates pile up all day and get washed once at night. It saves water and soap, but clean plates only exist tomorrow. The streaming way: each plate is washed as soon as it's used. Always clean, but you're back and forth to the sink all day. The difference isn't which is more advanced, but how soon the result is needed, and what that costs.

Batch, micro-batch, streaming

Batch Micro-batch Streaming
When it runs on a schedule, say every 00:00 every few seconds or minutes, in small groups per event, as it arrives
Latency (event to result) minutes to a day seconds to minutes milliseconds to seconds
Tools Airflow + SQL or Python, dbt Spark Structured Streaming Apache Flink, Kafka Streams
Examples daily reports, retraining a model every night a dashboard refreshed every minute fraud detection, stock during a flash sale, alerts

Streaming is the better fit when data must be processed within seconds of arriving: holding a suspicious payment, keeping stock from overselling, a live dashboard, an error alert. When a result a few hours or a day late is fine, batch is simpler and cheaper.

Latency, completeness, cost

These three are always traded against each other:

Event time and processing time

In streaming, each order is sent as an event: a small message holding order_id, created_at and its number of books. Every event has two times:

Usually they're seconds apart. But the event for order 8 (Maya, Sunday 4 October 20:30) got stuck in a queue job, like item 9 that was retried, and only reaches the processor on Monday 5 October at 12:00, after order 9's event (Monday 11:20). By event time, Maya's order belongs to the week of 28 September; by processing time, to the week of 5 October. The owner's question is about when people bought, so event time is right.

Windows: tumbling, sliding, session

A stream never ends, so "add everything up" becomes "add up per time window". Computed from the 14 clean orders (21 books), by event time:

Window What it is Result at Bookshop
Tumbling 1 day fixed slices, no overlap; each event lands in exactly one window 28 Sep 3, 29 Sep 2, 30 Sep 3, 1 Oct 3, ... , 3 Oct 0, ... , 9 Oct 1
Tumbling 1 week the same, per Monday week of 28 Sep 13, week of 5 Oct 8
Sliding 3 days, moving 1 day overlapping windows; each event lands in 3 windows 28 to 30 Sep 8, 29 Sep to 1 Oct 8, 2 to 4 Oct 2
Session, 24-hour gap a window ends after 24 hours without an event; lengths vary 5 sessions: 3, 5, 4, 6, 3 books

Sessions are the interesting one in batch, because they need window functions. In SQLite (the Lab and the Playground):

WITH RECURSIVE clean AS (
  SELECT *, ROW_NUMBER() OVER (PARTITION BY order_id, book_id ORDER BY id) AS rn
  FROM order_items
),
per_order AS (
  SELECT o.id AS order_id, o.created_at, SUM(b.qty) AS books
  FROM orders o
  JOIN clean b ON b.order_id = o.id AND b.rn = 1
  GROUP BY o.id, o.created_at
),
gaps AS (
  SELECT *, (julianday(created_at) - julianday(LAG(created_at) OVER (ORDER BY created_at))) * 24 AS hours
  FROM per_order
),
session AS (
  SELECT *, SUM(CASE WHEN hours IS NULL OR hours > 24 THEN 1 ELSE 0 END) OVER (ORDER BY created_at) AS session
  FROM gaps
)
SELECT MIN(created_at) AS window_start, MAX(created_at) AS last_order, COUNT(*) AS order_count, SUM(books) AS books
FROM session
GROUP BY session
ORDER BY session;
window_start last_order order_count books
2026-09-28 09:15:00 2026-09-28 13:02:00 2 3
2026-09-29 20:11:00 2026-09-30 08:40:00 2 5
2026-10-01 10:05:00 2026-10-02 12:00:00 3 4
2026-10-04 20:30:00 2026-10-07 14:45:00 5 6
2026-10-08 16:10:00 2026-10-09 10:30:00 2 3

Late data and watermarks

A stream processor can't wait forever before closing a window. It uses a watermark: an internal clock saying "every event before this time is taken to have arrived". The common form: the largest event time seen so far, minus a waiting time. A window closes and its result comes out as soon as the watermark passes the window's end. An event that arrives after its window has closed is late data, and by default it's dropped.

Remember Maya's order that arrives late. Guess first: counted by a stream, how many books does the week of 28 September come out with, 13 as in batch, or fewer? Here is that week's window (ending Monday 5 Oct 00:00), computed in the Lab the way Flink's event-time windows work:

Watermark waits Window comes out Holds Maya's order
0 hours Monday 5 Oct 11:20, when order 9's event arrives 12 books dropped: its window had closed when it arrived at 12:00
12 hours Tuesday 6 Oct 09:00, when order 10's event arrives 13 books counted

With no waiting time, the answer is 12: one book vanishes without an error. Waiting 12 hours makes it complete, but the result comes out a day later, because the watermark only moves when new events come, and Bookshop's orders are sparse. The daily batch had the complete 13 since Monday 00:00. Late events can also go to a separate path (a side output) to be fixed later instead of being dropped.

The owner's question needs no streaming

The owner reads "best seller per city per week" every Monday morning. The daily batch at 00:00 (the Airflow DAG from the previous lesson) has already loaded Sunday from the database, complete, Maya's order included, with a few seconds of work a day. Streaming for this question only adds Kafka, a processor that never stops, and watermark worries, for 14 orders in two weeks. Streaming earns its keep when a decision has to be made within seconds: refusing a fraudulent payment, locking stock during a flash sale, sounding an alarm when checkouts fail in a row.

Many teams use both. The Lambda architecture runs a batch path (complete) and a streaming path (fast) side by side, at the risk of two codebases that must produce the same numbers. The Kappa architecture uses streaming only, and recomputes by replaying old events from Kafka. Kafka is covered in the Kafka for Data Pipelines lesson, and real-time dashboards in the Real-time Analytics Pipeline lesson.

⚠️ Windows, watermarks and streaming nobody needed

TL;DR: Batch processes data that has piled up, on a schedule: simple, cheap, complete, but with latency from minutes to a day. Streaming processes each event as it arrives (Flink, Kafka Streams; micro-batch in Spark Structured Streaming): right when data must be processed within seconds, such as fraud detection and live dashboards, at the cost of always being on. Group by event time (created_at), not processing time. Tumbling windows don't overlap, sliding ones do, a session ends after a gap with no events (Bookshop: 5 sessions with a 24-hour gap). The watermark decides when a window closes: waiting 0 hours, Maya's late order is dropped and the week of 28 September comes out at 12 books; waiting 12 hours gives 13, a day later. The owner's weekly question is answered well by a daily batch. Kafka is in the Kafka for Data Pipelines lesson.

⚡ Batch or streaming

Meaning: Batch processes data that has piled up, on a schedule, say every night. Streaming processes each event as soon as it arrives. Streaming is the better fit when data must be processed within seconds, such as fraud detection or a live dashboard. When a few hours late is fine, batch is simpler and cheaper.

⚡ Event time, not processing time

Meaning: Group by event time (created_at). The event for Maya's order (Sunday 4 October 20:30) only arrives on Monday at 12:00. By processing time, her order moves to the week of 5 October.

⚡ Sessions from a running total

Write:

SELECT *, SUM(CASE WHEN hours IS NULL OR hours > 24 THEN 1 ELSE 0 END) OVER (ORDER BY created_at) AS session
FROM gaps

Meaning: Each order that comes more than 24 hours after the one before is marked 1. The running total of those marks becomes the session number: 5 sessions.

⚡ Watermarks and late data

Guess first: a watermark with no waiting time. How many books does the week of 28 September come out with, 13 as in batch, or fewer?

It's 12. The window had closed when Maya's order arrived, so her book is dropped without an error. Waiting 12 hours gives 13, but a day later.

Meaning: The watermark decides when a window closes. An event that arrives after that is late data.

What you will learn