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.

One pass through these 4 steps is one micro-batch. After the write, the driver goes back to step 1.
  • 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

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.

BatchRows it coversStarts atJob runs for
611 new rows23:01:002 s
79 new rows, rides 701 to 70923:01:102 s
812 new rows23:01:203 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()
)
batch_7
ride_idzonestatus
701SoMarequested
702Missionrequested
703SoMarequested
704Marinacancelled
705SoMarequested
706Missioncancelled
707Marinarequested
708SoMarequested
709Missionrequested

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?

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.