Request a demo

Glossary · Pipelines and tooling

What is idempotent data pipeline?

An idempotent data pipeline produces the same result whether a run executes once or several times for the same input. Retries, reruns and backfills then can't duplicate or corrupt data, which is what makes a pipeline safe to automate.

Also called: idempotency, idempotent pipeline, rerunnable pipeline, idempotent ETL

What breaks idempotency

  • Appending: INSERT INTO ... SELECT for "yesterday" adds the same rows again on a rerun.
  • Wall-clock time: CURRENT_DATE() or CURRENT_TIMESTAMP() in filters means a rerun next week processes a different period.
  • Non-deterministic values: GENERATE_UUID() or RAND() as keys give new values on each run.
  • Partial writes: a step that writes two tables and fails between them leaves one updated and one not.
  • Retried API calls: a job submission that timed out but actually started runs twice if the client retries blindly.

Idempotent patterns on BigQuery

Replace the period. Delete and rebuild the period inside a multi-statement transaction, or overwrite partitions (dbt insert_overwrite, a WRITE_TRUNCATE job to a partition decorator). The backfill entry shows a full example.

Upsert on a key. MERGE makes the target match the source for each key:

MERGE `my-project.marts.customer_ltv` AS t
USING (
  SELECT customer_id, SUM(amount) AS lifetime_revenue, MAX(created_at) AS last_order_at
  FROM `my-project.sales.orders`
  GROUP BY customer_id
) AS s
ON t.customer_id = s.customer_id
WHEN MATCHED THEN
  UPDATE SET lifetime_revenue = s.lifetime_revenue, last_order_at = s.last_order_at
WHEN NOT MATCHED THEN
  INSERT (customer_id, lifetime_revenue, last_order_at)
  VALUES (s.customer_id, s.lifetime_revenue, s.last_order_at);

Running it twice leaves the table unchanged after the first run, because every source key already matches. See MERGE.

Rebuild the table. CREATE OR REPLACE TABLE ... AS SELECT is idempotent by construction, and fine for small tables; for large ones it rescans everything each run.

Use the logical date. Pass the run's date from the orchestrator ({{ ds }} in Airflow, variables in dbt and Dataform) instead of reading the clock.

Make job submission safe. When you insert BigQuery jobs through the API, generate the job ID yourself and reuse it on retry: a job ID can only be used once in a project, so a retried submission can't start a second copy.

Deduplicate at ingestion. Streaming sources deliver at least once; keep a stable event ID and deduplicate in the first model, for example with QUALIFY ROW_NUMBER() OVER (PARTITION BY event_id ORDER BY ingested_at DESC) = 1.

Cost notes

Idempotent designs can be cheap or expensive. Replacing one partition per run scans one day of sources; CREATE OR REPLACE of a multi-terabyte table every hour rescans all of it, and a MERGE without a partition filter on the target scans the whole target each run. Aim for idempotent and incremental together: overwrite or merge only the partitions the run is responsible for.

FAQ

Why do pipelines need to be idempotent?

Because they will be rerun: orchestrators retry failed tasks, engineers rerun days after fixing bugs, and backfills replay history. A pipeline that appends on every run turns each of those into duplicate rows.

Is MERGE always idempotent?

MERGE on a correct key with deterministic source data is. It stops being idempotent if the source query depends on the current time, uses random values, or the key isn't unique in the source, in which case BigQuery fails the MERGE when one target row matches several source rows.

Read next

Browse the full glossary

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.