Execution vs Storage Memory

The unified region holds 2 kinds of data. Execution memory is working space for tasks running right now: the hash table a groupBy builds, the buffer a sort fills, the rows a sort-merge join holds while it matches keys, and records waiting to be written as shuffle files. Storage memory holds blocks you asked Spark to keep: partitions from cache() and persist(), plus broadcast variables, including the table a broadcast join ships to every executor.

Before Spark 1.6 a fixed wall stood between them, so a job with no cache left its storage share idle while its sorts spilled. The unified design takes the wall down and lets either side borrow what the other is not using. The rules that matter are the ones for getting borrowed memory back.

The borrowing rules

SituationWhat Spark does
Storage wants space and execution's side is idleStorage borrows it, so a cache can grow past its half
Execution wants space that storage has borrowedExecution evicts cached blocks until storage is back at its line
Execution wants more, and storage is at or below its lineExecution evicts nothing further, and the task spills to disk
Storage wants space that execution is usingStorage cannot evict execution, so the new block goes to disk or stays uncached, per its storage level

The asymmetry follows from what each side holds. Execution memory is the half-finished state of a running task, and taking it away would mean failing the task or redoing its work. A cached block is a copy: once evicted, Spark can rebuild it from lineage or read it back from disk. So execution wins every contest, but only down to the line spark.memory.storageFraction draws, which keeps a large shuffle from wiping a cache out entirely.

One executor, one join, step by step

Back to the running example: 4,735 MB of unified memory, with the storage line near 2,368 MB. A job caches an enriched trips DataFrame that needs 3,600 MB, then runs a join that asks for working memory in 2 rounds.

StepEventStorage (MB)Execution (MB)Free (MB)
1Executor starts004,735
2`cache()` fills 3,600 MB of partitions3,60001,135
3Join tasks ask for 2,000 MB2,7352,0000
4Join tasks ask for 600 MB more2,3682,3670
5Join finishes2,36802,367

At step 2 the cache has borrowed 1,232 MB from the execution side. At step 3 the join takes the 1,135 MB that were free and evicts 865 MB of cached partitions to cover the rest. At step 4 only 367 MB more can be evicted before storage reaches its line, so 233 MB of the request goes unmet and the tasks spill. After step 5 the cache is short 1,232 MB of partitions, and 2,367 MB sits free.

The running-example executor has a cached DataFrame using 3,000 MB of its 4,735 MB unified region when a join's tasks ask for 2,500 MB of execution memory. What happens?

What happens to evicted partitions depends on the storage level. Under MEMORY_ONLY they are dropped, and the next action that reads them recomputes them from lineage. Under a memory-and-disk level they are written to local disk as they leave memory, and the next read comes from there. A DataFrame's cache() uses a memory-and-disk level, while an RDD's cache() uses MEMORY_ONLY.

What a big join can do to your cache
  • Evict every cached block above the storage line
  • Turn the next read of those partitions into a recompute or a disk scan
  • Leave memory free that refills only when an action reads those partitions again
What your cache can do to a big join
  • Hold its protected half, about 2,368 MB here, out of execution's reach
  • Lower the ceiling on execution, so join tasks spill earlier
  • Never take memory from a task that is already running

That right-hand column is why a cache can never stall a join. No running task waits for storage to hand memory back. The worst a cache does is sit in its protected half, which caps execution and makes the join spill sooner, a cost the next section teaches you to read.

A DataFrame built from a wide join of transactions to products is persisted, and a large aggregation then runs on the same executors and evicts part of it. Which choice of storage level makes the next read of the evicted partitions skip a rerun of the join?

About this lesson

This section is part of the Memory and Caching: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.