Sub topic 7
Checkpoints and restarting a query
This chapter assumes you can write a basic streaming query with readStream and writeStream, and that you know Delta tables and Unity Catalog volumes. It covers what a checkpoint stores, how a query resumes after a stop, and which changes break a restart.
What a checkpoint is
A checkpoint is a directory where a streaming query records its progress. Spark writes to it after every micro-batch. It holds four kinds of information:
- Offsets: the range of source data each batch will read, written before the batch runs.
- Commits: a marker written after a batch finishes and its output is saved.
- State: the aggregation or deduplication data for stateful queries.
- Metadata: the query's unique ID.
You set the location with the checkpointLocation option:
query = (
spark.readStream.table("main.demo.orders_bronze")
.writeStream
.option("checkpointLocation", "/Volumes/main/demo/chk/orders_copy")
.trigger(availableNow=True)
.toTable("main.demo.orders_copy")
)
query.awaitTermination()
for f in dbutils.fs.ls("/Volumes/main/demo/chk/orders_copy"):
print(f.name)The listing typically shows offsets/, commits/, sources/ and metadata. Stateful queries also have a state/ directory.
How a restart works
When a query starts, Spark reads the checkpoint and compares the offsets log with the commits log.
Re-running an unfinished batch with the same offsets keeps the input stable. The output is exactly-once only when the sink handles the replay safely. Delta sinks do. The next chapter covers custom sinks.
Rules for checkpoints
- Use one checkpoint per query. Two queries that share a directory corrupt each other's progress.
- Keep it on durable storage, such as a Unity Catalog volume or cloud storage. Do not use a cluster's local disk.
- Do not delete it casually. With no checkpoint, the query starts over. A file source then re-reads every file, and a Kafka source follows
startingOffsets. - Treat it as tied to the query's logic. You can move the query to a new cluster or notebook and keep the same checkpoint.
Changes that are safe and unsafe
| Change | Result |
|---|---|
| Change the trigger interval | Safe |
| Add a stateless filter or column | Usually safe |
| Change the sink path or table | Allowed, but the sink must match the data already written |
| Change grouping keys or aggregations in a stateful query | Fails |
| Change `spark.sql.shuffle.partitions` for a stateful query | Ignored. The partition count is fixed when the state is first created. |
| Switch to a different source | Fails or gives wrong results |
When a change is not compatible, start the query with a new checkpoint path. Decide in advance how to handle the data you have already processed.
Common errors
- Sharing a checkpoint between two queries. Give each query its own path, for example one directory per target table.
- Reusing an old checkpoint after changing a stateful query. You see errors about state schema or missing state. Use a new checkpoint.
- Forgetting the option. Without it, Databricks may use a temporary location. The query then cannot resume after a restart.
Exercise
A streaming aggregation job fails after you change its groupBy columns, and you restart it with the same checkpoint. Why does the restart fail, and what should you do?
Show answer
The checkpoint's state directory holds data keyed by the old grouping columns, and the new query cannot read it. Start the query with a new checkpoint path, and plan how to handle data you have already aggregated, for example by backfilling it.