VOL. 01
← The learning path
FUNDAMENTALS / LESSON 08

Caching & fault tolerance

Reuse the right work, and know what Spark can recompute.

THE IDEA TO KEEP

Caching is a reuse decision. Recovery relies on the ability to recompute missing work from available inputs.

Cache a reusable boundary

If several actions reuse an expensive filtered dataset, persisting that intermediate result may help. Materialization costs time and storage. Compare total runtime, check the Storage tab, and release data when its reuse window ends.

Be precise about defaults

In PySpark 3.5.7, DataFrame cache() uses MEMORY_AND_DISK_DESER. Do not transfer this default to every RDD API or version. Storage settings and eviction determine what remains available for reuse.

Recovery has boundaries

Spark can recompute lost RDD partitions using lineage. That requires usable upstream inputs; it does not promise that every external side effect runs exactly once. Retry-aware writes, deterministic logic, and appropriate transactional sinks are separate design decisions.

Try it on a small dataset

Run this in a PySpark notebook with an existing spark session. Create a local session if needed. Examples are illustrative; this website does not execute Spark.

PYTHON / PYSPARK
from pyspark.sql import functions as F
reusable = spark.range(100).filter(F.col("id") % 2 == 0).cache()
try:
    print(reusable.count())  # materializes this small example
    reusable.agg(F.sum("id").alias("total")).show()
finally:
    reusable.unpersist()
Expected result

count: 50 · total: 2450

Result reasoned through on the small input; not runtime-tested against a Spark cluster.

Watch the assumption

Calling cache() alone does not eagerly materialize the dataset. Cached data can also be evicted or lost.

EXPLAIN IT BACK

Is caching every DataFrame a good way to speed up my pipeline?

Say your answer out loud before opening the notes.

Reveal the reasoning

No. Identify repeated expensive computation, memory capacity, and reuse lifetime. Caching one-use intermediates can add overhead and pressure. Measure, inspect storage, and unpersist deliberately.

Sources & verification

Core claims checked against Apache Spark 3.5.7 documentation and source references on 2026-09-13. Explanations, scenarios, and diagrams are original teaching material. Other releases and managed runtimes can differ.

Find a concept