How to reset a streaming checkpoint without reprocessing historical data?

angelinak
New Contributor

What is the safest way to restart a Structured Streaming pipeline with a new checkpoint?

Suppose the pipeline has already processed six months of data, but a change in the streaming logic makes the existing checkpoint incompatible.

How can we avoid processing all the historical data again? Is there a recommended recovery approach that also prevents duplicate records in the target Delta table?

SumeshKashyap
Honored Contributor

Hi @angelinak 

A new checkpoint knows nothing about the old one, so tell the source where to start explicitly. Take the last committed position from the old checkpoint and pass it as a starting option: startingVersion for a Delta source, startingOffsets for Kafka, modifiedAfter for Auto Loader. Make the sink idempotent too, so the boundary batch can't create duplicates.

Delta table source:

1. Stop the stream.

2. In the old checkpoint, find the highest batch ID under commits/ and open offsets/<that id>. The last line contains "reservoirVersion": N.

3. Start with a new checkpoint from that version:

(spark.readStream

   .option("startingVersion", N)

   .table("main.silver.events")

   ...

   .writeStream

   .option("checkpointLocation", "/Volumes/.../_checkpoints/events_v2")

   .toTable("main.gold.events"))

startingVersion and startingTimestamp are only read when a checkpoint is first created, so they're safe to leave in the code afterwards.

Kafka source: copy the per-partition offsets from that same offsets file into startingOffsets as JSON, for example {"topic":{"0":12345,"1":67890}}.

Auto Loader: use .option("modifiedAfter", "<timestamp of the last processed file>"). Don't rely on includeExistingFiles=false, because files that arrived while the stream was stopped would be skipped.

Preventing duplicates in the target: starting at version N can replay part of the last batch, so write with MERGE on the business key inside foreachBatch (or use txnAppId/txnVersion for idempotent appends) instead of a plain append. That way any overlap is a no-op.

Docs: https://docs.databricks.com/aws/en/structured-streaming/delta-lake#specify-initial-position

View solution in original post

arthurfr23
Contributor III

A new checkpoint has no memory of past progress. To avoid a full replay, you set the starting position on the source yourself. Duplicates at the cutover are best handled with an idempotent write (MERGE on a business key), not by trying to land on the exact offset.

If the source is a Delta table, these are the steps I'd follow:

  1. Stop the old query cleanly and let the last micro-batch commit.
  2. Find the cutover version. Run DESCRIBE HISTORY on the source and pick the version right after the last one the old stream processed. If you aren't sure, pick a slightly earlier version. Some overlap is fine, but a gap means lost data.
  3. Start the new query with a new checkpoint location and startingVersion (or startingTimestamp):
(spark.readStream
   .option("startingVersion", "12345")   # only honored on a NEW checkpoint
   .table("source_table")
   # ... new logic ...
   .writeStream
   .option("checkpointLocation", "/chk/my_stream_v2")
   .foreachBatch(upsert_to_target)        # MERGE on business key
   .start())
  1. Make the sink idempotent. Inside foreachBatch, use MERGE INTO target USING batch ON t.key = s.key. Then the overlapping records update existing rows instead of being appended again. If you use txnAppId/txnVersion for idempotent writes, you need a new txnAppId. A new checkpoint starts again at batch ID 0, so Delta would skip writes that reuse the old app ID.

Caveats from the docs: these options only apply when a query starts with a new checkpoint and are ignored afterwards. There also must be no incompatible schema change to the source after the version you pick.

Docs: - https://docs.databricks.com/aws/en/structured-streaming/delta-lake (startingVersion, idempotent writes) - https://docs.databricks.com/aws/en/structured-streaming/checkpoints (which changes need a new checkpoint)

Arthur Ferreira Reis

View solution in original post

tajamulkhan
Esteemed Contributor
The safest approach is to restart from the last successfully processed source offset while preserving the existing target Delta table.

Deleting a checkpoint removes both processing progress and streaming state, so I would avoid doing this without a recovery plan.

1. Identify the last successfully processed offset

The recovery strategy depends on the streaming source:

  • Delta Lake: Use startingVersion.

  • Kafka: Use startingOffsets.

  • Auto Loader: Consider modifiedAfter with a verified file modification timestamp and reconciliation.

2. Restart using a new checkpoint

For example, if the target contains all records through source Delta version 150, restart from version 151.

df = (
    spark.readStream
    .format("delta")
    .option("startingVersion", 151)
    .table("bronze.orders")
)
 

The starting version must still be available in the source table history. Also, startingVersion is inclusive.

3. Prevent duplicate records using idempotent MERGE

from delta.tables import DeltaTable

def upsert(batch_df, batch_id):
    target = DeltaTable.forName(
        spark, "silver.orders"
    )

    (target.alias("t")
        .merge(
            batch_df.alias("s"),
            "t.order_id = s.order_id"
        )
        .whenNotMatchedInsertAll()
        .execute()
    )

query = (
    df.writeStream
    .foreachBatch(upsert)
    .option("checkpointLocation", new_checkpoint_path)
    .start()
)
 

This example assumes immutable orders with unique, non-null order IDs. For mutable records, implement conditional updates and deterministic source deduplication.

4. Consider stateful transformations

If the pipeline uses window aggregations, watermarks or stream-stream joins, resetting the checkpoint also removes accumulated state. Historical data may need to be replayed to reconstruct that state correctly.

My recommendation is to preserve the old checkpoint, verify the last committed source position, create a new checkpoint and use idempotent writes. When the recovery position is uncertain, controlled overlapping replay is generally safer than risking data loss.

Could you confirm whether your source is Delta Lake, Kafka or Auto Loader? Also, does the pipeline contain any stateful transformations?

Official references:

View solution in original post

balajij8
Esteemed Contributor III

When the streaming logic changes in a way that breaks the checkpoint such as changing sources, sinks or stateful operations, there is no way to repair the old checkpoint. Streaming treats a new checkpoint directory as a brand new stream that starts completely fresh. Stop the stream, note down where it got to (the last Kafka offset or a cutoff timestamp), start the new query with a new checkpoint location, and explicitly tell it where to begin with Kafka that's startingOffsets or startingTimestamp so it picks up from the last processed point instead of replaying all six months. Note that trigger changes and rate limits don't need a new checkpoint and on DBR 18.2+ source evolution lets you add or reorder sources without losing your checkpoint state at all.

For duplicates, the target Delta table is the best safety point. If the new stream overlaps data the old one already wrote, write each micro batch with foreachBatch using an insert only MERGE (WHEN NOT MATCHED THEN INSERT) on the business key - the table itself then rejects anything already present, which is the right guard when streams overlap. Note if you are using txnAppId/txnVersion for idempotency, you must change the txnAppId when you reset the checkpoint - a fresh checkpoint restarts batch IDs at 0 so Delta would mistake the new batches for replays and silently skip them.