Part 1 of 6 · Processing data: batch and stream
- Batch vs stream processing, explained with a simulation
- Batch or stream: which one fits your problem?
- Stream windows, event time and late data, explained
- Data processing tools: what to choose by language and cloud
- Build a cheap data pipeline with TypeScript on Cloudflare or AWS
- Build batch pipelines with Spark and stream with Kafka, Flink
In short
- Batch processes a closed set of data on a schedule, then stops. It is cheap and easy to get right, but results come late.
- Stream handles each event as it arrives and runs around the clock. Results are fast, but it costs more and is harder to get right.
- The question that decides: if the result were an hour late, would anyone lose out? If not, start with batch.
The same flow of data can be handled in two ways: collect it and process it at once, or process it the moment it arrives. You meet both every day:
- Batch is the monthly statement. The bank collects a month of transactions, computes once and sends you one statement.
- Stream is the balance alert. Tap your card and a message arrives seconds later, because the system handled that one transaction as it happened.
Try it: how do the two approaches differ?
Both lanes below receive the same orders from an online shop. The batch lane waits for its beat and processes a whole run. The stream lane handles each order as it arrives.
Two lanes process the same flow of orders. Move the sliders and press the buttons to see what each approach costs.
Try these in order:
- Move "Batch beat" from 8 to 20 seconds. Batch's delay grows with the beat, while stream's stays the same.
- Press "Burst of 30 orders". Stream has to queue because each order takes its own slice of time. Batch takes the whole burst into its next run and handles it more neatly.
- Watch "Machine switched on". Batch only runs during a run. Stream stays on to wait for orders.
- Move "Orders arriving" up to 12 per second. Stream can no longer keep up and its queue grows without end.
What is the root difference between batch and stream?
The root difference is the shape of the data. Batch works on bounded data with a known end, such as all orders from 25 October. Stream works on unbounded data that has no known end.
How does batch processing work?
Batch processes a set of data that has already been collected. A job starts, reads everything for the period, computes, writes the result and stops. The next run starts over with new data.
- Data sourceDatabase, files, API
- Data lakeS3, GCS, Parquet
- Scheduled jobSpark, dbt
- Data warehouseBigQuery, Snowflake
- Reports, MLBI, model training
What defines batch:
- Bounded data. A clear start and end, such as all of yesterday's transactions.
- Runs on a schedule. Nightly, hourly or on request. Between runs the machines can be off.
- High throughput. Reading and computing large blocks is very efficient.
- High latency. Results come minutes or hours later, or the next day.
- Easy to re-run. Fix the logic and run again on the same data. An idempotent job gives the same result every time.
Examples: payroll, card statements, utility bills, daily revenue reports, payment reconciliation, training a model on a year of data.
How does stream processing work?
Stream treats data as a flow of events with no end. Each event, such as a card tap or a sensor reading, is handled as it arrives, usually within milliseconds to seconds. The application runs continuously and remembers state between events.
- Event sourceApps, IoT, logs
- Message brokerKafka, Pulsar, Kinesis
- Stream processorFlink, Kafka Streams
- DestinationDatabase, ClickHouse
- ActionAlerts, dashboards
What defines stream:
- Unbounded data. Events keep arriving, with no known end.
- Runs around the clock. Machines stay on to wait for events.
- Low latency. Results in milliseconds to seconds.
- Stateful. Counters, totals and sessions are kept between events.
- Harder to get right. You have to handle event order, late data, failures partway through and duplicates.
Examples: blocking suspected card fraud, driver locations on a map, alerts when errors spike, a dashboard for a big sale day, monitoring factory sensors.
Batch vs stream at a glance
| Aspect | Batch | Stream |
|---|---|---|
| Data | Bounded, closed before processing | Unbounded, keeps arriving |
| When it runs | On a schedule or on request | Continuously |
| Result delay | Minutes to hours | Milliseconds to seconds |
| Throughput | Very high, built for large blocks | High, but each event has its own cost |
| Machine cost | On while running, off when done | Always on, with headroom for peaks |
| Complexity | Lower, easy to check | Higher: state, order, late data |
| On failure | Re-run the job, if it is idempotent | Recover from a checkpoint and offset |
| Completeness | Sees all of the period's data | Updates gradually, may correct for late data |
Common misunderstandings
- Stream is not always better. It keeps machines running and is harder to get right. If people read a report each morning, batch is cheaper and simpler.
- There is a middle ground: micro-batch. Spark Structured Streaming splits the flow into very small batches by default. Flink handles each event. Both are called streaming.
- Real time comes in several grades. A dashboard refreshed each minute, an alert within a second and fraud blocking within 100 milliseconds are three different requirements at three different costs. Ask what delay is acceptable before choosing a technology.
The simulation above is a simplified model that shows the trend. It is not a measurement of a real system.
Check what you learned
Pick an answer to see why it is right or wrong.
Your banking app messages you right after you tap your card. Which approach is that?
Show the answer
Stream, because each transaction is handled as it happens. The message comes from a single transaction event and arrives within seconds, which is what stream does. The monthly statement is batch.
What does unbounded data mean?
Show the answer
Data that keeps arriving, with no known end. Unbounded is about having no end, not about size. A 10 TB file is still bounded.
In the simulation, orders arrive faster than they can be handled. What happens to the stream lane?
Show the answer
The queue grows and the delay keeps rising. Each order takes its own slice of time. When orders arrive faster than that, the queue grows. In real systems you add machines or partitions, and that costs more.