Caching and persistence
| Also known as | cache(), persist(), CACHE TABLE, storage levels
|
|---|---|
| Field | Distributed data processing |
| Key parts | Storage levels, storage memory, in-memory columnar cache, LRU eviction, unpersist()
|
| Open source implementations | Apache Spark |
| Related | Spark memory management, Checkpointing (Spark), RDD, Garbage collection in Spark |
In Apache Spark, caching or persistence keeps the computed partitions of a DataFrame, table or RDD in executor memory or on local disk, so that later actions reuse them instead of recomputing them from the source. persist() takes a storage level that says where and how the data is kept; cache() is persist() with the default level.[1] A cache is fault tolerant: a lost partition is recomputed from its lineage.[1]
Cache vs persist
cache() and persist() differ only in the storage level. The default is different for DataFrames and RDDs:
| API | cache() uses |
Source |
|---|---|---|
| DataFrame / Dataset (Scala, Java) | MEMORY_AND_DISK
|
[2] |
| DataFrame (PySpark) | MEMORY_AND_DISK_DESER, changed to match Scala in 3.0
|
[3] |
SQL CACHE TABLE
|
MEMORY_AND_DISK, unless set with OPTIONS ('storageLevel' ...)
|
[4] |
| RDD | MEMORY_ONLY
|
[1] |
Caching is lazy. cache() only marks the DataFrame; the data is stored the first time an action computes it.[1] CACHE TABLE is the exception: it caches immediately unless written as CACHE LAZY TABLE.[4]
DataFrames and tables are cached in Spark SQL's in-memory columnar format. Spark then scans only the needed columns and picks a compression codec per column (spark.sql.inMemoryColumnarStorage.compressed, default true).[5] Spark 4.3.0 also documents an Apache Arrow based cache format as an alternative.[5] Cached data is shared by all Spark sessions on the cluster.[4]
Storage levels
| Level | Where | If it does not fit |
|---|---|---|
MEMORY_ONLY
|
Memory, deserialized | Partitions that do not fit are not cached and are recomputed when needed |
MEMORY_AND_DISK
|
Memory, deserialized | Partitions that do not fit go to disk |
MEMORY_ONLY_SER, MEMORY_AND_DISK_SER (Java and Scala)
|
Memory, serialized (one byte array per partition) | As above; smaller in memory but more CPU to read |
DISK_ONLY
|
Local disk only | n/a |
..._2 variants, for example MEMORY_AND_DISK_2
|
As above, replicated on two nodes | As above |
OFF_HEAP (experimental)
|
Off-heap memory, serialized; requires off-heap memory to be enabled | n/a |
Source: RDD Programming Guide, Spark 4.3.0.[1]
In Python, stored objects are always serialized with Pickle, so the serialized and deserialized levels behave the same for RDDs.[1]
How to cache in PySpark and SQL
from pyspark import StorageLevel
dim = spark.read.table("main.sales.dim_customer").filter("active")
dim.persist(StorageLevel.MEMORY_AND_DISK)
dim.count() # action that fills the cache
# ... several queries that reuse dim ...
dim.unpersist()
CACHE TABLE active_customers AS SELECT * FROM main.sales.dim_customer WHERE active;
UNCACHE TABLE active_customers;
The Storage tab of the Spark UI lists everything cached in the application with its size in memory and on disk.[5]
Where cached data lives
Cached blocks use storage memory in the executor's unified memory region. Storage and execution borrow from each other, and execution can evict cached blocks down to spark.memory.storageFraction (default 0.5) of the region.[6] When memory is short, Spark drops old partitions in least-recently-used order. unpersist() removes data at once; it does not block unless called with blocking=True.[1] See Spark memory management.
When does caching hurt?
- The data is used once. The RDD guide describes the gain from persisting as reuse: each node keeps the partitions it computes and reuses them in other actions on that dataset or datasets derived from it.[1] If no later action reads the cached data, nothing is reused, and storing the partitions is extra work and memory with no gain.
- It takes memory from execution. Cached data competes with the working memory of tasks; more cache can mean more spill for joins and aggregations.[6]
- It adds GC pressure. The Spark docs name interference between tasks' working memory and cached data as one cause of garbage collection problems, and recommend caching less when the old generation is nearly full: "it is better to cache fewer objects than to slow down task execution".[6] See Garbage collection in Spark.
- It blocks executor scale-down. With dynamic allocation, executors that hold cached data are not removed by default (
spark.dynamicAllocation.cachedExecutorIdleTimeoutis infinite).[7] - Recomputing is cheap. The RDD guide advises not to spill to disk unless the functions that computed the data are expensive or filter a lot of it; otherwise recomputing a partition may be as fast as reading it from disk.[1]
Caching compared with checkpointing
| Caching / persistence | Checkpointing | |
|---|---|---|
| Keeps lineage? | Yes; lost partitions are recomputed from the source[1] | No; the plan is cut and the data is read back from the checkpoint[2] |
| Where | Executor memory or local disk | Reliable storage set with setCheckpointDir (or executor storage for local checkpoints)[2]
|
| Survives executor loss? | Recomputed (or read from a replica with _2 levels)
|
Yes for reliable checkpoints |
See also
References
- ↑ 1.00 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 RDD Programming Guide: RDD Persistence (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 2.0 2.1 2.2 Dataset.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ dataframe.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 4.0 4.1 4.2 CACHE TABLE (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 5.0 5.1 5.2 Performance Tuning (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 6.0 6.1 6.2 Tuning Spark (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Job Scheduling (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.