What is the difference between cache and persist?

Apache SparkDistributed ComputingRdd

Apache Spark Problem Overview


In terms of RDD persistence, what are the differences between cache() and persist() in spark ?

Apache Spark Solutions


Solution 1 - Apache Spark

With cache(), you use only the default storage level :

  • MEMORY_ONLY for RDD
  • MEMORY_AND_DISK for Dataset

With persist(), you can specify which storage level you want for both RDD and Dataset.

From the official docs:

> - You can mark an RDD to be persisted using the persist() or cache() methods on it. > - each persisted RDD can be stored using a different storage level > - The cache() method is a shorthand for using the default storage level, which is StorageLevel.MEMORY_ONLY (store deserialized objects in memory).

Use persist() if you want to assign a storage level other than :

  • MEMORY_ONLY to the RDD
  • or MEMORY_AND_DISK for Dataset

Interesting link for the official documentation : which storage level to choose

Solution 2 - Apache Spark

> The difference between cache and persist operations is purely > syntactic. cache is a synonym of persist or persist(MEMORY_ONLY), i.e. > cache is merely persist with the default storage level MEMORY_ONLY

> But Persist() > We can save the intermediate results in 5 storage levels. > > 1. MEMORY_ONLY > 2. MEMORY_AND_DISK > 3. MEMORY_ONLY_SER > 4. MEMORY_AND_DISK_SER > 5. DISK_ONLY


> /** * Persist this RDD with the default storage level > (MEMORY_ONLY). /
def persist(): this.type = > persist(StorageLevel.MEMORY_ONLY) > > /
* * Persist this RDD with the default storage level > (MEMORY_ONLY). */
def cache(): this.type = persist()

see more details here...


Caching or persistence are optimization techniques for (iterative and interactive) Spark computations. They help saving interim partial results so they can be reused in subsequent stages. These interim results as RDDs are thus kept in memory (default) or more solid storage like disk and/or replicated. RDDs can be cached using cache operation. They can also be persisted using persist operation.

> #persist, cache > > These functions can be used to adjust the storage level of a RDD. > When freeing up memory, Spark will use the storage level identifier to > decide which partitions should be kept. The parameter less variants > persist() and cache() are just abbreviations for > persist(StorageLevel.MEMORY_ONLY).

> Warning: Once the storage level has been changed, it cannot be changed again!


Warning -Cache judiciously... see (https://stackoverflow.com/questions/28981359/why-do-we-need-to-call-cache-or-persist-on-a-rdd)

Just because you can cache a RDD in memory doesn’t mean you should blindly do so. Depending on how many times the dataset is accessed and the amount of work involved in doing so, recomputation can be faster than the price paid by the increased memory pressure.

It should go without saying that if you only read a dataset once there is no point in caching it, it will actually make your job slower. The size of cached datasets can be seen from the Spark Shell..

Listing Variants...

def cache(): RDD[T]
 def persist(): RDD[T]
 def persist(newLevel: StorageLevel): RDD[T]

See below example :

val c = sc.parallelize(List("Gnu", "Cat", "Rat", "Dog", "Gnu", "Rat"), 2)
     c.getStorageLevel
     res0: org.apache.spark.storage.StorageLevel = StorageLevel(false, false, false, false, 1)
     c.cache
     c.getStorageLevel
     res2: org.apache.spark.storage.StorageLevel = StorageLevel(false, true, false, true, 1)

enter image here

Note : Due to the very small and purely syntactic difference between caching and persistence of RDDs the two terms are often used interchangeably.

See more visually here....

Persist in memory and disk:

enter image description here

Cache

Caching can improve the performance of your application to a great extent.

enter image description here

Solution 3 - Apache Spark

There is no difference. From RDD.scala.

/** Persist this RDD with the default storage level (`MEMORY_ONLY`). */
def persist(): this.type = persist(StorageLevel.MEMORY_ONLY)

/** Persist this RDD with the default storage level (`MEMORY_ONLY`). */
def cache(): this.type = persist()

Solution 4 - Apache Spark

Spark gives 5 types of Storage level

  • MEMORY_ONLY
  • MEMORY_ONLY_SER
  • MEMORY_AND_DISK
  • MEMORY_AND_DISK_SER
  • DISK_ONLY

cache() will use MEMORY_ONLY. If you want to use something else, use persist(StorageLevel.<*type*>).

By default persist() will store the data in the JVM heap as unserialized objects.

Solution 5 - Apache Spark

Cache() and persist() both the methods are used to improve performance of spark computation. These methods help to save intermediate results so they can be reused in subsequent stages.

The only difference between cache() and persist() is ,using Cache technique we can save intermediate results in memory only when needed while in Persist() we can save the intermediate results in 5 storage levels(MEMORY_ONLY, MEMORY_AND_DISK, MEMORY_ONLY_SER, MEMORY_AND_DISK_SER, DISK_ONLY).

Solution 6 - Apache Spark

For impatient:

Same

Without passing argument, persist() and cache() are the same, with default settings:

  • when RDD: MEMORY_ONLY
  • when Dataset: MEMORY_AND_DISK
Difference:

Unlike cache(), persist() allows you to pass argument inside the bracket, in order to specify the level:

  • persist(MEMORY_ONLY)
  • persist(MEMORY_ONLY_SER)
  • persist(MEMORY_AND_DISK)
  • persist(MEMORY_AND_DISK_SER )
  • persist(DISK_ONLY )

Voilà!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRamanaView Question on Stackoverflow
Solution 1 - Apache SparkaharsView Answer on Stackoverflow
Solution 2 - Apache SparkRam GhadiyaramView Answer on Stackoverflow
Solution 3 - Apache SparkMike ParkView Answer on Stackoverflow
Solution 4 - Apache SparkketankkView Answer on Stackoverflow
Solution 5 - Apache Sparkuser11332824View Answer on Stackoverflow
Solution 6 - Apache SparkjackView Answer on Stackoverflow