Sub topic 4
Cores, memory, and partitions
This chapter assumes you know Spark jobs, stages, tasks, partitions, and shuffles, and that you can run a notebook on a Databricks cluster. It explains how cluster cores and memory relate to partitions, which is the basis for choosing a cluster size.
Cores are task slots
Spark runs one task per core at a time. A cluster with 4 workers of 8 cores each has 32 task slots. A stage with 200 tasks runs in about 200 / 32, or 7, waves.
Memory is shared by the tasks running on the same executor. With 64 GB on a worker and 8 concurrent tasks, each task gets roughly 8 GB of that share, minus overhead for the JVM and Spark's own storage.
Partitions set the work units
A partition is the unit of data one task processes. The partition count decides how much of the cluster a stage can use.
- Fewer partitions than cores leaves cores idle.
- Very many tiny partitions waste time on task scheduling.
- Very large partitions use a lot of memory per task and can spill to disk or fail.
A common target is partitions of about 100 to 200 MB, with a partition count that is a small multiple of the total cores. Both extremes show up in the Spark UI, covered in a later chapter.
Checking your settings
This cell shows the slots available and the shuffle settings in effect.
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 approximates the total cores. spark.sql.shuffle.partitions is the number of partitions after a shuffle, 200 by default. With adaptive query execution (AQE) enabled, Spark can coalesce small shuffle partitions at run time. This makes the default 200 less of a problem for small data, but it does not replace sizing the cluster for large data.
Worked example
A job reads 400 GB and shuffles it. At about 128 MB per partition, that is roughly 3,200 partitions. On a cluster with 64 cores, this runs in 50 waves. Doubling to 128 cores halves the waves, if the work is evenly spread and the cluster is the bottleneck. If you see only 200 shuffle partitions, each holds about 2 GB, which risks spilling regardless of cluster size.
Common errors
- Adding workers without raising the partition count, so the new cores sit idle.
- Leaving
spark.sql.shuffle.partitionsat 200 for very large or very small shuffles. - Ignoring skew. One huge partition keeps a single core busy while the rest finish early, and more cores will not help.
- Choosing nodes with many cores but little memory per core for memory-heavy work.
Exercise
A stage has 40 tasks and runs on a cluster with 160 cores. You double the cluster to 320 cores. Does the stage get faster, and why?
Show answer
No. The stage has only 40 tasks, so at most 40 cores are used even on the original cluster. To benefit from more cores, the stage needs more partitions, for example by repartitioning or raising the shuffle partition count.