Sub topic 15
Tuning throughput, partitions and state
This chapter assumes you can run a streaming query with a checkpoint and a trigger, and that you know how watermarks bound state. It covers three levers: how much data each batch reads, how many shuffle partitions process it, and how large the state grows.
Start with the metrics
Tune from evidence. In lastProgress, compare inputRowsPerSecond (arrival rate) with processedRowsPerSecond (what the query handles). If processing is slower than arrival, the query falls behind. Also check batchId duration and the stateOperators entries.
Control batch size
An unbounded first batch can read the whole backlog and fail with out-of-memory errors or long shuffles. Cap each batch with a source option.
| Source | Option |
|---|---|
| Auto Loader | `cloudFiles.maxFilesPerTrigger`, `cloudFiles.maxBytesPerTrigger` |
| Delta table | `maxFilesPerTrigger`, `maxBytesPerTrigger` |
| Kafka | `maxOffsetsPerTrigger` |
Smaller batches lower memory pressure and latency per batch. Larger batches reduce per-batch overhead and write fewer small files. A good starting point is a batch that finishes well inside the trigger interval.
df = (
spark.readStream.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/demo/chk/orders_schema")
.option("cloudFiles.maxBytesPerTrigger", "512m")
.load("/Volumes/main/demo/landing/orders")
)maxBytesPerTrigger is a soft limit. A batch can exceed it slightly.
Match shuffle partitions to the cluster
Stateful operators such as aggregations and joins shuffle data into spark.sql.shuffle.partitions partitions. Each partition holds its own slice of state. Too many partitions create tiny tasks and many small state files. Too few create large tasks and memory pressure.
As a rule of thumb, use one to two partitions per core.
Keep state small
State grows with the number of distinct keys and with how long you keep them. To limit it:
- Set the shortest watermark your data allows. A 10-minute watermark keeps state for roughly 10 minutes of late data.
- Group by fewer or lower-cardinality keys when you can.
- Add a time bound to stream-stream joins and
dropDuplicatesWithinWatermarkso old keys can be removed. - Use the RocksDB state store for large state. It keeps state off the JVM heap, which avoids long garbage collection pauses.
spark.conf.set(
"spark.sql.streaming.stateStore.providerClass",
"com.databricks.sql.streaming.state.RocksDBStateStoreProvider",
)Set this before starting the query. As with partitions, you can't switch state store providers on an existing checkpoint.
Check state growth in the progress report:
progress = query.lastProgress
for op in progress["stateOperators"]:
print(op["numRowsTotal"], op["memoryUsedBytes"])If numRowsTotal rises without leveling off, the watermark or time bound is not removing old keys.
Add compute last
If batches are sized, partitions fit, and state is bounded, then add cores. Throughput of a stateful query scales with its partition count, so more cores than partitions give no benefit.
Exercise
A stateful aggregation has run for a month with the default of 200 shuffle partitions. You raise spark.sql.shuffle.partitions to 800 and restart with the same checkpoint. Why does the query still use 200 partitions, and what must you do to use 800?
Show answer
The number of state partitions is recorded in the checkpoint, so the existing state keeps its original layout. To use 800, start the query with a new checkpoint location and rebuild the state, for example by reprocessing from the source.