Sub topic 3
How checkpoints give you each file exactly once
This chapter assumes you know Spark Structured Streaming basics (readStream, writeStream), Delta tables, and cloud storage paths. It builds on the cloudFiles source from the previous chapter.
The problem a checkpoint solves
Auto Loader must answer one question on every run: which files are new? If it only listed the folder, it would have no memory of what it had already loaded. A restart would either reload everything or skip files. The checkpoint is that memory.
What the checkpoint stores
You set the checkpoint with the checkpointLocation option on writeStream. It is a folder in storage that Spark writes to as the stream runs. It holds two kinds of state:
- Progress of the stream: which micro-batches were planned and which were committed to the sink.
- For Auto Loader, a key-value store (RocksDB) that records the files discovered so far, keyed by file path.
Auto Loader uses this store to skip any file it has already seen. That is how a file is ingested once, even when the folder holds millions of files and the stream restarts many times.
Why this gives exactly-once results
Two pieces work together:
- The checkpoint records which files belong to each micro-batch before processing starts. After a failure, Spark replays the same batch with the same files.
- The Delta sink is idempotent for streaming writes. It tracks the batch ID, so a replayed batch is not written twice.
The result is effectively exactly-once ingestion of each file, with no custom bookkeeping from you.
Using a checkpoint
The example below is the same minimal stream from the plan. Notice that every stream gets its own checkpoint path.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.load("/Volumes/main/raw/landing/orders"))
(df.writeStream
.option("checkpointLocation", "/Volumes/main/raw/_checkpoints/orders")
.trigger(availableNow=True)
.toTable("main.bronze.orders"))Run this cell twice with no new files. The second run finds nothing new and writes no rows. Drop in a new JSON file and run it again. Only that file is loaded.
Rules that keep it correct
- Use one checkpoint per stream. Two streams sharing a checkpoint corrupt each other's progress.
- Keep the checkpoint with its table. Pair each checkpoint with one target and one source path.
- Do not edit or delete checkpoint files by hand.
- Keep the checkpoint location stable. Moving it makes Auto Loader forget its history.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Every file reloaded after a redeploy | The checkpoint path changed or was deleted | Point back to the original checkpoint path |
| Stream fails with a concurrency or state error | Two streams share one checkpoint | Give each stream its own checkpoint folder |
| Edited file is not reloaded | The path is already recorded in the checkpoint | Write changed data as a new file instead |
| Rows missing after changing the source path | New source with an old checkpoint | Use a new checkpoint for a new source |
Checkpoints and triggers
The checkpoint is also what makes availableNow jobs work as a schedule. Each run reads the checkpoint, finds files added since the last run, processes them, and stops. The next scheduled run continues from the saved position. The next chapter covers triggers in more detail.
Exercise
A teammate changes the checkpointLocation of a working Auto Loader stream to a new folder "to tidy things up" and restarts it. The landing folder still holds 10,000 old files. What happens to the target table, and how should the teammate fix it?
Show answer
The new checkpoint is empty, so Auto Loader treats all 10,000 files as new and appends them again, which creates duplicate rows. To fix it, restore the original checkpoint path. If the duplicates were already written, remove them from the table, for example by restoring a previous Delta table version. In future, move or rename the checkpoint folder only together with a plan for the stream's history.
Exercise
Why must two different Auto Loader streams not share one checkpoint folder?
Show answer
The checkpoint holds the progress and the list of discovered files for a single stream. Sharing it mixes the state of two streams, so they can skip files or fail with state errors. Each stream needs its own checkpoint.