Caching vs. checkpointing

Keeping a result around versus cutting the lineage that produced it: two different tools for two different problems.

The rule

and persist keep a computed result in memory or on disk, but preserve the that produced it. If a cached partition is lost, Spark recomputes it from the original plan, exactly as if the cache had never existed.

writes the data to reliable storage and truncates the lineage. The plan is cut at that point; nothing upstream of a checkpoint can ever be re-executed to reproduce it, because Spark no longer remembers how to.

Why it costs what it costs

Caching is lazy: cache() and persist() register a storage request; partitions are populated when an action computes them. If a materialized dataframe is used only once, storing it usually adds overhead without any later reuse savings.

A long lineage, as commonly happens in iterative algorithms, makes recomputation on failure expensive: an executor loss late in a chain of 50 shuffles can force Spark to re-run most of that chain. checkpoint exists specifically to bound how far back a recompute can go.

localCheckpoint, the fast but non-durable option

also truncates lineage, but writes to executor-local storage instead of reliable distributed storage. It's faster because there's no network write to durable storage, but it is not : if the executor holding that data is lost, the data is gone and cannot be recomputed, since the lineage that would have allowed recomputation was already cut.

This is the right fix for runaway lineage in iterative or DAG-engine code where fault tolerance across executor loss is less of a concern than keeping the plan from growing unbounded.

cache vs. checkpoint vs. localCheckpoint

cache / persistcheckpointlocalCheckpoint
LineagePreservedTruncatedTruncated
StorageMemory or local diskReliable distributed storageExecutor-local storage
Fault-tolerantYes, via recomputeYesNo
Typical useDataframe reused multiple timesBreaking a long lineage durablyBreaking a long lineage fast, tolerating data loss on executor failure

When to actually cache

Cache only when a dataframe is read more than once downstream and the cost of recomputing it exceeds the cost of holding it in memory. A dataframe used exactly once gains nothing from caching; the cache write cost is wasted.

Evictions make this worse than not caching at all: Spark spends time managing a cache entry that gets evicted before its second read, then recomputes anyway on that second read, paying both the caching overhead and the full recompute cost.

Cost / gotcha

Caching a dataframe materialized for only one use usually adds storage overhead without reuse savings.

Streaming checkpoints are a different thing

A streaming query's checkpoint stores offsets (what has been read from the source) and operator state (aggregation buffers, join buffers), so a restart resumes exactly where the query stopped rather than reprocessing from the beginning.

Changing the query, for example adding a new aggregation or changing a join's shape, can make an existing checkpoint incompatible with the new plan. Deleting a checkpoint to work around an incompatibility silently re-reads from the source according to the configured starting offset, which can mean reprocessing data the sink has already seen.

Cost / gotcha

Deleting a broken streaming checkpoint is rarely free: it is a decision about how much history gets reprocessed, made silently.

Spark UI evidence

The Storage tab shows cached RDDs/dataframes, their memory and disk usage, and fraction cached, letting you see whether a cache was actually fully materialized or partially evicted. For checkpoints, the configured checkpoint directory shows the written files directly, separate from any Spark UI tab.

Worked example

What happens when a stored partition disappears?

Compare recovery paths for Spark batch computations. Streaming checkpoints instead track query progress and possibly operator state.

SituationRecovery pathImportant dependency
Cached partition evictedRecompute the missing partitionRetained lineage and readable inputs
Reliable checkpoint retainedRead materialized checkpoint filesDurable checkpoint storage
localCheckpoint block lostRecovery may failEarlier lineage was cut; no durable copy is guaranteed

What to notice: Cache eviction can trigger recomputation. A reliable checkpoint cuts that earlier lineage by establishing a durable recovery boundary; localCheckpoint cannot promise the same resilience.

Practice / self-test

1. A cached dataframe shows 40% cached in the Storage tab. What happens on the next read of the uncached 60%?

Reveal answer

Spark recomputes that portion from the original lineage, since caching preserves lineage as a fallback for anything not actually resident in the cache.

2. Why does checkpoint truncate lineage but cache does not?

Reveal answer

checkpoint is designed to bound recomputation by writing a durable copy and discarding the plan that produced it. cache is designed to avoid recomputation when possible while still guaranteeing correctness if the cached copy is lost, which requires keeping the lineage as a fallback.

3. When would you choose localCheckpoint over checkpoint?

Reveal answer

When lineage has grown so long that planning or recomputation becomes a problem (common in iterative algorithms), and you can tolerate losing that data on executor failure, since localCheckpoint is faster but not fault-tolerant.

4. You delete a streaming checkpoint because the query is failing to start. What actually happens next?

Reveal answer

The query loses its record of processed offsets and state, and restarts reading from the source according to the configured starting point, which usually means reprocessing data that was already delivered to the sink.

5. A dataframe is read exactly once downstream. Should you cache it?

Reveal answer

No. Caching has a write cost and no second read to benefit from it, so it is pure overhead with no corresponding savings.