Memory and Caching: Beginner
Picture a Netflix data engineer whose morning Spark job joins a day of viewing events to member profiles, then feeds the joined table to 4 reports. Each report is an action, so the join runs 4 times. One .cache() in the right place cuts that to once. A missing unpersist() leaves the result holding executor memory long after the last report has read it.
The board below plays 1 action on 7 orders split across 2 partitions. The filter keeps the 5 completed orders, the groupBy shuffles them by region across 3 tasks, and collect() returns 3 totals: West 210, East 120, North 60. Run kept.count() after it and Spark reads all 7 rows and filters them a second time, because nothing from the first run was kept.
kept = orders.filter(F.col("status") == "Completed")totals = kept.groupBy("region").agg(F.sum("amount").alias("revenue"))totals.collect()
A job with 3 actions
Each action on the right runs every box to its left. The cache belongs on enriched, the last box all 3 actions share.
Count the shared work
| Where the cache sits | Action 1 runs | Actions 2 and 3 each run | Shared work in total |
|---|---|---|---|
| No cache | read + join | read + join | 21 min |
| On `clicks` | read + join | users read + join | 18 min |
| On `enriched` | read + join | a read of the cache | 7 min |
| On each action's own output | read + join | read + join | 21 min, plus 3 caches nobody reads again |
The rule the table shows: put the cache on the last DataFrame every later action shares, the point where the chain branches. Cache earlier, and each action still repeats the steps between the cache and the branch. Cache later, on 1 branch, and only the action on that branch benefits.
Another way to read the table: for each later action, a cache saves the work between the source and the cached DataFrame. On clicks that is only the 1.5-minute events read, because the users read and the join still run after it. On enriched it is 7, because both reads and the join sit behind it. The wide steps usually dominate that number, so a cache placed right after a join or a groupBy saves the shuffle along with everything before it.
These times leave out the cache's own costs, storing the partitions during the first action and reading them back in the later ones. Both are small next to a 5-minute join, and a later section of this lesson prices them.
Both actions read priced. Which DataFrame should carry .cache() to save the most work?
Narrow the cache before you store it
The branch point also decides how big the cache is, because a cache holds every row and every column of the DataFrame it sits on. If the 3 actions only read user_id, page_id and country, a select of those columns right before .cache() stores 3 columns instead of everything users carried. A filter placed before the cache shrinks it the same way, by rows.
enriched that 3 actions read is a single DataFrame to put .cache() on; the same chain retyped inside each action leaves you no single DataFrame to cache.cache() and persist() make the same request: keep this DataFrame's partitions on the executors once an action computes them. They differ in 1 thing, who chooses the storage level, meaning where the partitions are kept: memory, local disk or both.
| Call | Storage level it sets |
|---|---|
| `df.cache()` | the default for DataFrames: memory first, local disk for what does not fit |
| `df.persist()` | the same default |
| `df.persist(StorageLevel.MEMORY_ONLY)` | the level you pass, here memory only |
So cache() is shorthand for persist() with no argument. Reach for persist() when you want a level other than the default; the next section covers how to choose one. Both return the DataFrame they were called on and also mark it in place, so df.cache() on a line of its own works as well as df = df.cache().
Calling cache() on a DataFrame that is already cached changes nothing: Spark logs a warning that the data is already cached and keeps the first level. To switch levels, call unpersist() first, then persist() with the new level.
Marking is free, the first action pays
Like a transformation, cache() is lazy. The line returns at once, launches no job and stores nothing. The partitions are stored by the first action that computes them: as each task finishes a partition, the executor running it keeps a copy. That first action does all of its usual work plus the storing, so it runs a little slower than it would without the cache. Every action after it reads the stored copy.
Traced through the job from the previous section, with enriched.cache() on its own line before the actions:
| Line | What Spark does | Time |
|---|---|---|
| `enriched.cache()` | records the cache mark; no job runs | none |
| `enriched.count()` | reads, filters and joins, storing each partition as its task finishes | 7 min plus the storing |
| `enriched.groupBy("country").count().collect()` | reads the stored partitions and aggregates them | the aggregation only |
| `enriched.write.mode("overwrite").parquet(...)` | reads the stored partitions and writes them out | the write only |
The first action sets how much is stored
A partition is stored only when an action computes it in full, so the action you run first sets how much of the cache exists afterwards. count() has to visit every partition to count its rows, so it fills the whole cache. show() asks for 20 rows and computes only as many partitions as it needs to find them, sometimes 1 out of 200. The other 199 stay unstored until a later action computes them, and that later action pays the full build for each of them.
df.cache() followed by df.count(): 1 cheap action whose only job is to fill every partition, so the actions after it all start from a full cache. take(), first() and head() fill only part of it, the same way show() does.The DataFrame has 200 partitions. After show() finishes, how many of them are stored in the cache?
Checking the cache is in the plan
explain() shows whether a query will read the cache. After enriched.cache(), the plan for enriched.groupBy("country").count() starts from an InMemoryTableScan over an InMemoryRelation, where before it started from the file scans and the join. The plan reflects the mark, so it looks the same before and after the first action. To see how much is stored, open the Storage tab in Spark's web UI: it lists each cached DataFrame with its storage level, Cached Partitions, Fraction Cached, Size in Memory and Size on Disk. A Fraction Cached far below 100% right after a show() is the partial fill described above.
Try it on the practice data. The cache() line marks completed, count() computes and stores every partition, and the final collect() reads the stored copy to sum profit.
orders = spark.table("orders")
completed = orders.filter("status = 'Completed'").cache()
completed.count()
completed.agg(F.sum("profit").alias("completed_profit")).collect()| 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` |
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.
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` |
- 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
- 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?
cache() vs persist() section, and the next action then pays the full build again.- 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
- 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?
Pricing a single cache
Take enriched again: 7 minutes to build. Suppose storing it adds 30 seconds to the first action and reading it back costs each later action 20 seconds. Without a cache, each action pays the 7 minutes. With it, the first pays 7 minutes and 30 seconds, and each later one pays 20 seconds.
Four ways a cache burns memory
- Read by 1 actionnothing to reuseThe cache is filled, then never read. The job pays for the storing and holds the memory for no saving.
- Cheap to rebuild and largelittle saved for a lot heldA filter over a file rebuilds in seconds, yet its cache takes memory that a costly join result could have used.
- Wider than the actions needevery column is storedA cache holds all columns of its DataFrame. If the actions read 3 of 40, a select first stores 3 columns instead of 40.
- Too big for its levelmemory held, work repeatedAt MEMORY_ONLY, partitions that do not fit are recomputed on every read, so the job holds memory and still repeats the work.
- A joined fact table read by 4 reports
- A per-user aggregate reused by 3 model features
- A cleaned lookup table feeding several joins in 1 job
- A raw file read cached before a single query
- A 200-column table cached when the reports read 5
- A per-day cache from an earlier loop pass still held during the next pass
- ▸Will 2 or more actions read this DataFrame?
- ▸Does rebuilding it run a join, a shuffle or a slow read?
- ▸After a
selectof the columns the actions read, will it fit at the level you chose?
After a run, the Storage tab answers the third question. A Size on Disk you did not plan for, or a Fraction Cached under 100% at MEMORY_ONLY, means the cache did not fit and some reads turned into disk reads or recomputes.
Executor memory can hold 1 of these 2 DataFrames as a cache, but not both. Each would be read by 3 actions. Which one should the job cache?
A cached DataFrame stays stored until 1 of 3 things happens: your code calls unpersist() on it, the application ends, or Spark needs the room for something else. Spark cannot tell that your code has moved on, so a variable that no later line reads frees nothing. In a scheduled job that runs for an hour, a forgotten cache holds memory for the rest of the run, and the joins and aggregations on those executors have less memory to work with while it is held.
unpersist() takes effect at once
cache() is lazy, but unpersist() is eager: when the line runs, Spark tells every executor to drop that DataFrame's stored partitions. By default it returns without waiting for the drop to finish, and unpersist(blocking=True) waits. The DataFrame itself still works afterwards. The next action on it recomputes from the source, exactly as if it had never been cached.
Run the cell: bulk is cached, filled by count(), then released, and the final total still comes back correct because Spark recomputes bulk from transactions for it.
transactions = spark.table("transactions")
bulk = transactions.filter(F.col("quantity") > 1).cache()
bulk.count()
bulk.unpersist()
bulk.agg(F.sum("total_amount").alias("bulk_revenue")).collect()Three places to put the call
| Where visits.unpersist() sits | The 2 reports | Memory while baskets builds |
|---|---|---|
| right after `visits.count()` | both recompute the join from the source | free |
| after the by_device write | both read the cache | free |
| nowhere | both read the cache | still holding `visits` |
The first row is the costly mistake: the cache is filled, released and never read, so the job pays for storing it and then runs the join 2 more times. The last row costs memory instead of time: baskets competes with data nobody reads, and some of its partitions can end up on local disk or recomputed.
Releasing everything, and checking
spark.catalog.clearCache() drops every cached DataFrame and table in the session in 1 call. It fits the end of a notebook, or the boundary between 2 phases that share nothing. Inside a job where another part still reads a cache, it throws away work you meant to keep, so release by name there.
| Check | Shows | After cache() | After unpersist() |
|---|---|---|---|
| `df.is_cached` | whether the DataFrame carries the cache mark | `True`, even before any action | `False` |
| `print(df.storageLevel)` | the level the mark asks for | `Disk Memory Deserialized 1x Replicated` | `Serialized 1x Replicated`, meaning no memory and no disk |
| Storage tab | partitions stored and their size | a row once an action fills it | the row is gone |
Calling unpersist() on a DataFrame that was never cached does nothing and raises no error, so a release at the end of a code path is safe even when an earlier branch skipped the cache.
This job combines placement, the first action and release. What happens when it runs?
> A food-delivery marketplace runs a nightly Spark job that joins 30 days of orders to restaurant and courier tables, then builds 4 outputs from the joined result: city revenue, courier lateness, top dishes and refund rates. After those, the same job builds training features for a demand model from a separate table of menu changes, and that phase needs as much executor memory as it can get.
.cache() goes there, after a select of the columns the outputs read.count() right after the cache fills every partition, so all 4 outputs start from stored data and none of them pays for the join.MEMORY_ONLY.unpersist() right after the fourth output frees the memory before the feature phase starts, instead of leaving 30 days of orders in place until the job ends.cache() is persist() at the default level. Both are lazy, and the first action that computes a partition stores it.count(). show(), take() and first() store only the partitions they compute.MEMORY_ONLY when a recompute is a short scan, MEMORY_AND_DISK when it reruns a join or shuffle.select of the columns those actions use.unpersist() is eager: call it after the last read and before the next phase needs the memory.Cache the DataFrame your actions share, choose where it lives, and release it once the last reader is done.
- Category
- Spark
- Difficulty
- beginner
- Duration
- 20 minutes
- Challenges
- 16 hands-on challenges
Topics covered: Where to Put the Cache, cache() vs persist(), Storage Levels, When Caching Wastes Memory, Releasing with unpersist()
Lesson Sections
- Where to Put the Cache (concepts: Caching & Persistence)
Every action you call runs the full chain of transformations behind its DataFrame, starting from the source files. Spark keeps no result between actions unless you tell it to. A cache is that instruction: it stores the partitions of 1 DataFrame on the executors, so later actions start from the stored partitions and skip every step that built them. The decision that matters most is which DataFrame in the chain gets stored. A job with 3 actions Here is a daily job that reads click events, keeps 2
- cache() vs persist() (concepts: Caching & Persistence)
Marking is free, the first action pays The first action sets how much is stored Checking the cache is in the plan
- Storage Levels (concepts: 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. 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 dif
- When Caching Wastes Memory (concepts: Caching & Persistence)
A cache has 3 costs. The first action stores each partition as it goes, which makes it slower. The stored partitions take executor memory, or local disk, for as long as the cache exists. And when the cache does not fit, part of it is recomputed or read from disk anyway. A cache earns its place only when the later actions save more time than those costs add. Pricing a single cache The saving grows with every action that reads the cache and with every minute of work behind it. With only 1 action,
- Releasing with unpersist() (concepts: Caching & Persistence)
Notebooks make this worse. Picture a session on a cluster whose executors can hold about 400GB of cached data. Four cells each cache a 90GB result, and none is read again after its cell. By the fifth cell, 360GB is held for nobody. Storing the fifth result first means moving older partitions out of memory, at the default level by writing them to local disk, so the fifth cell pays to save data that is already dead. unpersist() takes effect at once That timing decides where the call goes: after th