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 stateExecution poolMost a task can holdFloor a task is given
No cache, 4 tasks running4,735 MBabout 1,184 MBabout 592 MB
Cache at its 2,368 MB line, 4 tasks runningabout 2,367 MBabout 592 MBabout 296 MB
Cache at its line, 2 cores instead of 4about 2,367 MBabout 1,184 MBabout 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()
trips
trip_iddriver_idfare
110112.5
21018
310222
410115.25
51039.75
610131
71046.5
810118
910211
1010127.5
1110514
121017.25
drivers
driver_idcity
101Chicago
102Austin
103Denver
104Austin
105Boston

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:

MetricMinMedian75th percentileMax
Duration41 s48 s55 s14 min
Shuffle Read Size75 MiB92 MiB105 MiB1.9 GiB
Peak Execution Memory230 MiB280 MiB310 MiB2.3 GiB
Spill (Memory)0 B0 B0 B7.6 GiB
Spill (Disk)0 B0 B0 B1.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?

Tracing a spill to the memory it lacked

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.