Backpressure
Concepts covered: Structured Streaming
By default a micro-batch reads everything the source has received since the last committed offset. While input is steady that is a normal-sized batch, but after an outage or a spike the next batch inherits the whole backlog at once. One giant batch shuffles more data than the executors can hold, so it spills to disk or fails. Because a batch commits all or nothing, a failure discards the work and Spark retries the same giant batch.
Structured Streaming leaves the intake rate to you: you set a ceiling on how much each trigger may read, and each source spells it differently:
| Source | Option | What it caps |
|---|---|---|
| Files | `maxFilesPerTrigger` | New files read per batch |
| Offset-based message log | `maxOffsetsPerTrigger` | Offsets per batch, split across the log's partitions by backlog |
| Offset-based message log | `minOffsetsPerTrigger` with `maxTriggerDelay` | A minimum batch size, waited for up to a time limit, since Spark 3.3 |
| Rate, for testing | `rowsPerSecond` | Rows generated each second |
request_files = (
spark.readStream
.format("parquet")
.schema(request_schema)
.option("maxFilesPerTrigger", 200)
.load("/landing/ride_requests")
)Pricing the catch-up
Take the request stream from the message log, 3,000 rows a second on a 10-second trigger. A normal batch carries 30,000 rows and finishes in 6 seconds, about 0.2 milliseconds a row. A 30-minute outage leaves 5.4 million rows waiting, so the first uncapped batch after the restart is 180 times normal size. With maxOffsetsPerTrigger at 150,000, each batch looks like this:
| Each capped batch | Value |
|---|---|
| Rows read | 150,000 |
| Duration at 0.2 ms a row | 30 seconds |
| New rows arriving meanwhile | 90,000 |
| Backlog drained | 60,000 |
| Batches to clear 5.4 million | 90 |
- 45 min
Every batch in that stretch is a normal batch scaled by 5, so its memory and shuffle needs stay predictable, spill included, and a failure costs 30 seconds of work. The price is latency: a row near the end of the backlog waits out the whole catch-up before it is written.
Choosing the number
Two limits box the cap in. From below, it must be larger than a normal batch: at 30,000, the stream reads exactly what arrives in each 10-second trigger and the backlog never shrinks. From above, it must stay under the largest batch your executors finish without spilling, where the stage page shows zero in its spill columns, and under the batch duration your consumers can wait for. If the Structured Streaming tab shows input rate above process rate for hours with no outage behind it, no cap helps: the cluster is too small for the stream, and only more executors or a cheaper query close the gap.
A stream averages 3,000 rows per second on a 10-second trigger, and each row costs about 0.2 milliseconds. After an outage, which maxOffsetsPerTrigger value never clears the backlog?
- 150,000, because each batch then runs longer than the 10-second trigger.
- 60,000, because each batch then takes 12 seconds, longer than the trigger.
- 30,000, because each batch reads only what arrives in one trigger interval.
- 300,000, because batches that large always spill and never finish.
The spark.streaming.backpressure.enabled setting belongs to Spark's older streaming API and changes nothing in Structured Streaming.
The cell splits a message table into capped batches in message order. Set cap to the rows you would allow per trigger and compare how many batches result and which messages each one covers.
About this lesson
This section is part of the Structured Streaming: Advanced 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.