Sub topic 6
Picking a Starting Size for a Fixed Cluster
This chapter assumes you know Spark jobs, stages, tasks, partitions, and shuffles. It also assumes you can run notebooks and jobs in Databricks, and that you know what a cloud instance type is. By the end you will be able to turn a data volume and a node type into a first cluster size, and you will know why that size is only a hypothesis.
Why start with a fixed size
A fixed-size cluster has a set number of workers (num_workers). Nothing changes while it runs. That makes it the easiest setup to reason about and measure. Autoscaling, covered in the next chapter, moves the worker count during a run. You can't judge a range of sizes until you know what one size does with your workload.
Treat the number you pick here as a starting point. The goal is a size that is close enough to measure, not a size that is perfect.
The inputs to the estimate
Three quantities drive the estimate.
- Data volume. The size of the data the heaviest stage reads or shuffles, not only the size of the input table. A join or aggregation can shuffle more data than it reads.
- Target partition size. Spark works best when each partition is roughly 100 to 200 MB for typical scan and shuffle work. Much smaller partitions waste time on task overhead. Much larger ones risk spilling to disk or running out of memory.
- Cores per worker. Each core is one task slot. A worker with 4 cores runs 4 tasks at the same time.
The driver does not run tasks for your data transformations in the usual setup. Its cores do not count toward task slots. Only worker cores do.
A step-by-step estimate
Follow these steps to get a starting number.
- Estimate the data volume of the heaviest stage in MB. Say 400 GB, which is about 400,000 MB.
- Divide by a target partition size of 128 MB. That gives about 3,125 partitions.
- Decide how many "waves" of tasks you accept. A wave is one full round where every core runs one task. With 3,125 partitions and 400 cores, you need about 8 waves. Fewer waves mean a faster stage, but a bigger cluster.
- Pick a wave count that fits your time goal, then work backward to cores:
cores = partitions / waves. - Divide cores by cores per worker to get the worker count.
For example, suppose you want about 8 waves and use workers with 4 cores each. You need roughly 3,125 / 8 = 390 cores, which is about 98 workers. If that is too costly, accept more waves. At 25 waves you need about 125 cores, which is about 31 workers.
This shows the core trade-off. Total work stays the same. More workers shorten the run, and fewer workers lower the hourly rate. With a fixed amount of work, the total cost can stay about the same while the run time changes, as long as the job scales well.
Checking the memory side
Cores are only half of the estimate. Each task needs memory for its partition plus working space for joins, sorts, and aggregations.
A rough check is memory per core. Divide the worker's memory by its core count. For example, a node with 32 GB of RAM and 4 cores gives about 8 GB per core, though Spark does not make all of that available to tasks. The JVM, system overhead, and cached data all take a share.
If your partitions are 128 MB but the task needs several times that during a wide shuffle or a skewed join, a low memory-per-core ratio causes spill to disk or out-of-memory errors. In that case, choose a memory-optimized node type instead of adding more workers of the same type. Adding workers with the same ratio gives you more of the same problem.
| Symptom in the Spark UI | Likely cause | First change |
|---|---|---|
| Many tasks, long queue, low memory pressure | Too few cores | Add workers |
| Spill to disk on most tasks | Too little memory per core | Use a memory-optimized node |
| A few very slow tasks, others finished | Skew | Fix the data distribution, not the size |
| Cores idle during the stage | Too few partitions | Raise partition count or shrink the cluster |
Check what your cluster actually offers
Before you change anything, read what the cluster gives you. This cell prints the numbers the estimate relies on.
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}")defaultParallelism is usually the total worker core count, so compare it to your estimate. The shuffle partition setting tells you how many tasks a shuffle stage creates. Its default is 200, which is too low for a large shuffle and too high for a small one. With adaptive query execution (AQE) enabled, Spark can merge small shuffle partitions at run time. That reduces the cost of a too-high setting but does not fix a too-low one.
Writing the cluster down
Once you have a worker count, record it in a Clusters API specification. Here is a fixed cluster. The key difference from an autoscaling spec is num_workers in place of an autoscale block.
{
"cluster_name": "etl-fixed",
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"num_workers": 8,
"autotermination_minutes": 30
}Keep autotermination_minutes set even on a fixed cluster. A fixed cluster costs money for every minute it is up, including idle time.
Diagram: from data to workers
Edge cases and common mistakes
- Counting the driver's cores. The driver does not add task slots for DataFrame work. Size the driver separately, and only make it larger if you collect big results or run heavy driver-side code.
- Sizing from input size alone. A 50 GB table that explodes through a join can shuffle several hundred GB. Look at the largest stage in a previous run if you have one.
- Compressed input. Parquet files are smaller on disk than in memory. A 100 GB compressed dataset can need several times that in memory once decoded, so estimate from uncompressed volume where you can.
- Ignoring caching. If you cache a large DataFrame, that memory is not available to tasks. Add memory for the cache or accept spill.
- Too many small workers. Very large counts of small nodes raise shuffle network traffic and driver bookkeeping. A smaller number of larger nodes often shuffles faster. Very large nodes, though, make a lost node more expensive to recover from.
- Treating the estimate as final. Data grows and queries change. The estimate gives you a first run to measure.
What comes next
The fixed cluster gives you a baseline. The next chapter shows how autoscaling moves the worker count within a range, so a job that has uneven stages does not pay for peak size the whole time. The measurement habits that tell you whether your starting size was right are covered later, under monitoring and the tuning loop.
Exercise
A stage shuffles about 256 GB (about 256,000 MB). You target 128 MB partitions and accept about 10 waves. Workers have 8 cores each. Roughly how many workers do you need? Then, if the Spark UI shows heavy spill to disk on most tasks, what do you change first?
Show answer
Partitions = 256,000 / 128 = 2,000. Cores = 2,000 / 10 = 200. Workers = 200 / 8 = 25. Heavy spill on most tasks points to too little memory per core, so first switch to a memory-optimized node type (or reduce cores per worker) instead of only adding more workers of the same type.
Exercise
Why does a fixed cluster still need autotermination_minutes set?
Show answer
A fixed cluster is billed for every minute it is running, including idle time. Auto-termination stops it after the set idle period so you do not pay for unused capacity.