PySpark Persistence Storage Levels
All different storage levels supported by PySpark are available in the pyspark.StorageLevel module. The storage level specifies how and where to persist or cache a PySpark DataFrame.
MEMORY_ONLY – This is the default behavior of the RDD cache() method and stores the RDD or DataFrame as deserialized objects in JVM memory. When there is not enough memory available, it will not save RDD of some partitions and these will be re-computed as and when required. This takes more memory.
MEMORY_ONLY_SER – This is the same as MEMORY_ONLY, but the difference is that it stores RDD as serialized objects in JVM memory. It takes less memory (space-efficient) than MEMORY_ONLY as it saves objects as serialized and takes additional CPU cycles to deserialize.
MEMORY_ONLY_2 – Same as MEMORY_ONLY storage level but replicates each partition to two cluster nodes.
MEMORY_ONLY_SER_2 – Same as MEMORY_ONLY_SER storage level but replicates each partition to two cluster nodes.
MEMORY_AND_DISK – This is the default behavior of the DataFrame or Dataset. In this Storage Level, the DataFrame will be stored in JVM memory as a deserialized object. When required storage is greater than available memory, it stores some of the excess partitions into the disk and reads the data from the disk when required. It is slower as there is I/O involved.
MEMORY_AND_DISK_SER – This is the same as MEMORY_AND_DISK storage level, with the difference being it serializes the DataFrame objects in memory and on disk when space is not available.
MEMORY_AND_DISK_2 – Same as MEMORY_AND_DISK storage level but replicates each partition to two cluster nodes.
MEMORY_AND_DISK_SER_2 – Same as MEMORY_AND_DISK_SER storage level but replicates each partition to two cluster nodes.
DISK_ONLY – In this storage level, DataFrame is stored only on disk and the CPU computation time is high as I/O is involved.
DISK_ONLY_2 – Same as DISK_ONLY storage level but replicates each partition to two cluster nodes.
Below is the table representation of the Storage level. Review the impact of space usage, CPU time, and performance to choose the one that best fits your needs.
Storage Level Space used CPU time In memory On-disk Serialized Recompute some partitions
----------------------------------------------------------------------------------------------------
MEMORY_ONLY High Low Y N N Y
MEMORY_ONLY_SER Low High Y N Y Y
MEMORY_AND_DISK High Medium Some Some Some N
MEMORY_AND_DISK_SER Low High Some Some Y N
DISK_ONLY Low High N Y Y N
Some Points to Note on Persistence
- PySpark automatically monitors every
persist()andcache()call, checking usage on each node, and drops persisted data if not used using the least-recently-used (LRU) algorithm. You can also manually remove using theunpersist()method. - PySpark caching and persistence are optimization techniques to improve the performance of PySpark jobs.
- For RDD, the
cache()default storage level isMEMORY_ONLY, but for DataFrame and Dataset, the default isMEMORY_AND_DISK. - On the Spark UI, the Storage tab shows where partitions exist in memory or disk across the cluster.
- Dataset
cache()is an alias forpersist(StorageLevel.MEMORY_AND_DISK). - Caching of PySpark DataFrame or Dataset is a lazy operation, meaning a DataFrame will not be cached until you trigger an action.
