Spill Ratios

A stage that spills adds 2 columns to its stage page, Spill (Memory) and Spill (Disk), and they measure the same records in 2 states. Spill (Memory) is the execution memory a task released by spilling, counted as the memory pages it held at that moment. Spill (Disk) is what those records took up once serialized and compressed into a spill file.

Their quotient is a compression ratio for your data in the form the engine holds it. With a ratio of 8, every byte on disk had been 8 bytes of memory.

What the ratio tells you

StageSpill (Memory)Spill (Disk)RatioSpill time goes to
796 GB12 GB8.0CPU: serializing and compressing
1230 GB27 GB1.1Disk: writing nearly every byte held

A high ratio makes spilling cheap on disk and expensive on CPU: stage 7 serializes 96 GB to write 12 GB, and its flame graph shows the spill and compression frames from the previous section. A ratio near 1 means the data was already dense, such as compressed blobs, hashes or random IDs that LZ4 cannot shrink. Stage 12 writes almost every byte it held, so its local disks set the pace, which the next section's OS checks confirm.

The ratio also converts shuffle bytes into memory bytes. Shuffle files go through the same serializer and the same lz4 codec as spill files, so a task's Shuffle Read Size times the ratio approximates the memory that task needs to hold its whole input.

Stage 21 shows Spill (Memory) of 44 GB and Spill (Disk) of 40 GB. Its tasks run slowly while executor CPU sits near 30%. Where does the spill time go?

Sizing from what tasks held

Spill totals say whether a stage spilled. Sizing needs the stage's summary metrics, read at the max row, because the largest task is the first to run out. Show Additional Metrics adds Peak Execution Memory, the most a task held at once. A task that peaked at P and still spilled S wanted roughly P + S to finish without touching disk.

The animation below plays a sort-merge join in which user 7 owns 9 of the 12 events. Each key's rows land on a single reducer task, so the task that receives user 7 holds those 9 events and the matching user row, more than any other task. At production scale, that task is the max row of the summary metrics.

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
joined = events.join(users, "user_id")
joined.count()
events
user_idpage
1home
7home
7search
7cart
7search
2home
7item
7item
7cart
3search
7checkout
7home
users
user_idplan
1free
2pro
3free
7pro
9free

Stage 15: 200 tasks, 16 GB heaps, 4 cores

MetricMedian taskMax task
Shuffle Read Size690 MB705 MB
Peak Execution Memory2,300 MB2,360 MB
Spill (Memory)3,200 MB3,280 MB
Spill (Disk)400 MB410 MB

Supply first. A 16 GB heap is 16,384 MB; subtract the 300 MB Spark reserves and multiply by spark.memory.fraction, 0.6, to get 9,650 MB of unified memory. With 4 tasks running and nothing cached, each can claim a quarter, about 2,410 MB. Now demand. The max task peaked at 2,360 MB and spilled 3,280 MB more, so it wanted about 5,640 MB, which matches its 705 MB of shuffle input times the ratio of 8. Each task needs about 2.3 times the memory it gets.

Halving the executor to 2 cores raises each task's share to about 4,825 MB, still short of 5,640, and halves how many tasks the executor runs at once. Raising the partition count works the other way, by shrinking what each task holds. Keeping the max task's input under 2,410 / 8, roughly 300 MB, means splitting the stage's 140,000 MB of shuffle data at least 467 ways, and 600 partitions leaves headroom for hash-table overhead and uneven keys.

About this lesson

This section is part of the The Spark UI: Advanced 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.