Built with Tutify.io

    AIStudyLabs

    Spark Persistence Levels

    Spark Persistence Levels

    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

    Introduction to PySpark for Data Engineering — Cache, Persistence and Shared Variables

    Introduction to PySpark

    Setup PySpark Environment

    SparkSession & SparkContext

    Resilient Distributed Datasets (RDDs)

    Apache Spark Concepts

    Cache, Persistence and Shared Variables

    DataFrame Introduction

    DataFrame Narrow Transformation

    DataFrame Wider Transformation

    DataFrame Actions

    PySpark SQL Functions

    Advanced DataFrame Features

    PySpark SQL

    Spark Read & Write CSV Files

    Working with JSON Files

    Working with Parquet Files

    What Next?

    Tutorial Not Found