Data engineering

Spark cluster sizing calculator

Pick a cluster shape or an EC2 instance type and get the executor, memory, driver and shuffle-partition settings that actually fit on it — with the arithmetic shown beside every number, so you can argue with it.

Runs entirely in your browser — nothing is uploaded

Your cluster

10 × r5.4xlarge = 160 vCPU and 1,280 GiB of raw capacity. 8 GiB per vCPU. Wide joins, big shuffles, cached DataFrames.

Assumptions — change these if your cluster is not a stock YARN setup

Recommended configuration

Usable / node15 cores · 117 GiB
Executors29
Task slots145
Heap in flight1015 GiB
Core use93%
RAM use91%

Each node keeps 1 core and 11 GiB for the operating system, the YARN NodeManager and (on EMR) the instance agent. That leaves 15 cores and 117 GiB per node for Spark.

spark.executor.cores

5

5 cores packs 3 executors into the 15 usable cores on each node with nothing left over. Five is the usual ceiling: one JVM reading from HDFS or S3 stops gaining throughput past about five concurrent tasks, and every extra task shares a single heap and a single garbage collector.

spark.executor.instances

29 (3/node)

3 executors per node × 10 nodes = 30 container slots, minus one for the ApplicationMaster/driver in cluster mode, so ask YARN for 29.

spark.executor.memory

35g

117 GiB of usable node memory ÷ 3 executors = 39 GiB per container. That container holds the heap and the off-heap overhead together, so the heap is budget ÷ (1 + 0.1) rounded down to a whole GiB — 35g.

spark.executor.memoryOverhead

3584m

max(384 MB, 0.1 × 35g) = 3.5 GiB. This is JVM metaspace, thread stacks, the shuffle and Netty buffers, and any Python worker processes — none of it comes out of the heap. Running out of it is the classic "Container killed by YARN for exceeding memory limits".

YARN container size

39 GiB

35g heap + 3.5 GiB overhead = 38.5 GiB, which YARN rounds up to 39 GiB because container sizes are allocated in whole-GiB increments. 3 of those fit in 117 GiB of usable node memory, leaving 1.5 GiB per node that no executor asks for.

spark.driver.memory / cores

16g · 4 cores

The driver plans queries, tracks every task and holds the broadcast relations, but it does no partition work — 4 cores is plenty. The executor heap is 35g, but the driver is capped at 16g: past that it is usually collecting data it should be writing out instead. In cluster mode it runs in a YARN container on one of these nodes: 17.6 GiB of the 39 GiB slot it takes, leaving 21.4 GiB of that slot idle. Raise it if you collect() results, broadcast large dimension tables, or run many queries concurrently through one session.

Total parallelism

145 task slots

29 executors × 5 cores = 145 task slots. That is the maximum number of tasks Spark can run at once, and the number every partition count should be reasoned against.

spark.sql.shuffle.partitions

16,095

2000 GiB ÷ 128 MB per partition ≈ 16000 partitions, rounded up to 16095 so it is an exact multiple of the 145 task slots. Each core then runs 111 full waves of ~127 MB tasks instead of finishing with a ragged tail.

spark.default.parallelism

290

Two tasks per core (145 × 2 = 290) for the RDD API and for the initial read when Spark cannot infer a better split. Slightly over-subscribing hides stragglers without adding much scheduling overhead.

What to watch out for

  • Note21.4 GiB stranded in the driver’s slot

    The driver takes a whole 39 GiB slot on one node but only asks for 17.6 GiB of it, because the driver heap is capped at 16g. The remaining 21.4 GiB on that node is too small to hold another 39 GiB executor, so it stays idle for the whole run — the RAM figure counts it as committed, because that is what the cluster gives up for the driver. Client mode hands it back.

  • WarningHeap is at or above the 32 GiB compressed-oops boundary

    At 35g the JVM turns off compressed ordinary object pointers, so every object reference grows from 4 bytes to 8 — you can genuinely end up with less usable memory than a 30 GiB heap. Full GC pauses get longer too. On memory-rich instances the vCPU-to-RAM ratio forces this; cap spark.executor.memory near 30g and hand the remainder to spark.memory.offHeap.size, or move to a more balanced instance family.

  • NoteRunning PySpark?

    Python workers are separate processes that live entirely outside the JVM heap and are charged to the container overhead. For pandas UDFs or heavy PySpark, raise the overhead factor to 0.15–0.25 — this is the single most common cause of containers being killed on EMR.

  • Note16000 partitions is not a multiple of 145 cores

    The raw estimate ends on a wave of 50 tasks with 95 cores idle — about 0.59% of the run. Rounding up to 16095 fills the last wave and costs little, because the partitions only shrink from 128 MB to ~127 MB.

  • NoteAdaptive query execution changes the job

    On Spark 3.2+ AQE is on by default and will coalesce these partitions at runtime based on the real shuffle output. Treat spark.sql.shuffle.partitions as the upper bound and let spark.sql.adaptive.advisoryPartitionSizeInBytes do the fine tuning.

  • NoteEMR turns dynamic allocation on by default

    When spark.dynamicAllocation.enabled is true, --num-executors is ignored and Spark scales executors itself. Set it to false to make these numbers take effect, or keep it on and set the min/max executor bounds from them instead.

Copy it into your job

spark-submit \
  --master          yarn \
  --deploy-mode     cluster \
  --driver-cores    4 \
  --driver-memory   16g \
  --num-executors   29 \
  --executor-cores  5 \
  --executor-memory 35g \
  --conf            spark.executor.memoryOverhead=3584m \
  --conf            spark.driver.memoryOverhead=1639m \
  --conf            spark.sql.shuffle.partitions=16095 \
  --conf            spark.default.parallelism=290 \
  --conf            spark.sql.adaptive.enabled=true \
  --conf            spark.sql.adaptive.coalescePartitions.enabled=true \
  --conf            spark.sql.adaptive.advisoryPartitionSizeInBytes=134217728 \
  --conf            spark.dynamicAllocation.enabled=false \
  your_job.py

These are starting points, not universal truths. They assume a YARN cluster, whole-GiB container increments and a shuffle roughly the size of your input. Run the job, read the Spark UI, and move the numbers.

How this calculator works

Spark sizing fails in two familiar ways. Someone divides a node's RAM by the executor count, forgets the JVM heap is not the whole container, and YARN kills the job at 90% with Container killed by YARN for exceeding memory limits. Or spark.sql.shuffle.partitions stays at 200 on a 600-core cluster and most of it idles through every shuffle. This is the arithmetic I kept re-deriving while tuning Spark on EMR and Databricks.

Reserve first, then divide

A node's advertised specification is not what YARN can hand out. The operating system, the NodeManager and, on EMR, the instance agent take cores and memory first, so the model reserves one core and 8% of RAM per node — floored at 1 GiB, capped at 16 GiB. That 8% is a convention, not a reading of your cluster, and EMR is markedly more conservative on smaller nodes: an m5.2xlarge has 32 GiB, of which 8% leaves 29 GiB, while EMR's default yarn.nodemanager.resource.memory-mb hands out 24,576 MB. Read that property off your own cluster, subtract it from the node's RAM, and type the difference in as the reservation — every number downstream moves with it.

Why five cores

spark.executor.cores is how many tasks share one JVM. Around five, one HDFS or S3 client stops gaining throughput from more concurrency while GC, lock contention and the blast radius of losing an executor keep growing. Five is a ceiling, not an answer: the model starts there and drops to four or three only when five would strand more than 10% of a node's cores, and never below three on a node with six or more, because narrow executors duplicate broadcast data and lose intra-executor parallelism.

Memory is the second constraint on how many executors fit, but a deliberately weak one: the model only shrinks the count when a node cannot give every container the cores would have allowed a whole 1 GiB YARN allocation increment. At the default reservation that takes a node with under roughly a third of a GiB of RAM per vCPU — some six times leaner than a c5, and leaner than anything in the table below. No catalogue instance trips it, so the packing you see there is decided entirely by cores. On a hand-entered shape that does trip it, the model drops to the executor count memory allows and widens each one back out toward the target.

The memory formula

overhead = max(384 MB, factor × executorMemory) // factor defaults to 0.10
container = executorMemory + overhead

⇒ factor branch — when factor × container ÷ (1 + factor) ≥ 384 MB
(at factor 0.10, that is every container ≥ 4224 MB)

executorMemory = container ÷ (1 + factor)
rounded down to a whole GiB — or to a 128 MB
step when the quotient is under 2 GiB

⇒ floor branch — when it lands under 384 MB, the factor is moot

executorMemory = container − 384 MB
rounded down to a 128 MB step

The container is what YARN allocates; the heap is only part of it, and solving backwards from the container budget is the step people skip. Which branch you land in matters more than it looks. The 384 MB floor dominates small containers — nearly 40% of a 1 GiB one — which is why tiny executors waste so much, and once the floor binds the overhead cannot grow, so rounding the heap to a whole GiB would strand up to 1 GiB of the container that nothing could ever claim. That is why the floor branch rounds in 128 MB steps instead: a 3 GiB container becomes 2688m of heap plus 384 MB of overhead, filling it exactly, rather than 2g plus 384 MB and 640 MB of dead space. The c5.large row in the table below is this case. YARN rounds requests up to a whole allocation increment, so the budget itself is trimmed to a whole GiB first; otherwise containers that each "fit" get rounded up and no longer do.

Partitions come from the data

Shuffle partitions are sized from the data you are moving: 128–200 MB each, rounded up to a whole multiple of the task slots so the last wave is full rather than a few tasks holding the stage open. That rounding is cheap when the job runs many waves, because it only shaves a few MB off each partition. It is not cheap when the data does not fill even one wave — rounding 8 partitions up to 145 slots multiplies the task count 18-fold and leaves ~7 MB per task, and the calculator says so rather than pretending the rounding was free. Since Spark 3.2, adaptive query execution coalesces them at runtime, so treat the number as an upper bound and let spark.sql.adaptive.advisoryPartitionSizeInBytes tune it.

What the model does not know

It does not know your join skew, whether you cache DataFrames, or that one key holds 40% of the rows. It assumes YARN, whole-GiB increments, a shuffle roughly the size of the input, and no off-heap allocation beyond the overhead. Every number is a starting point to take into the Spark UI: read the task duration spread, the spill columns and GC time, then move one setting at a time.

Starting points by EC2 instance type

Derived by this calculator for a ten-node cluster in cluster deploy mode, reserving one core and the default memory slice per node. m5 gives 4 GiB per vCPU, r5 gives 8 and c5 gives 2, and that ratio does not change the shape of the packing at all: at any given vCPU count all three families land on the same executor.cores and the same executors per node — 16 vCPU gives 5 cores and 3 executors on m5.4xlarge, r5.4xlarge and c5.4xlarge alike. What the ratio changes is the heap inside each executor: 17g, 35g and 8g respectively. A c5 does not run out of executor slots before it runs out of cores; it runs out of room to shuffle, join and cache in, which shows up as spilling rather than as idle cores.

Check before you copyThe usable-RAM column is an assumption, not a fact about your cluster

Every figure in this table is derived from the default reservation — one core and 8% of RAM — not from what your NodeManager actually advertises. On several instance types EMR is markedly more conservative than 8%. On m5.2xlarge this table assumes 29 GiB usable, while EMR's default yarn.nodemanager.resource.memory-mb is 24,576 MB. Two of the 14 GiB containers below need 28 GiB, so on a real EMR node only one of them fits: YARN quietly schedules half the executors you sized for and nothing in the logs says why. Read yarn.nodemanager.resource.memory-mb off your own cluster, subtract it from the node's RAM, type that in as the reservation, and use the numbers the calculator gives you then.

Recommended Spark executor settings for common EC2 instance types, derived from a reservation of one core and 8% of RAM per node. The usable-RAM column is that assumption, not your cluster's real yarn.nodemanager.resource.memory-mb, which on EMR is often lower.
InstancevCPURAMUsable (assumed)executor.coresExecutors / nodeexecutor.memorymemoryOverheadContainer
m5.large28 GiB7 GiB116g615m7 GiB
m5.xlarge416 GiB14 GiB3112g1229m14 GiB
m5.2xlarge832 GiB29 GiB3212g1229m14 GiB
m5.4xlarge1664 GiB58 GiB5317g1741m19 GiB
m5.8xlarge32128 GiB117 GiB5617g1741m19 GiB
m5.12xlarge48192 GiB176 GiB5917g1741m19 GiB
m5.16xlarge64256 GiB240 GiB51218g1844m20 GiB
m5.24xlarge96384 GiB368 GiB51917g1741m19 GiB
r5.large216 GiB14 GiB1112g1229m14 GiB
r5.xlarge432 GiB29 GiB3126g2663m29 GiB
r5.2xlarge864 GiB58 GiB3226g2663m29 GiB
r5.4xlarge16128 GiB117 GiB5335g3584m39 GiB
r5.8xlarge32256 GiB240 GiB5636g3687m40 GiB
r5.12xlarge48384 GiB368 GiB5936g3687m40 GiB
r5.16xlarge64512 GiB496 GiB51237g3789m41 GiB
r5.24xlarge96768 GiB752 GiB51935g3584m39 GiB
c5.large24 GiB3 GiB112688m384m3 GiB
c5.xlarge48 GiB7 GiB316g615m7 GiB
c5.2xlarge816 GiB14 GiB326g615m7 GiB
c5.4xlarge1632 GiB29 GiB538g820m9 GiB
c5.9xlarge3672 GiB66 GiB578g820m9 GiB
c5.12xlarge4896 GiB88 GiB598g820m9 GiB
c5.18xlarge72144 GiB132 GiB5148g820m9 GiB
c5.24xlarge96192 GiB176 GiB5198g820m9 GiB

Frequently asked questions

Why five cores per executor and not more?

Every task in an executor shares one JVM, one garbage collector and one HDFS/S3 client. Beyond roughly five concurrent tasks the client stops delivering more read throughput while GC pauses, lock contention and the cost of losing a single executor all keep rising. Five is not sacred — it is the point where the returns flatten on most storage backends. Fewer cores are fine; a lot more usually is not.

How is spark.executor.memoryOverhead calculated?

Overhead is max(384 MB, overheadFactor × spark.executor.memory), and this calculator defaults the factor to 0.10, which is Spark’s own default on YARN. The heap and the overhead have to fit inside one YARN container together, so the calculator solves backwards for the heap — and which side of that max() wins changes the arithmetic. When the factor wins, the heap is container ÷ (1 + factor), rounded down to a whole GiB, or to a 128 MB step when the quotient is under 2 GiB. When the 384 MB floor wins — at the default factor that is every container below 4,224 MB — the factor stops mattering and the heap is container − 384 MB, rounded down to a 128 MB step. The finer rounding there is deliberate: the overhead is pinned at 384 MB and cannot grow into whatever a whole-GiB rounding would shave off, so that memory would simply be lost. A 3 GiB container therefore gives 2688m of heap plus 384 MB of overhead, not 2g. Overhead pays for JVM metaspace, thread stacks, Netty shuffle buffers and Python worker processes — none of which come out of the heap.

My PySpark job keeps getting killed by YARN. What should I change?

Raise the overhead factor. Python workers are separate OS processes that live entirely outside the JVM heap and are charged against the container, so a job that is fine in Scala at 0.10 will be killed at the same setting in PySpark. Try 0.15 for plain PySpark and 0.20 to 0.25 when pandas UDFs, Arrow or ML libraries are involved, then re-derive the heap from the same container budget — which is what this calculator does when you change the factor.

How many shuffle partitions should I set?

Aim for 128 to 200 MB of shuffle data per partition, then round up to a whole multiple of your total task slots so the last wave is full rather than a long tail of idle cores. That rounding is close to free while there are several waves to spread it over — the partitions shrink by a few MB each. It is not free when the data is smaller than one wave: rounding 8 partitions up to 145 does not fill a tail, it multiplies the task count 18-fold and cuts every task to a few MB, and the honest fix is fewer nodes or more data. Spark’s default of 200 knows nothing about your data or your cluster either: it starves a 500-core cluster and shreds a 10 GiB job into thousands of tiny tasks.

Does adaptive query execution make this unnecessary?

No, but it changes what the number means. On Spark 3.2 and later AQE is on by default and coalesces partitions at runtime from real shuffle statistics, so spark.sql.shuffle.partitions becomes an upper bound rather than an exact count, and spark.sql.adaptive.advisoryPartitionSizeInBytes does the fine tuning. Executor cores, memory and overhead are still fixed when the JVM starts — AQE cannot help you there.

Why did my --num-executors get ignored on EMR?

EMR enables dynamic allocation by default, and it does so in the cluster’s spark-defaults, so it is on in client mode exactly as it is in cluster mode — the deploy mode has nothing to do with it. While spark.dynamicAllocation.enabled is true, Spark scales executors itself and the static count is ignored. Either set it to false, or keep dynamic allocation and feed these numbers into spark.dynamicAllocation.minExecutors and maxExecutors instead. The generated spark-submit command sets it to false so the sizing actually takes effect.

Is any of this stored or uploaded?

No. The whole calculator is a few hundred lines of arithmetic running in your browser. There is no server, no API and no analytics on the numbers you type. Your inputs are remembered in this browser’s local storage so the page comes back the way you left it, and clearing site data removes them.