<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Caching_and_persistence</id>
	<title>Caching and persistence - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Caching_and_persistence"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Caching_and_persistence&amp;action=history"/>
	<updated>2026-12-06T00:08:06Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Caching_and_persistence&amp;diff=129&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Caching_and_persistence&amp;diff=129&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:36Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Caching and persistence&lt;br /&gt;
| also_known_as   = &amp;lt;code&amp;gt;cache()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;persist()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;CACHE TABLE&amp;lt;/code&amp;gt;, storage levels&lt;br /&gt;
| field           = Distributed data processing&lt;br /&gt;
| key_parts       = Storage levels, storage memory, in-memory columnar cache, LRU eviction, &amp;lt;code&amp;gt;unpersist()&amp;lt;/code&amp;gt;&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Spark memory management]], [[Checkpointing (Spark)]], [[RDD]], [[Garbage collection in Spark]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/rdd-programming-guide.md, docs/sql-performance-tuning.md, docs/sql-ref-syntax-aux-cache-cache-table.md, docs/tuning.md, docs/job-scheduling.md, sql/api Dataset.scala, python/pyspark/sql/dataframe.py)}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], &amp;#039;&amp;#039;&amp;#039;caching&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;persistence&amp;#039;&amp;#039;&amp;#039; 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. &amp;lt;code&amp;gt;persist()&amp;lt;/code&amp;gt; takes a storage level that says where and how the data is kept; &amp;lt;code&amp;gt;cache()&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;persist()&amp;lt;/code&amp;gt; with the default level.&amp;lt;ref name=&amp;quot;rddguide&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/rdd-programming-guide.html#rdd-persistence |title=RDD Programming Guide: RDD Persistence (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; A cache is fault tolerant: a lost partition is recomputed from its lineage.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Cache vs persist ==&lt;br /&gt;
&amp;lt;code&amp;gt;cache()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;persist()&amp;lt;/code&amp;gt; differ only in the storage level. The default is different for DataFrames and RDDs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! API !! &amp;lt;code&amp;gt;cache()&amp;lt;/code&amp;gt; uses !! Source&lt;br /&gt;
|-&lt;br /&gt;
| DataFrame / Dataset (Scala, Java)&lt;br /&gt;
| &amp;lt;code&amp;gt;MEMORY_AND_DISK&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;ref name=&amp;quot;dataset&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/api/src/main/scala/org/apache/spark/sql/Dataset.scala |title=Dataset.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| DataFrame (PySpark)&lt;br /&gt;
| &amp;lt;code&amp;gt;MEMORY_AND_DISK_DESER&amp;lt;/code&amp;gt;, changed to match Scala in 3.0&lt;br /&gt;
| &amp;lt;ref name=&amp;quot;pydf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/dataframe.py |title=dataframe.py (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| SQL &amp;lt;code&amp;gt;CACHE TABLE&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;MEMORY_AND_DISK&amp;lt;/code&amp;gt;, unless set with &amp;lt;code&amp;gt;OPTIONS (&amp;#039;storageLevel&amp;#039; ...)&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;ref name=&amp;quot;cachetable&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-ref-syntax-aux-cache-cache-table.html |title=CACHE TABLE (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| RDD&lt;br /&gt;
| &amp;lt;code&amp;gt;MEMORY_ONLY&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Caching is lazy. &amp;lt;code&amp;gt;cache()&amp;lt;/code&amp;gt; only marks the DataFrame; the data is stored the first time an action computes it.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; &amp;lt;code&amp;gt;CACHE TABLE&amp;lt;/code&amp;gt; is the exception: it caches immediately unless written as &amp;lt;code&amp;gt;CACHE LAZY TABLE&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;cachetable&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
DataFrames and tables are cached in Spark SQL&amp;#039;s in-memory columnar format. Spark then scans only the needed columns and picks a compression codec per column (&amp;lt;code&amp;gt;spark.sql.inMemoryColumnarStorage.compressed&amp;lt;/code&amp;gt;, default true).&amp;lt;ref name=&amp;quot;perf&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-performance-tuning.html |title=Performance Tuning (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Spark 4.3.0 also documents an Apache Arrow based cache format as an alternative.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt; Cached data is shared by all Spark sessions on the cluster.&amp;lt;ref name=&amp;quot;cachetable&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Storage levels ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Level !! Where !! If it does not fit&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;MEMORY_ONLY&amp;lt;/code&amp;gt;&lt;br /&gt;
| Memory, deserialized&lt;br /&gt;
| Partitions that do not fit are not cached and are recomputed when needed&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;MEMORY_AND_DISK&amp;lt;/code&amp;gt;&lt;br /&gt;
| Memory, deserialized&lt;br /&gt;
| Partitions that do not fit go to disk&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;MEMORY_ONLY_SER&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;MEMORY_AND_DISK_SER&amp;lt;/code&amp;gt; (Java and Scala)&lt;br /&gt;
| Memory, serialized (one byte array per partition)&lt;br /&gt;
| As above; smaller in memory but more CPU to read&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;DISK_ONLY&amp;lt;/code&amp;gt;&lt;br /&gt;
| Local disk only&lt;br /&gt;
| n/a&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;..._2&amp;lt;/code&amp;gt; variants, for example &amp;lt;code&amp;gt;MEMORY_AND_DISK_2&amp;lt;/code&amp;gt;&lt;br /&gt;
| As above, replicated on two nodes&lt;br /&gt;
| As above&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;OFF_HEAP&amp;lt;/code&amp;gt; (experimental)&lt;br /&gt;
| Off-heap memory, serialized; requires off-heap memory to be enabled&lt;br /&gt;
| n/a&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;small&amp;gt;Source: RDD Programming Guide, Spark 4.3.0.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&amp;lt;/small&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Python, stored objects are always serialized with Pickle, so the serialized and deserialized levels behave the same for RDDs.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to cache in PySpark and SQL ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pyspark import StorageLevel&lt;br /&gt;
&lt;br /&gt;
dim = spark.read.table(&amp;quot;main.sales.dim_customer&amp;quot;).filter(&amp;quot;active&amp;quot;)&lt;br /&gt;
dim.persist(StorageLevel.MEMORY_AND_DISK)&lt;br /&gt;
dim.count()          # action that fills the cache&lt;br /&gt;
&lt;br /&gt;
# ... several queries that reuse dim ...&lt;br /&gt;
&lt;br /&gt;
dim.unpersist()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
CACHE TABLE active_customers AS SELECT * FROM main.sales.dim_customer WHERE active;&lt;br /&gt;
UNCACHE TABLE active_customers;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Storage tab of the Spark UI lists everything cached in the application with its size in memory and on disk.&amp;lt;ref name=&amp;quot;perf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Where cached data lives ==&lt;br /&gt;
Cached blocks use &amp;#039;&amp;#039;&amp;#039;storage memory&amp;#039;&amp;#039;&amp;#039; in the executor&amp;#039;s unified memory region. Storage and execution borrow from each other, and execution can evict cached blocks down to &amp;lt;code&amp;gt;spark.memory.storageFraction&amp;lt;/code&amp;gt; (default 0.5) of the region.&amp;lt;ref name=&amp;quot;tuning&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/tuning.html |title=Tuning Spark (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; When memory is short, Spark drops old partitions in least-recently-used order. &amp;lt;code&amp;gt;unpersist()&amp;lt;/code&amp;gt; removes data at once; it does not block unless called with &amp;lt;code&amp;gt;blocking=True&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; See [[Spark memory management]].&lt;br /&gt;
&lt;br /&gt;
== When does caching hurt? ==&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;The data is used once.&amp;#039;&amp;#039;&amp;#039; 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.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt; If no later action reads the cached data, nothing is reused, and storing the partitions is extra work and memory with no gain.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;It takes memory from execution.&amp;#039;&amp;#039;&amp;#039; Cached data competes with the working memory of tasks; more cache can mean more [[Spill (memory)|spill]] for joins and aggregations.&amp;lt;ref name=&amp;quot;tuning&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;It adds GC pressure.&amp;#039;&amp;#039;&amp;#039; The Spark docs name interference between tasks&amp;#039; working memory and cached data as one cause of garbage collection problems, and recommend caching less when the old generation is nearly full: &amp;quot;it is better to cache fewer objects than to slow down task execution&amp;quot;.&amp;lt;ref name=&amp;quot;tuning&amp;quot; /&amp;gt; See [[Garbage collection in Spark]].&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;It blocks executor scale-down.&amp;#039;&amp;#039;&amp;#039; With [[dynamic allocation]], executors that hold cached data are not removed by default (&amp;lt;code&amp;gt;spark.dynamicAllocation.cachedExecutorIdleTimeout&amp;lt;/code&amp;gt; is infinite).&amp;lt;ref name=&amp;quot;jobsched&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/job-scheduling.html |title=Job Scheduling (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Recomputing is cheap.&amp;#039;&amp;#039;&amp;#039; 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.&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Caching compared with checkpointing ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! Caching / persistence !! [[Checkpointing (Spark)|Checkpointing]]&lt;br /&gt;
|-&lt;br /&gt;
! Keeps lineage?&lt;br /&gt;
| Yes; lost partitions are recomputed from the source&amp;lt;ref name=&amp;quot;rddguide&amp;quot; /&amp;gt;&lt;br /&gt;
| No; the plan is cut and the data is read back from the checkpoint&amp;lt;ref name=&amp;quot;dataset&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Where&lt;br /&gt;
| Executor memory or local disk&lt;br /&gt;
| Reliable storage set with &amp;lt;code&amp;gt;setCheckpointDir&amp;lt;/code&amp;gt; (or executor storage for local checkpoints)&amp;lt;ref name=&amp;quot;dataset&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Survives executor loss?&lt;br /&gt;
| Recomputed (or read from a replica with &amp;lt;code&amp;gt;_2&amp;lt;/code&amp;gt; levels)&lt;br /&gt;
| Yes for reliable checkpoints&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Spark memory management]]&lt;br /&gt;
* [[Checkpointing (Spark)]]&lt;br /&gt;
* [[Garbage collection in Spark]]&lt;br /&gt;
* [[RDD]]&lt;br /&gt;
* [[Dynamic allocation]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>