Schedule icon
Script icon
Return icon
Loop icon
Sequential icon
If icon
Log icon
Subflow icon
Set icon
SlackIncomingWebhook icon
Fail icon

Safe Backfill Controller with Checkpoints and Resume

Backfill any date-partitioned job with Kestra by day, week or month, with a concurrency limit, KV checkpoints and automatic resume after failures.

Categories
CoreData

Backfilling a year of data usually means a fragile script looping over dates. When it dies at month seven, someone has to work out what already ran, whether re-running is safe, and how to avoid hammering the source. This blueprint turns any date-partitioned job into a safe, resumable backfill. It splits a date range into partitions, runs your job flow once per partition with a concurrency limit, checkpoints every partition that finishes, and when a run fails, the next run picks up exactly where it stopped.

Kestra's built-in backfill replays a flow's Schedule trigger over past dates. This controller is for everything else: jobs that take a date range as input, backfills you want to run in parallel with a cap, and long backfills that need to survive failures and resume without redoing finished work.

How it works

  1. plan (io.kestra.plugin.scripts.python.Script) splits start_date to end_date into DAY, WEEK or MONTH partitions. Weeks start on Monday and months on the 1st, so partitions line up with how data is usually stored. Each partition has a start and an exclusive end.
  2. checkpoints_before (io.kestra.plugin.core.debug.Return) snapshots each partition's checkpoint from the KV store, so the summary can tell what was already done.
  3. run_partitions (io.kestra.plugin.core.flow.Loop) processes two partitions at a time, with transmitFailed: false so one failure doesn't stop the rest. For each partition:
    • skip_or_run (io.kestra.plugin.core.flow.If) skips it if its checkpoint is SUCCESS, unless force is set.
    • run_job (io.kestra.plugin.core.flow.Subflow) runs your job flow with partition_start and partition_end, waits for it, and labels the child execution with the backfill ID and partition.
    • mark_success (io.kestra.plugin.core.kv.Set) checkpoints the partition. If anything fails, mark_failed in the errors branch records FAILED instead. Checkpoints expire after 90 days.
  4. summarize compares the checkpoints before and after, and notify posts the result to Slack: done in this run, already done, and failed.
  5. fail_if_incomplete (io.kestra.plugin.core.execution.Fail) fails the run while any partition is missing, naming the failed partitions and how to resume.
  6. If anything else fails, such as an invalid date range, alert_failure in the flow's errors branch posts the failed task, the error and a link to Slack. An incomplete backfill isn't alerted twice, because notify has already reported it. The Slack calls retry 3 times.
  7. The flow has a concurrency limit of 1, so two runs of the controller never process the same backfill at once. A second run waits in the queue.
  8. The nightly_resume trigger (io.kestra.plugin.core.trigger.Schedule, shipped disabled) re-runs the backfill configured in the input defaults every night. Finished partitions are skipped, so it only retries what's missing, for example data published late.

This blueprint ships with an example job, backfill-example-nyc-taxi-monthly, which computes monthly NYC taxi trip metrics with DuckDB from the public TLC Parquet files. Run it once with the defaults to see a real backfill with no account or setup.

What you get

  • Resumable backfills. Re-run with the same backfill_id, and only failed or missing partitions run again.
  • Bounded load on the source system through the loop's concurrency limit.
  • A KV checkpoint per partition, with the job execution that produced it in the description, for auditing.
  • Child executions labeled backfill_id and partition, so you can filter them in the UI.
  • A Slack summary and a clearly failed run while a backfill is incomplete, plus a Slack alert naming the failed task when the controller itself fails.
  • The same controller for any job that takes a date range: dbt runs, API extracts, warehouse loads or ML feature builds.

Who it's for

  • Data engineers who backfill pipelines after schema changes, bug fixes or onboarding a new source.
  • Analytics teams rebuilding historical aggregates partition by partition.
  • Platform teams who want a standard, auditable way to run backfills instead of ad hoc scripts.

Why orchestrate this with Kestra

A backfill is a long-running, failure-prone loop, which is exactly what scripts handle worst. Kestra runs each partition as its own execution with its own logs, retries and labels. It caps parallelism with concurrencyLimit and records progress in the KV store, which outlives any single run. A backfill can stop and resume across days. Every partition is visible in the UI, and the job flow stays reusable for daily runs.

Prerequisites

  • A job flow that accepts partition_start and partition_end as DATE inputs (start inclusive, end exclusive) and is safe to re-run for the same partition. The example job does this by overwriting one KV entry per month.
  • A Slack incoming webhook for the summary and failure alerts.
  • A Kestra worker with Docker available for the Python steps.

Secrets

  • SLACK_WEBHOOK_URL: Slack incoming webhook for the backfill summary and failure alerts.

Inputs

  • job_namespace (STRING, default company.team): namespace of the job flow.
  • job_flow_id (STRING, default backfill-example-nyc-taxi-monthly): flow run once per partition.
  • start_date (DATE, default 2024-01-01): first date of the range. The first partition starts at its boundary, for example the 1st of the month.
  • end_date (DATE, default 2024-06-30): last date of the range, inclusive. The last partition is the one containing this date.
  • granularity (SELECT DAY, WEEK or MONTH, default MONTH): partition size.
  • backfill_id (STRING, default initial-load): names the checkpoints. Re-use it to resume, or change it to backfill the same range again from scratch.
  • force (BOOL, default false): re-run every partition, ignoring checkpoints.

Outputs

  • {{ outputs.complete }} (flow output): true when every partition has succeeded.
  • {{ outputs.summary }} (flow output): total partitions, done in this run, already done, and the failed partitions.
  • {{ outputs.plan.vars.starts }} and {{ outputs.plan.vars.ends }}: the planned partitions.
  • {{ outputs.run_partitions.terminatedIterations }}: partition outcomes by state for this run.
  • KV keys backfill-<backfill_id>-<job_flow_id>-<partition> with value SUCCESS or FAILED.

Quick start

  1. Add both flows and the SLACK_WEBHOOK_URL secret.
  2. Run safe-backfill-controller with the defaults. It backfills January to June 2024 with the example job in under a minute.
  3. Run it again with the same inputs. Every partition is skipped.
  4. To see a resume, backfill a range that ends in months not yet published by NYC TLC. Those partitions fail, the run fails, and a later run with the same backfill_id retries only them.
  5. Point job_namespace and job_flow_id at your own job flow.

How to extend

  • Raise or lower concurrencyLimit on run_partitions to match what your source can take.
  • Add a retry to run_job for flaky sources, so transient errors are retried before a partition is marked failed.
  • Pass more inputs to your job through run_job.inputs, such as a target table or a dry-run flag.
  • Replace the Slack step with email, Teams or PagerDuty, or add an SLA to alert when a backfill runs too long.
  • Enable nightly_resume with your backfill as the input defaults to let it complete itself as late data arrives.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.