Sub topic 6
Picking a starting size for a fixed cluster
This chapter assumes you know Spark jobs, stages, tasks, partitions and shuffles, and that you can run notebooks on Databricks. The goal is a reasonable first guess for a fixed-size cluster, which you then refine with measurements in later chapters.
Start from tasks, not nodes
A cluster does work through task slots. Each core on a worker runs one task at a time. Total cores across workers set how many partitions Spark processes in parallel. Memory per core sets how large each partition can be before it spills to disk.
So sizing has two questions:
- How many cores keep the job's partitions busy in a reasonable number of waves?
- How much memory per core holds a partition plus the work done on it?
A starting recipe
- Estimate the input size that the heaviest stage reads or shuffles.
- Divide by a target partition size of about 128 MB to get the partition count. Shuffle-heavy stages often do better with 100 to 200 MB.
- Choose total cores so the stage finishes in a few waves. A wave is one round of tasks filling every slot. One to four waves is a common starting range.
- Pick a node type with enough memory per core (see the node types chapter), then divide total cores by cores per node to get the worker count.
For example, a job shuffles 400 GB. At 128 MB per partition, that is about 3,200 partitions. With 200 cores, that is 16 waves, which is long. With 800 cores, it is 4 waves. Reading that, you might start at 100 workers with 8 cores each for a deadline-driven job, or accept more waves on 25 workers (200 cores) if the job is not time-sensitive and cost matters.
Check what the cluster gives you
Run this in a notebook to see the parallelism you actually have, and whether the shuffle settings match your plan.
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}")Compare shuffle_partitions with your partition estimate. The default is 200. With 800 cores, 200 shuffle partitions leave 600 cores idle in that stage, unless adaptive query execution (AQE) adjusts the count at run time. With 8 cores and 3,200 partitions, the default is too low in the other direction, and each partition is large.
Common errors
- Sizing from the total data set. Size from the heaviest stage, not the table size. A 5 TB table filtered to 50 GB early needs a cluster for 50 GB.
- Ignoring memory per core. Many cores on a low-memory node cause spills. Wide joins and aggregations need more memory per core than simple scans.
- Too many small nodes. Shuffles move more data across the network when it is spread over many nodes. Fewer, larger nodes keep more of it local.
- Treating the first guess as final. It is a hypothesis. Run the job, then read the Spark UI.
Exercise
A stage shuffles 192 GB. You target 128 MB partitions and want about 4 waves on workers with 8 cores each. Roughly how many partitions, total cores, and workers do you need?
Show answer
192 GB is about 196,608 MB, so roughly 1,536 partitions at 128 MB. With 4 waves, you need about 1,536 / 4 = 384 cores. At 8 cores per worker, that is 48 workers. Also check that shuffle partitions is set near 1,536 or that AQE is enabled.