When Caching Wastes Memory
Concepts covered: 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
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.
- 6 min 10 s
- 12 min 50 s
The saving grows with every action that reads the cache and with every minute of work behind it. With only 1 action, nothing is saved and the storing time is pure loss. That gives the first 2 tests: at least 2 actions must read the DataFrame, and the work behind it must cost more than reading the cache back.
Four ways a cache burns memory
- Read by 1 action: The cache is filled, then never read. The job pays for the storing and holds the memory for no saving.
- Cheap to rebuild and large: A filter over a file rebuilds in seconds, yet its cache takes memory that a costly join result could have used.
- Wider than the actions need: A 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 level: At MEMORY_ONLY, partitions that do not fit are recomputed on every read, so the job holds memory and still repeats the work.
The second case hurts even when the cache saves a little, because caches on an executor share its memory: a cache that saves little can crowd out 1 that saves a lot.
Here is a job that makes 2 of these mistakes, then the version that fixes them:
all_events = spark.read.parquet("/data/events").cache()
march = all_events.filter(F.col("event_date") >= "2024-03-01").select("user_id", "event_type")
march.groupBy("event_type").count().collect()
march.select("user_id").distinct().count()all_events = spark.read.parquet("/data/events")
march = all_events.filter(F.col("event_date") >= "2024-03-01").select("user_id", "event_type").cache()
march.groupBy("event_type").count().collect()
march.select("user_id").distinct().count()The first version stores every column of every day of events, a scan that is cheap to redo, to serve 2 actions that read 2 columns of March, and both actions still rerun the filter and the select on top of it. The second stores only those 2 columns for March, on the DataFrame the 2 actions share.
- Caches that repay their memory
- 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
- Caches that burn it
- 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
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?
recent = spark.read.parquet("/data/page_views").filter(F.col("dur_ms") > 0)
by_user = recent.join(users, "user_id").groupBy("user_id", "country").agg(F.sum("dur_ms").alias("total_ms"))- recent, because it is built first and feeds by_user
- by_user, because a join and a groupBy sit behind it
- recent, because a larger cache saves more work per read
- Neither, because 3 actions are too few to repay a cache
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.