Micro-Batches
Concepts covered: Structured Streaming
A Spark job has a beginning and an end: read partitions, run tasks, return a result. A stream has no end, so one job can never cover it. Spark closes the gap by cutting the stream into slices and running one small job per slice. Each slice is called a micro-batch.
The loop the driver runs
Once a streaming query starts, the driver repeats the same 4 steps for as long as the query lives.
- Ask the source what is new (source, driver)
- Fix the batch's row range (transform, driver)
- Run one batch job (transform, executors)
- Write the output (storage, sink)
- Ask the source what is new -> Fix the batch's row range
- Fix the batch's row range -> Run one batch job
- Run one batch job -> Write the output
What each step does
- Ask the source: where does your data end now? Anything past the previous batch's end is new.
- Fix the range: the driver records which rows this batch covers before any task runs.
- Run the job: executors process only those rows, using the plan Catalyst produced.
- Write the output: the result goes to the sink and the batch is complete.
Batch 7, up close
Batches are numbered from 0. Here are 3 consecutive batches from the ride stream on a busy evening, with a batch starting every 10 seconds.
| Batch | Rows it covers | Starts at | Job runs for |
|---|---|---|---|
| 6 | 11 new rows | 23:01:00 | 2 s |
| 7 | 9 new rows, rides 701 to 709 | 23:01:10 | 2 s |
| 8 | 12 new rows | 23:01:20 | 3 s |
Batch 7 reads 9 rows and nothing else. The rows that batches 0 to 6 covered are already accounted for, and no later batch reads them again. A batch's size is set by how many rows arrived since the last one, and the age of the stream has no effect on it.
Inside the batch, everything is the Spark you know. The 9 rows sit in 2 input partitions, so the job opens with 2 tasks. The filter is narrow and drops the 2 cancelled rides in place, leaving 7. The groupBy is wide, so those rides cross a shuffle keyed on zone into 3 shuffle partitions, where the counts are finished. The animation plays that job, with collect() standing in for the write to the sink.
result = (
batch_7
.where("status = 'requested'")
.groupBy("zone")
.count()
.collect()
)| ride_id | zone | status |
|---|---|---|
| 701 | SoMa | requested |
| 702 | Mission | requested |
| 703 | SoMa | requested |
| 704 | Marina | cancelled |
| 705 | SoMa | requested |
| 706 | Mission | cancelled |
| 707 | Marina | requested |
| 708 | SoMa | requested |
| 709 | Mission | requested |
For batch 7's own rows the job returns SoMa 4 and Mission 2, with Marina at 1. A standing count has to cover the whole table, so Spark carries each zone's total forward from batch to batch and adds the new counts to it. If SoMa stood at 31 after batch 6, it stands at 35 after batch 7, which is what a batch query over every row so far would return. How Spark stores those carried totals is a later lesson's subject.
Batch 7 covered 9 rows and finished. 12 more rows arrive before batch 8 starts. Which rows does batch 8's job read?
- Only the 12 rows that arrived after batch 7's range ended.
- All 21 rows: the 9 from batch 7 plus the 12 new ones.
- Every row since the query started, so the totals can be rebuilt.
- Enough rows to fill 3 shuffle partitions, with the rest left for batch 9.
Batch costs carry over
Because a micro-batch is a regular job, your batch knowledge transfers whole. A groupBy in the query means a shuffle in every batch, paid again each time. A join that is not broadcast shuffles both sides in every batch too. A join to a small lookup table, such as a list of zones under the 10MB broadcast threshold, gets broadcast instead, so it adds no shuffle. A hot key skews batch 7 the way it would skew a nightly job. And every batch pays a fixed cost before any row moves, since the driver has to plan the job and schedule its tasks. For 9 rows that overhead outweighs the work.
The Spark UI shows this directly. Each micro-batch appears in the Jobs tab as its own job, and the job description carries the batch number, such as batch = 7. Open it and you find the same stage and task breakdown, with shuffle sizes, that you would inspect for any slow job. A slow stream is a sequence of batch jobs that each take too long, and you find the slow stage the way you already know.
So a streaming query is a loop: find the new rows, fix the range, run a job, write the result. The driver asks one end of that loop for new rows and the job writes to the other. Each end has its own name and its own requirements.
About this lesson
This section is part of the Structured Streaming: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.