Sub topic 9
When not to autoscale: streaming and other limits
This chapter assumes you know Spark jobs, stages, and micro-batches. It also assumes you can read a Clusters API specification like the one in the previous chapters. Autoscaling adjusts the worker count between a minimum and a maximum. It works best for batch jobs whose load changes over time. In some workloads it adds cost or instability and gives little benefit.
Why streaming fits poorly
A Structured Streaming query runs one micro-batch after another, with no long idle gap between them. Autoscaling decides to remove workers by looking at idle capacity. A streaming query keeps a steady, small load, so the cluster rarely looks idle for long enough to scale down. It also may not scale up fast enough when a burst arrives. The result is a cluster that sits near its maximum, or one that resizes repeatedly.
For streaming, you have two practical options:
- Use a fixed-size cluster sized for peak throughput, with
num_workersset explicitly. - Use Lakeflow Spark Declarative Pipelines (formerly Delta Live Tables) with enhanced autoscaling, which is designed for streaming load.
A fixed streaming cluster looks like this:
{
"cluster_name": "stream-ingest",
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"num_workers": 4
}This cluster has no autotermination_minutes field, which matches a job that should run continuously. Terminating a streaming cluster stops the stream. Run it as a job cluster and let the job manage its lifecycle.
Other limits of autoscaling
Streaming is the most common case, but not the only one.
- Scale-up is not instant. New nodes take minutes to start. A job that finishes in two minutes may complete before extra workers arrive.
- Scale-down is cautious. Databricks removes workers only after they have been idle, and it considers shuffle data stored on them. Jobs with heavy shuffles often keep workers longer than you expect.
- Cached data is lost with removed workers. A workload that depends on a cached DataFrame can slow down after a scale-down.
- Runtimes become hard to predict. Service-level targets are harder to meet when the cluster size varies from run to run.
- Spot loss adds to the churn. Spot workers can be reclaimed while autoscaling also adds and removes nodes. Each change makes the runtime less predictable.
A quick check
Before you choose autoscaling, run the check from the parallelism chapter and see whether the cluster size is already a good match for the work:
total_cores = spark.sparkContext.defaultParallelism
shuffle_partitions = spark.conf.get("spark.sql.shuffle.partitions")
aqe_enabled = spark.conf.get("spark.sql.adaptive.enabled")
print(f"Default parallelism (approx. total cores): {total_cores}")
print(f"Shuffle partitions: {shuffle_partitions}")
print(f"Adaptive query execution enabled: {aqe_enabled}")If the load is steady and the numbers match the work, a fixed size is simpler and more predictable.
Exercise
A cluster runs a continuous Structured Streaming query with steady input. A teammate proposes autoscaling from 2 to 10 workers to save money. What do you recommend, and why?
Show answer
Use a fixed-size cluster sized for peak throughput, or move the stream to a pipeline with enhanced autoscaling. A continuous micro-batch load rarely leaves workers idle long enough to scale down, so classic autoscaling saves little and can cause resizing that disrupts the stream.