Cache Eviction

A cached DataFrame is a set of blocks, 1 per partition, each held by the block manager of the executor that computed it. Blocks live in the storage side of the unified pool, and they leave it in 2 ways. When a new block needs room, the block manager evicts the least recently used blocks of other datasets. When a task needs execution memory, it can take back whatever storage has borrowed beyond spark.memory.storageFraction of the pool, 0.5 by default, evicting blocks until it has enough. Blocks below that line are safe from execution, though a newer cache can still push them out.

Neither kind of eviction raises an error. A block stored with MEMORY_ONLY is dropped, and the next action that needs that partition rebuilds it from lineage inside the task: the source read and every transformation that built the cached DataFrame run again for that partition. A level that includes disk writes the evicted block to local disk instead, which costs a read but no recompute. One more rule shapes what you see: a block being stored never evicts blocks of its own dataset, so a DataFrame larger than its memory ends up partly cached, and under MEMORY_ONLY the partitions that never fit are recomputed on every action.

Watching a cache shrink

Take the trips cache from a nightly pricing job: 200 partitions persisted with MEMORY_ONLY over 10 executors, each with a 9,650MB unified pool from a 16g heap. Here is its Storage tab row, read at 2 moments:

WhenCached PartitionsFraction CachedSize in Memory
After the first action200100%79,100MB
After the join stage12261%48,251MB

Each executor held 20 partitions, 7,910MB, against a protected half of 4,825MB. The join stage needed execution memory, reclaimed the borrowed share, and left every executor with about 12 partitions. The 78 that left were dropped, so the next stage that reads the cache rebuilds each of them from the trip source and the enrichment that produced it, on every run, without a line in the driver log.

Residency versus intent

Which signals report what is actually held

Keeping the cache resident

Do
  • Persist with MEMORY_AND_DISK when a rebuild costs more than a local disk read
  • Select and filter before caching, so the blocks fit inside the protected half
  • Call unpersist() on caches a loop no longer reads, before the next iteration
  • Checkpoint data whose rebuild you cannot afford at all, or write it out and read it back
Avoid
  • Treat isCached as proof that a partition is still in memory
  • Cache a DataFrame larger than the storage memory your executors hold
  • Raise spark.memory.storageFraction without checking the spill it adds

Raising spark.memory.storageFraction to 0.85 would protect about 8,200MB per executor, enough for all 20 trip partitions, but it leaves execution a guaranteed 1,450MB, so the join stage would spill instead of evicting. Size the protection from the cache's real footprint in the Storage tab.

A loop persists features with MEMORY_ONLY and reads it in every iteration. features.storageLevel still reports MEMORY_ONLY, yet iteration 3 takes twice as long as iteration 1, and the Storage tab shows Fraction Cached at 55%. What explains it?

unpersist() acts at once: it removes the blocks as soon as it runs, with no action needed. So it belongs after the last action that reads the cache. Called 1 action early, that last action finds no blocks and rebuilds every partition from lineage, which is the same recompute an eviction causes, this time chosen by your own code.

About this lesson

This section is part of the Memory and Caching: 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.