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.

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

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"))

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.