Checkpoint Frequency
Concepts covered: Caching & Persistence
Every checkpoint is a bet. You pay its write cost on every run and collect only on runs where something fails after it. Checkpoint too rarely and a failure throws away hours; too often and the writes cost more than the failures they insure against. The interval follows from 2 numbers you can measure: what a single checkpoint costs, and how often the failure it guards against happens.
Which failure is the checkpoint for?
| Failure | How Spark recovers | What a checkpoint saves |
|---|---|---|
| Task failure | Retries the task from its stage's inputs | Nothing: the retry reads input that already exists |
| Executor or node loss | Recomputes lost partitions from lineage, plus shuffle files lost with a node | The walk back through a long lineage |
| Driver loss | It does not: the application ends | Everything since the last reliable write, if the rerun resumes from it |
Task failures need no checkpoint. Executor loss needs one only where lineage runs long, mostly in iterative loops. The failure that sets a pipeline's interval is the one that restarts it from its last reliable write: a lost driver, a cluster terminated mid-run, a spot reclaim of the driver's node.
Young's interval
In 1974 John Young published a first-order approximation of the checkpoint interval that minimizes expected lost time: T = sqrt(2 × C × M), where C is the cost of a single checkpoint and M is the mean time between restart-forcing failures. It balances 2 losses: each checkpoint costs C, and each failure throws away about half an interval of work.
Work an example. A daily pipeline runs 6 hours of compute, writing its intermediate state to reliable storage costs 5 minutes, and the run history shows a restart every 8 hours on average. Young gives sqrt(2 × 5 × 480) minutes, roughly 69, so cut the 6 hours into 5 segments of 72 minutes.
| Checkpoint plan | Segments | Run time, no failures | Expected run time |
|---|---|---|---|
| None | 1 | 6.00 h | 8.94 h |
| Every 3 hours | 2 | 6.17 h | 7.52 h |
| Every 72 minutes (Young) | 5 | 6.42 h | 6.96 h |
| Every 10 minutes (habit) | 36 | 9.00 h | 9.14 h |
The expected times use the standard model of failures arriving at random, in which a segment of length S, checkpoint included, takes M × (e^(S/M)-1) on average, assuming instant restarts. Checkpointing every 10 minutes loses to not checkpointing at all, because 36 writes add 3 hours to every run whether anything fails or not. The curve is also flat near the optimum: 3-hour segments cost only about half an hour more than Young's, so a rough M is good enough.
Inside a job: lineage depth
Executor loss inside an iterative job follows the same logic at smaller scale. Iteration 30 depends on all 29 before it, so after a node loss Spark may recompute lost partitions back to iteration 1. With 40 spot executors, each reclaimed every 20 hours on average, the job loses an executor every 30 minutes. Taking the recompute as the lost work and 1 minute as the cost of a reliable checkpoint, sqrt(2 × 1 × 30) gives about 8 minutes: with 3-minute iterations, a checkpoint every 3 iterations. Use a reliable checkpoint here, since a local checkpoint keeps its blocks on the very executors spot reclaims take away.
spark.sparkContext.setCheckpointDir("/chk/rank_iterations")
ranks = initial_ranks
for i in range(1, 31):
ranks = update(ranks, links)
if i % 3 == 0:
ranks = ranks.checkpoint()The same reasoning sets how often a multi-step pipeline writes intermediate tables. Each write splits the pipeline into steps an orchestrator can rerun on their own, which caps what a failure costs, and each write costs storage plus a scan for the next step. Materialize where the work behind the write would hurt to repeat, and skip writes that protect a few minutes of compute.
A 20-hour job faces a restart-forcing failure every 50 hours on average, and a reliable checkpoint costs 4 minutes. Roughly how often should it checkpoint?
- Every 4 minutes, matching the interval to the checkpoint's cost
- Every 2.5 hours, the square root of 2 × 4 × 3,000 minutes
- Every 25 hours, half the mean time between failures
- Never, since a failure every 50 hours is too rare to matter
About this lesson
This section is part of the Fault Tolerance: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.