Batch or stream processing
easy 26 minBatch processing collects a finite set of data and handles it all at once.
Video lesson · Chapter 1 of 5
One ledger, two clocks
Batch or stream in 75 seconds
1:15Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
There are two ways to work through data. Batch processing waits until a piece is complete, such as one day of payments, and then runs over all of it. The job starts, finishes and reports. Stream processing starts with the first event and keeps going. It updates its answer with every event or every few seconds.
The first works on bounded data: the job can see where it ends. The second works on unbounded data: there is always more to come. Nearly every other difference follows from this one.
Why it exists
Handling many records together is cheap, because fixed costs such as opening files, starting tasks and committing are shared. Having all the records also makes the answer complete. That is why batch has been the normal way for a long time.
But a batch answer is old when it arrives. Where a decision depends on the last minute, such as stopping a stolen card, the value is gone by morning. Streaming buys freshness and pays with complexity.
A short history
2004
Google publishes MapReduce at OSDI: batch jobs over very large files on many ordinary machines.
2013
The Discretized Streams paper at SOSP shows how to run a stream as a series of small batch jobs, the idea behind Spark Streaming. In the same year Google publishes MillWheel, which works record by record.
2015
Google's Dataflow Model paper describes one model for bounded and unbounded data. Apache Beam, an open source unified model for batch and streaming, follows this line.
2018
Spark 2.3 adds an experimental mode that processes records continuously beside its small batches.
Words to know
- Bounded
- With a known end, such as the files of one day.
- Unbounded
- Without an end, such as a topic of events.
- Latency
- How long until the answer is there.
- Throughput
- How much is handled per second.
- Staleness
- How old the data behind an answer is.
- Micro-batch
- A very small batch, run again and again.
- Backfill
- Running a job again over old data.
- Schedule
- The plan of when a batch job starts.
How it works
Step through the idea, or press Play.
Batch. Collect, process, one result
Understand it
Start here
Laundry day and the cup
At Sam's home, shirts and cups are both washed, but not in the same way.
Nobody washes one shirt at a time, and nobody waits a week for a clean cup.
The idea
Wait, or go now
Batch
Batch processing alone
Collect all day, run once
Good at
- High throughput: terabytes in one run
- Simpler to write and debug
- Complete data, so accurate answers
- Easy to run again over old data
- Cheap for large sets
Weak at
- Answers come hours or days later
- Data is old by then
- Cannot react to one event
- Uses many machines for a short time
- A failure late in a long run costs hours
Typical schedules and tools
| When | Job | Tools |
|---|---|---|
| Daily at 02:00 | Load and transform into a warehouse | Spark, or SQL in the warehouse |
| Every hour | Totals per hour | Spark, Trino |
| Weekly | Training a model | Spark, Python jobs |
| Monthly | Financial reports | SQL, Hive |
A batch run
How old can a batch answer be?
Stream
Stream processing alone
Always running
Good at
- Answers in milliseconds to minutes
- Reacting to one event
- Running continuously
- Finding unusual behaviour at once
- Work spread over the day
Hard
- More difficult to write and test
- Events arrive out of order
- State has to be managed
- Exactly-once results need care
- Somebody has to run it day and night
Tools and how they work
| Tool | How |
|---|---|
| Apache Flink | Handles records one by one, keeps state. Can also run bounded jobs in a batch mode |
| Apache Kafka | Keeps the events. Kafka Streams is a library that processes them |
| Spark Structured Streaming | Small batches by default, as low as 100 milliseconds end to end in its guide |
| Apache Storm | An earlier record-by-record system |
The same payment on two roads
Cost
Latency against throughput
Each write, file or task has a fixed cost. Sharing it over many records makes each record cheap, but the first record has to wait for the last one.
Records per second
Compare
Side by side
| Aspect | Batch | Stream |
|---|---|---|
| Data | Bounded | Unbounded |
| Latency | Minutes to days | Milliseconds to minutes |
| Runs | On a schedule | Continuously |
| Throughput per machine | Very high | High, with some overhead per event |
| Complexity | Lower | Higher |
| State | Computed in the run, then gone | Kept and updated for months |
| Late data | Included if it arrives before the run | Watermarks, corrections |
| Repair | Run again over the files | Replay the log, if it is still there |
| Machines | Many for a short time | Few all the time |
In between
Micro-batches and hybrids
Between the two ends there is a wide middle. A batch every five minutes is enough for many dashboards. It keeps batch's simple model and cuts the waiting.
| Style | Answer after | Effort |
|---|---|---|
| Nightly batch | up to a day | Lowest |
| Hourly batch | about an hour | Low |
| Micro-batch | seconds to minutes | Medium |
| Record by record | milliseconds to seconds | Highest |
If you need both a quick and an exact answer, there are two well-known shapes. Lambda architecture runs a batch path and a stream path side by side. Kappa architecture keeps only the stream and replays the log. Apache Beam and Flink also let one program run over bounded and unbounded data.
Repair
Running it again
A bug was found
Decide
Which one?
Three questions
Batch when
- You analyse history
- Accuracy needs all the data
- Volume matters more than speed
- Results can wait hours
- You train models
- You load a warehouse
Stream when
- Insight is needed now
- Something must react
- You monitor continuously
- Fraud and unusual events
- Live dashboards
- Services react to events
Both in one company: an online shop, as an example
| Part | Batch | Stream |
|---|---|---|
| Recommendations | Train the model at night | Use the last clicks |
| Delivery | Drivers' monthly pay | Arrival time on the map |
| Payments | Reports for sellers | Fraud check |
| Search | Weekly quality report | Stock changes in the index |
Practice
Good habits
Start from the decision
Choose by the latency somebody really needs.
Plan the repair
Know how you would recompute before you launch.
Measure both
Throughput and latency. One often hides the other.
Do not force real time
If batch is enough, streaming only adds late events, backpressure and night shifts.
Summary
The whole topic in one minute.
Batch collects, then processes everything once: complete, simple to repeat, cheap per record, and late. Stream processes as events arrive: early, steady, and harder to run because of late data, state and replays.
Choose by the decision that depends on the answer. If nobody acts for hours, use batch. If minutes are fine, use smaller batches. If seconds matter, stream, and keep the raw data so that a batch job can produce the exact number.
- Bounded data: batch. Unbounded data: stream.
- Large groups share fixed costs, and the first record waits.
- A batch answer can be as old as the interval plus the run time.
- Streaming adds state, late events and replays.
- Smaller batches solve many freshness problems.
- Keep raw data, and make every job safe to repeat.