Skip to the page
Tecyfy

PySpark interview question · hard

How do you tune the number of executors, cores, and memory in Spark?

The short answerStart from the node size: give each executor about 4–5 cores, divide each node's memory between the executors on it after leaving room for the operating system and for memory overhead, and set the number of executors from the cores you want in total.

The full answer

Start from the node size: give each executor about 4–5 cores, divide each node's memory between the executors on it after leaving room for the operating system and for memory overhead, and set the number of executors from the cores you want in total. Then check the Spark UI for spills, garbage collection time and idle cores, and adjust.

A worked example Cluster: 10 worker nodes, each with 16 cores and 64 GB of memory.

  1. Reserve for the OS and node services: about 1 core and a few GB per node, leaving 15 cores and about 60 GB.
  2. Cores per executor: 5. More than about 5 tends to hurt I/O throughput and makes garbage collection pauses longer; 1 wastes memory on per-executor overhead.
  3. Executors per node: 15 ÷ 5 = 3, so 30 in total. On YARN, leave one executor's worth of capacity for the application master, giving 29.
  4. Memory per executor: 60 GB ÷ 3 = 20 GB per container. Memory overhead defaults to 10% of executor memory (at least 384 MB) and covers off-heap use; PySpark jobs with Python UDFs need more. So set about 18 GB of executor memory and 2 GB of overhead.
spark-submit \
  --num-executors 29 \
  --executor-cores 5 \
  --executor-memory 18g \
  --conf spark.executor.memoryOverhead=2g \
  --driver-memory 8g \
  job.py

How memory inside an executor is used After 300 MB of reserved memory, spark.memory.fraction (0.6) of the heap is shared by execution (shuffles, joins, sorts) and storage (cache), which borrow from each other; the rest is for user data structures and Spark's internal metadata. These defaults rarely need changing.

Dynamic allocation For variable workloads or shared clusters, let Spark add and remove executors:

spark.dynamicAllocation.enabled=true
spark.dynamicAllocation.minExecutors=2
spark.dynamicAllocation.maxExecutors=50
spark.dynamicAllocation.shuffleTracking.enabled=true    # or an external shuffle service

Match parallelism to cores With 145 cores (29 × 5), each stage should have at least 2–3 times that many tasks. Set spark.sql.shuffle.partitions accordingly for large jobs, and let adaptive query execution coalesce small partitions.

Read the symptoms

Symptom in the Spark UIAdjustment
Heavy spill to diskMore memory per core: fewer cores per executor or more memory; or more, smaller partitions
Long garbage collectionSmaller heaps (more executors with less memory each)
Containers killed for exceeding memoryRaise memory overhead, especially for PySpark
Idle executors, few tasksMore partitions
One slow taskSkew, not resources

Driver sizing Usually 4–8 GB is enough; more only if the job collects results, broadcasts large tables or plans very large queries.

Managed platforms On Databricks, EMR or Dataproc, you mostly choose instance types and autoscaling, and the platform sets executor sizes; serverless options size them for you. The same principles still guide which instance types to choose: memory-optimised for joins and caching, compute-optimised for CPU-heavy transformations.

Last updated 16 October 2026