EMR YARN Dynamic Executor Allocation
Dynamic allocation lets a Spark application grow and shrink the number of executors it holds while it runs. On YARN, and so on Amazon EMR, every executor is a YARN container, so scaling executors means asking the ResourceManager for containers when tasks pile up and handing them back when they sit idle. The point is cost: a cluster is paid for by the resources reserved, not by the work done.
In a YARN-managed Spark deployment on Amazon EMR, dynamic allocation adjusts the number of executors based on the current workload:
- Scale up: when pending tasks exceed available executor slots, the ApplicationMaster requests new containers from the ResourceManager
- Scale down: when executors sit idle beyond a timeout threshold, they are released back to the cluster
- Safe release: only an executor with no running tasks is released; one that holds cached blocks is kept until
cachedExecutorIdleTimeout, which is infinite by default, and its shuffle files stay readable through the shuffle service. Since Spark 3.1, decommissioning can also migrate an executor's blocks to its peers before it goes
This mechanism prevents over-provisioning while ensuring sufficient resources during computation spikes.
A YARN ResourceManager and three EMR NodeManagers whose Spark executors grow from two to six under a backlog of tasks and shrink back to one as they sit idle, with the executor count plotted over time against the workload.
YARN in one page
YARN separates resource management from the applications that use it (Vavilapalli et al., SoCC 2013). Three roles matter for Spark:
- The ResourceManager runs once per cluster, on the EMR primary node. Its scheduler, usually the CapacityScheduler, decides which application gets which resources on which node. It knows nothing about Spark.
- A NodeManager runs on every worker node. It reports the node's capacity, launches the containers the ResourceManager assigns to it, monitors their memory, and kills a container that exceeds its limit. On EMR, core and task nodes run NodeManagers.
- Each application has its own ApplicationMaster. For Spark it is the component that asks the ResourceManager for executor containers. In cluster deploy mode the Spark driver runs inside the ApplicationMaster's container; in client mode the driver runs on the machine that submitted the job and the ApplicationMaster only negotiates containers.
A container is a slice of one node: an amount of memory and a number of virtual cores. One Spark executor is one container, and the executor runs as many tasks at once as it has cores (spark.executor.cores).
What an executor really asks for
The most common YARN surprise is that the container is bigger than spark.executor.memory. That setting is only the JVM heap. Spark adds a memory overhead for everything outside the heap: thread stacks, direct byte buffers used by the network layer, native libraries, JVM metadata, and the Python worker processes in PySpark unless they have their own budget. The container YARN is asked for is heap plus overhead plus any off-heap and PySpark memory: spark.executor.memory plus spark.executor.memoryOverhead, plus spark.memory.offHeap.size and spark.executor.pyspark.memory when they are set. The overhead, unless set explicitly, is spark.executor.memoryOverheadFactor times the heap, 0.10 by default, but at least 384 MiB.
The ResourceManager then normalizes the request. The CapacityScheduler rounds memory up to a multiple of yarn.scheduler.minimum-allocation-mb (1024 in Hadoop's defaults; check your cluster's yarn-site.xml) and refuses anything above yarn.scheduler.maximum-allocation-mb. With an 8 GiB heap and the stock defaults, the overhead is MiB, the request is MiB, and the CapacityScheduler rounds it up to MiB, the next multiple of 1024.
That 9216 is what counts against the node's yarn.nodemanager.resource.memory-mb, and so it decides how many executors fit on a node. Sizing executors means working backwards from the node: the memory YARN may hand out, divided by the container size, gives executors per node, and the leftover is wasted. On EMR the per-node YARN memory is set from the instance type, and the EMR-specific maximizeResourceAllocation option sizes executors to the largest a core node can hold.
Inside the heap, Spark's unified memory manager reserves 300 MB, then gives spark.memory.fraction (0.6) of the rest to execution and storage, which share it; spark.memory.storageFraction (0.5) of that region is protected from eviction for cached blocks.
When an executor is killed with a message that it exceeded its physical memory limit, the heap did not overflow: an OutOfMemoryError would have said so. The process as a whole outgrew heap plus overhead. The fix is a larger spark.executor.memoryOverhead, or finding what grew off-heap, not a larger heap.
How dynamic allocation decides
Spark's ExecutorAllocationManager runs in the driver and compares two numbers: the executors it holds, and the executors it would need to run every running and pending task at once, which is the task count divided by the tasks one executor can run in parallel (its cores divided by spark.task.cpus). The settings below all live under the spark.dynamicAllocation. prefix.
Scaling up. When tasks have been pending for schedulerBacklogTimeout (1 s by default), Spark requests executors. If the backlog persists for another sustainedSchedulerBacklogTimeout, it requests again, and each round asks for twice as many as the last: 1, 2, 4, 8. The ramp starts small in case a few executors are enough, and grows fast in case they are not. It never asks for more than the backlog needs or than maxExecutors allows.
Scaling down. An executor that has had no task for executorIdleTimeout (60 s by default) is released, but never below minExecutors (0 by default). An executor holding cached blocks follows cachedExecutorIdleTimeout instead, which defaults to infinity, so in practice it is never released.
Shuffle data. Releasing an executor must not lose shuffle files that later stages will read. Either the external shuffle service serves them from the NodeManager after the executor is gone, or shuffleTracking.enabled keeps executors with live shuffle data from being released at all. The fault tolerance article shows what happens when shuffle files disappear.
A worked example: the panel
The panel's cluster has NodeManagers with room for executors each, executors with cores, and bounds of to executors starting from . Each of its ticks is one allocation decision; a backlog that lasts a full tick counts as having outlived the timeout.
With one spike and a one-tick idle timeout, the backlog drives the count up to , and as the load drains the executors go back until remains. Counting a container as paid for from the tick it is requested until the tick it is released, the run holds executor-ticks against for a cluster fixed at the maximum: less. The price is the ramp: task-ticks spent waiting for executors that a fixed cluster would already have had.
Switch to short stages, where the load drops to zero between stages. With a one-tick idle timeout every gap releases executors and the next stage asks for them again: containers requested and released over the run, and task-ticks of waiting. With a three-tick timeout the gaps are bridged, executors are released, waiting falls to , and the saving shrinks to . Neither answer is right in general; the idle timeout should be set from the gaps a job actually has.
Click an executor to give it cached data, and it stays for the rest of the run however idle it is. That is the default cachedExecutorIdleTimeout at work.
Specific to Amazon EMR
- Defaults. Since EMR release 4.4.0,
spark.dynamicAllocation.enabledis true and the Spark shuffle service is configured in YARN automatically.spark.executor.memoryandspark.executor.coresare set from the core and task instance types. Open-source Spark ships with dynamic allocation off. - Two layers of scaling. Dynamic allocation changes how many executors one application holds inside the cluster. EMR managed scaling, or an autoscaling policy, changes how many nodes the cluster has. They interact: executors released by Spark free YARN capacity that lets managed scaling remove nodes, and a backlog of container requests is what makes it add them.
- Node decommissioning. EMR builds on YARN's graceful decommissioning: Spark stops scheduling new tasks on a node that is being decommissioned and lets running ones finish. For Spot instances, which get only a short warning, EMR also cleans up the node's cached and shuffle blocks early so recomputation starts sooner, and does not count fetch failures from decommissioned nodes against the stage's retry limit.
Common pitfalls
- Unbounded
maxExecutors. The default is infinity. One job with a wide stage can take every container in the cluster and hold it until the stage ends, while other applications queue. - Cached executors that never leave. A job that caches early keeps its peak executor count for its whole run.
unpersist()what is no longer needed, or setcachedExecutorIdleTimeout. - Churn on jobs with short stages. A 60-second idle timeout shorter than the gaps between stages releases executors only to request them again, and every request pays the container start-up time.
- Oversized executors. Very large heaps mean long garbage-collection pauses and coarse packing on nodes. Mid-sized executors with a few cores each are the usual compromise.
- Ignoring overhead in PySpark. Python workers live outside the JVM and, without
spark.executor.pyspark.memory, inside the overhead budget. Pandas UDFs over large batches are a common source of containers killed for exceeding memory. - Fixed
spark.executor.instancestogether with dynamic allocation. An explicit instance count becomes the initial executor count; read the effective configuration in the Spark UI's Environment tab rather than assuming.
Short answers
How does Spark dynamic allocation work?
Spark watches its task backlog. When tasks have been pending longer than the schedulerBacklogTimeout setting, 1 second by default, it asks the cluster manager for more executors, doubling the request each round up to what the tasks need and maxExecutors allows. An executor with no tasks for executorIdleTimeout, 60 seconds by default, is released, unless it holds cached data or minExecutors would be crossed.
What is spark.executor.memoryOverhead?
The memory YARN reserves for an executor on top of its JVM heap: thread stacks, direct buffers, native libraries, interned strings, and Python workers when spark.executor.pyspark.memory is not set. By default it is 10% of spark.executor.memory with a minimum of 384 MiB, and the container YARN allocates is heap plus overhead plus any off-heap or PySpark memory configured.
Why was my Spark container killed by YARN for exceeding memory limits?
The executor's process grew past the container size YARN granted, which is heap plus overhead, and the NodeManager killed it. The heap was not the problem, so raising spark.executor.memory alone rarely helps. Raise spark.executor.memoryOverhead, reduce what lives off-heap, or for PySpark give the Python workers their own spark.executor.pyspark.memory.
How much memory does a Spark executor request from YARN?
spark.executor.memory plus spark.executor.memoryOverhead, plus spark.memory.offHeap.size and spark.executor.pyspark.memory when those are set. The ResourceManager then rounds the request up to its allocation increment, a multiple of yarn.scheduler.minimum-allocation-mb in the CapacityScheduler, and rejects requests above yarn.scheduler.maximum-allocation-mb.
Does dynamic allocation need the external shuffle service?
It needs some way to keep shuffle files readable after their executor is released. The classic answer on YARN is the external shuffle service in each NodeManager. Since Spark 3.0, shuffle tracking can be used instead: it keeps executors that hold live shuffle data from being released.
Is dynamic allocation enabled on Amazon EMR by default?
Yes. Since EMR release 4.4.0, EMR sets spark.dynamicAllocation.enabled to true and configures the Spark shuffle service on YARN automatically. Open-source Spark leaves dynamic allocation off by default.
Further reading
- V. K. Vavilapalli et al., "Apache Hadoop YARN: Yet Another Resource Negotiator", SoCC 2013. The design of the ResourceManager, NodeManagers, ApplicationMasters and containers.
- Apache Spark documentation, Running Spark on YARN. Deploy modes, the ApplicationMaster, memory overhead and the external shuffle service on YARN.
- Apache Spark documentation, Job Scheduling, section "Dynamic Resource Allocation". The request and remove policies and graceful decommission of executors.
- Apache Hadoop documentation, Hadoop: Capacity Scheduler. Queues, minimum and maximum allocation, and how requests are normalized.
- Amazon EMR Release Guide, Configure Spark. The Spark defaults EMR sets,
maximizeResourceAllocation, and node decommissioning settings. - On this site: RDD transformations and the shuffle, the DAG scheduler and fault tolerance via lineage.