How to Use Checpoint in PySpark

PySpark is a popular open-source framework used for processing large amounts of data. It is built on top of the Apache Spark framework and provides a high-level API for distributed data processing. Checkpoints in PySpark are used to reduce the risk of job failures due to out of memory errors. In this blog post, we will explain what checkpoints are and how to use them in PySpark with an example.

What are Checkpoints in PySpark?

Checkpoints in PySpark are a mechanism to store intermediate RDD (Resilient Distributed Datasets) results to disk. By storing intermediate results to disk, PySpark can avoid recomputing the entire RDD lineage when a node fails due to out-of-memory errors. This mechanism is particularly useful when working with large RDDs that do not fit in memory.

Checkpointing is a costly operation, as it involves writing data to disk. Therefore, it should be used judiciously, only when necessary, and only on large RDDs that are likely to cause out-of-memory errors. Checkpoints also increase the complexity of the execution plan, which can lead to slower job execution times.

How to use Checkpointing in PySpark?

In PySpark, checkpointing can be enabled on an RDD by calling the checkpoint() method on the RDD. This method takes a string parameter that specifies the path to the directory where the checkpoint data will be stored.

Here is an example of how to use checkpointing in PySpark:

from pyspark import SparkContext, SparkConf

conf = SparkConf().setAppName("Checkpoint Example").setMaster("local[*]")
sc = SparkContext(conf=conf)


# create an RDD and checkpoint it
rdd = sc.parallelize(range(1000000)).map(lambda x: x*x)

# perform some action on the RDD

# stop the SparkContext

In this example, we create an RDD by parallelizing a range of integers and then squaring each integer using the map() transformation. We then call the checkpoint() method on the RDD to enable checkpointing and specify the checkpoint directory using the setCheckpointDir() method. Finally, we perform an action on the RDD by calling the count() method, which triggers the computation of the RDD lineage.

When an RDD is checkpointed, PySpark stores the RDD data to disk and also stores the RDD lineage. This stored lineage can be used to recover the RDD in case of a node failure. When an RDD is recovered from a checkpoint, PySpark does not need to recompute the entire lineage, which can save a significant amount of computation time


Checkpoints in PySpark are a useful mechanism to store intermediate RDD results to disk, which can reduce the risk of job failures due to out-of-memory errors. However, checkpointing is a costly operation and should only be used when necessary. By enabling checkpointing on an RDD, PySpark stores the RDD data and lineage to disk, which can be used to recover the RDD in case of a node failure.

Leave a Reply