Storage Levels
Concepts covered: Caching & Persistence, Memory Management
A storage level answers 3 questions about a cached DataFrame: does it live in executor memory, on the executor's local disk, or both; how many copies exist; and what happens to a partition when memory runs out. The levels you will use cover those answers in a handful of combinations.
| Level | Where partitions go | A partition that does not fit in memory |
|---|---|---|
| `MEMORY_ONLY` | executor memory | is left unstored and recomputed each time an action needs it |
| `MEMORY_AND_DISK` | memory first, then local disk | is written to the executor's local disk and read back from there |
| `DISK_ONLY` | local disk only | never comes up: every partition goes to disk |
| `MEMORY_ONLY_2`, `MEMORY_AND_DISK_2` | as the level without `_2`, on 2 executors | is handled as by the level without `_2` |
Local disk here means the disk attached to each executor's machine. It is scratch space: whatever a level writes there disappears when the application ends, and your output files are never involved.
The defaults differ by API
A DataFrame's cache() uses memory and disk. Print its level after calling it and you see Disk Memory Deserialized 1x Replicated, the MEMORY_AND_DISK_DESER level: memory first, local disk for what does not fit, 1 copy. An RDD's cache() uses MEMORY_ONLY, so data cached through .rdd behaves differently when memory runs short. Deserialized describes how the stored bytes are laid out, and the choices in this section are about memory, disk and copies.
enriched.cache()
print(enriched.storageLevel)
sessions.persist(StorageLevel.MEMORY_ONLY)
history.persist(StorageLevel.DISK_ONLY)Pick by what a miss costs
Every level is a bet about misses: the partitions an action needs that memory is not holding. On a miss, MEMORY_ONLY recomputes the partition from the transformations that built it, and the disk levels read it back from local disk. A disk read costs about the time to read that partition's bytes. A recompute can cost everything behind the DataFrame: the file read, plus any join, groupBy or other shuffle. So the choice comes down to which of those is cheaper for this DataFrame, and how often misses will happen.
| DataFrame | How it is built | Size against memory | Level, and why |
|---|---|---|---|
| `sessions` | a filter over 1 day of files | fits with room to spare | `MEMORY_ONLY`: misses are rare, and a miss reruns a short scan |
| `enriched` | the 7-minute read and join | about twice the executors' memory | `MEMORY_AND_DISK`: half of it would miss, and a disk read beats rerunning the join |
| `history` | 3 joins and a `groupBy` over a year | many times the memory | `DISK_ONLY`: almost all of it would miss anyway, and memory stays free for `sessions` |
- MEMORY_ONLY fits when
- the DataFrame fits in memory with room to spare
- recomputing a partition is a short scan or filter
- you would rather a miss fall back to the source than to disk
- MEMORY_AND_DISK fits when
- the DataFrame might not fit in memory
- recomputing a partition reruns a join, a groupBy or a slow read
- a steady read from local disk beats an unpredictable recompute
Copies on 2 executors
When an executor is lost, the partitions it had cached go with it, memory and local disk alike, and the next action that needs them recomputes them. A _2 level stores each partition on 2 executors, so the surviving copy serves the next read when 1 of them is lost. It also doubles the memory the cache takes. Use it only when executors are lost often and a recompute is slow enough to matter; for most batch jobs, 1 copy is the right default.
A DataFrame takes 20 minutes to build through 2 shuffles, is about 3 times the executors' combined memory, and 5 later actions read it. Which level should it get?
- MEMORY_ONLY, because reads from memory are the fastest
- MEMORY_ONLY_2, so a lost executor does not cost a recompute
- No cache, because it cannot fit in memory anyway
- MEMORY_AND_DISK, keeping what fits in memory and the rest on local disk
- Do
- Choose MEMORY_AND_DISK when a recompute reruns a join or shuffle
- Choose MEMORY_ONLY for small DataFrames that rebuild from a short scan
- Check the level with print(df.storageLevel) after setting it
- Avoid
- Don't pick MEMORY_ONLY for a DataFrame larger than memory
- Don't use a _2 level by habit; it doubles the memory used
- Don't treat DISK_ONLY as a saved output; it vanishes with the application
A DataFrame is a filter over 1 day of page_views files. It rebuilds in about 20 seconds, takes about a tenth of the executors' memory, and 3 actions read it. Which call fits it best?
- persist(StorageLevel.MEMORY_ONLY), since it fits with room to spare
- persist(StorageLevel.DISK_ONLY), to keep executor memory free
- persist(StorageLevel.MEMORY_ONLY_2), so a lost executor costs nothing
- MEMORY_ONLY, set after the first action so that action runs faster
About this lesson
This section is part of the Memory and Caching: Beginner 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.