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
| Stage | Spill (Memory) | Spill (Disk) | Ratio | Spill time goes to |
|---|---|---|---|---|
| 7 | 96 GB | 12 GB | 8.0 | CPU: serializing and compressing |
| 12 | 30 GB | 27 GB | 1.1 | Disk: 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?
- To disk: 44 GB held became 40 GB written, so spill writes almost every byte; check the local disks.
- To CPU: a ratio near 1 means the serializer works hardest, so profile with -e cpu.
- To memory: the 4 GB gap is what tasks lost by spilling, so add 4 GB to each executor.
- To network: spill files go to other executors, so a ratio near 1 means slow transfers.
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()| user_id | page |
|---|---|
| 1 | home |
| 7 | home |
| 7 | search |
| 7 | cart |
| 7 | search |
| 2 | home |
| 7 | item |
| 7 | item |
| 7 | cart |
| 3 | search |
| 7 | checkout |
| 7 | home |
| user_id | plan |
|---|---|
| 1 | free |
| 2 | pro |
| 3 | free |
| 7 | pro |
| 9 | free |
Stage 15: 200 tasks, 16 GB heaps, 4 cores
| Metric | Median task | Max task |
|---|---|---|
| Shuffle Read Size | 690 MB | 705 MB |
| Peak Execution Memory | 2,300 MB | 2,360 MB |
| Spill (Memory) | 3,200 MB | 3,280 MB |
| Spill (Disk) | 400 MB | 410 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.