Persist and Caching
The methods persist() and cache() in Apache Spark are used to save the RDD, DataFrame, or Dataset in memory for faster access during computation. They are effectively ways to optimize the execution of your Spark jobs, especially when you have repeated transformations on the same data. However, they differ in how they handle the storage:
cache()#
This method is a shorthand for using persist() with the default storage level. In other words, cache() is equivalent to calling persist() without any parameters. The default storage level is MEMORY_AND_DISK in PySpark and MEMORY_AND_DISK_SER in Scala Spark. This means that the RDD, DataFrame, or Dataset is stored in memory and, if it doesn't fit, the excess partitions are stored on disk.
persist(storageLevel)#
This method allows you to control how the data should be stored. You can pass a storage level as an argument to the persist() function, which gives you finer control over how the data is persisted. The storage level can be one of several options, each of which offers different trade-offs of memory usage and CPU efficiency, and can use either memory or disk storage or both.
Storage Levels in Persist
a. StorageLevel.DISK_ONLY: Store the RDD partitions only on disk.
b. StorageLevel.DISK_ONLY_2: Same as the DISK_ONLY, but replicate each partition on two cluster nodes.
c. StorageLevel.MEMORY_AND_DISK: Store RDD as deserialized objects in the JVM. If the RDD does not fit in memory, store the partitions that don't fit on disk, and read them from there when they're needed.
d. StorageLevel.MEMORY_AND_DISK_2: Similar to MEMORY_AND_DISK, but replicate each partition on two cluster nodes.
e. StorageLevel.MEMORY_AND_DISK_SER: Store RDD as serialized Java objects (one byte array per partition). This is generally more space-efficient than deserialized objects, especially when using a fast serializer, but more CPU-intensive to read.
f. StorageLevel.MEMORY_AND_DISK_SER_2: Similar to MEMORY_AND_DISK_SER, but replicate each partition on two cluster nodes.
g. StorageLevel.MEMORY_ONLY: Store RDD as deserialized Java objects in the JVM. If the RDD does not fit in memory, some partitions will not be cached and will be recomputed on the fly each time they're needed.
h. StorageLevel.MEMORY_ONLY_2: Similar to MEMORY_ONLY, but replicate each partition on two cluster nodes.
i. StorageLevel.MEMORY_ONLY_SER: Store RDD as serialized Java objects (one byte array per partition). This is more space-efficient, but more CPU-intensive to read.
j. StorageLevel.MEMORY_ONLY_SER_2: Similar to MEMORY_ONLY_SER, but replicate each partition on two cluster nodes.