Spilling to Disk
A spill is what a task does when it asks for execution memory and the memory manager refuses. The operator holding the data, whether a sort buffer, an aggregation hash table or the sort inside a sort-merge join, writes what it has to local disk as a sorted file, frees the memory, and keeps reading. When input runs out, it merges the spilled files with what is still in memory. The task finishes with the right answer, after serializing, writing, reading back and merging data that never needed to leave RAM.
How much memory one task can get
Every task running on an executor at the same moment draws on the same execution pool. With N tasks running, each can hold at most 1/N of the pool. Spark also keeps a floor: before a task is left with less than 1/(2N), it waits for other tasks to release memory. On the running example's 4 cores, N is 4.
| Executor state | Execution pool | Most a task can hold | Floor a task is given |
|---|---|---|---|
| No cache, 4 tasks running | 4,735 MB | about 1,184 MB | about 592 MB |
| Cache at its 2,368 MB line, 4 tasks running | about 2,367 MB | about 592 MB | about 296 MB |
| Cache at its line, 2 cores instead of 4 | about 2,367 MB | about 1,184 MB | about 592 MB |
Use the table as a test for any stage: when a task's input, held in memory as rows, outgrows its share, that task spills. Shuffle read sizes in the UI are compressed bytes, and the same rows usually take several times that space once deserialized, so 300 MB of shuffle read can overflow a 592 MB share. The last row shows a lever on the cores: halving them doubles each task's share without touching the heap, at the price of half as many tasks at once.
Where a skewed key puts the pressure
This job joins trips to drivers with broadcasting switched off, so both sides are shuffled into 3 reducer tasks by driver_id. Driver 101 has 7 of the 12 trips. Every row a reducer task receives is held in its execution memory while the sort-merge join sorts and matches it, so watch which task collects the 7.
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
joined = trips.join(drivers, "driver_id")
joined.collect()| trip_id | driver_id | fare |
|---|---|---|
| 1 | 101 | 12.5 |
| 2 | 101 | 8 |
| 3 | 102 | 22 |
| 4 | 101 | 15.25 |
| 5 | 103 | 9.75 |
| 6 | 101 | 31 |
| 7 | 104 | 6.5 |
| 8 | 101 | 18 |
| 9 | 102 | 11 |
| 10 | 101 | 27.5 |
| 11 | 105 | 14 |
| 12 | 101 | 7.25 |
| driver_id | city |
|---|---|
| 101 | Chicago |
| 102 | Austin |
| 103 | Denver |
| 104 | Austin |
| 105 | Boston |
The plan has 3 stages: a map stage for each side writing shuffle files, then the join stage reading them. The task that receives driver 101 holds at least 7 trip rows, while the other 2 tasks share at most the remaining 5. Scale every row up a million times: that task gets the same share of memory as its neighbours, with several times their input, and it is the one that spills.
Reading a spill in the stage metrics
Spark records 2 spill numbers per task. Spill (Memory) is the size of the spilled data as deserialized rows in memory at the moment it was written out. Spill (Disk) is the size of the same data serialized and compressed on disk. The memory figure is usually several times the disk figure, and both read 0 for a task that never ran short. The stage page shows them as quantiles across the stage's tasks:
| Metric | Min | Median | 75th percentile | Max |
|---|---|---|---|---|
| Duration | 41 s | 48 s | 55 s | 14 min |
| Shuffle Read Size | 75 MiB | 92 MiB | 105 MiB | 1.9 GiB |
| Peak Execution Memory | 230 MiB | 280 MiB | 310 MiB | 2.3 GiB |
| Spill (Memory) | 0 B | 0 B | 0 B | 7.6 GiB |
| Spill (Disk) | 0 B | 0 B | 0 B | 1.6 GiB |
This stage ran on running-example executors with a cache sitting at its 2,368 MB line, so the execution pool was about 2,367 MB and each of 4 tasks started with about 592 MB. The max column carries the story. One task read 1.9 GiB of shuffle data, about 21 times the median, and spilled 7.6 GiB of rows. Its peak execution memory of 2.3 GiB is nearly the whole pool: as the other tasks finished, N fell, its share grew toward all of it, and it still could not hold its input. The median task, with its 592 MB share and a 280 MiB peak, never spilled.
A stage's summary metrics show the median task reading 120 MiB of shuffle data with no spill, while the max task reads 3.1 GiB and spills 11 GiB. Executors have 8g heaps and 4 cores. Which change targets the cause?
- Raise spark.sql.shuffle.partitions so that every task reads less shuffle data.
- Raise spark.executor.memory to 16g so that each task's share of memory grows.
- Raise spark.memory.fraction to 0.9 so that execution gets more of each heap.
- Salt or isolate the hot key so that its rows spread across many tasks.
Tracing a spill to the memory it lacked
- Find the stage with nonzero Spill (Disk) and open its summary metrics.
- Compare Max with Median. Spill on the top tasks only means skewed input: salt or isolate the hot key, since more partitions leave it in one task.
- Spill on every task means each task's input outgrows its share. Raise spark.sql.shuffle.partitions so each task reads less.
- Even spill that more partitions do not cure, with a large cache at its line, means the cache is capping execution. Unpersist it before the stage, or give each task more memory with fewer cores or a bigger heap.
Find the execution memory in a query
The cell below aggregates the practice transactions table. It has 2 shuffles and 3 stages: the groupBy hashes rows by user_id, and the orderBy range-partitions the totals. Each reducer task builds a structure from execution memory, a hash table for the aggregation and a sort buffer for the ordering. Run it, then point to the 2 operations that would spill first if this table held a billion rows.
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.