All insights
Data EngineeringSep 4, 2026 · 3 min read

What happens when your pipeline runs twice

A network timeout caused a retry, and monthly revenue doubled in your warehouse. Here is how to build idempotent pipelines that never duplicate records.

By Ikonnect Service

Two parallel entry funnels on a pale coral ground with an orange gate diverting duplicate tokens into a side tray

A payment webhook arrives at 02:14. Your ingestion worker processes the transaction, formats the payload, and begins writing the record to your cloud database. Midway through the write operation, a transient network blip interrupts the connection. The worker times out, treats the execution as failed, and automatically schedules an immediate retry. Five minutes later, the retry succeeds cleanly. When morning arrives, accounting reports that daily gross revenue is overstated by eighteen thousand dollars because every interrupted transaction was inserted twice.

This scenario illustrates the danger of non-idempotent data pipelines. In distributed computing, failures are inevitable. Network packets drop, API servers restart, and database nodes fail over. Automated retries are necessary to keep workflows resilient.

However, designing an architecture where re-running an operation duplicates data or alters existing state ensures that every transient error introduces financial corruption. When an ingestion job runs twice, the outcome in your warehouse should be identical to running it once.

That property is known as idempotency. Building idempotent pipelines is not an advanced optimization; it is the baseline requirement for reliable data engineering.

The failure of blind appends

Most data duplication originates from relying on blind INSERT INTO operations. When an ingestion task extracts a batch of records from an external API, naive code appends the records directly to the destination table:

sql
-- The non-idempotent pattern: Blind Appends
INSERT INTO analytics.orders (order_id, customer_id, amount, status, created_at)
VALUES ('ord_44921', 'cust_1102', 140.00, 'completed', '2026-09-04 02:14:00');

Executing this statement once records one order for one hundred and forty dollars in your warehouse. If an orchestrator retries the task due to a downstream notification timeout, the statement executes a second time. The table now contains two identical rows for ord_44921.

Downstream queries that calculate total revenue using SUM(amount) will now count that transaction twice. Customer lifetime value calculations distort, inventory counts desynchronize, and executive trust in reporting evaporates.

Cleaning up duplicate rows after they enter production tables requires writing complex deduplication queries or restoring backups. Preventing duplicates at ingestion time is far simpler and structurally sound.

The three-step order of operations for idempotency

Achieving idempotency requires enforcing a strict sequence during every write operation. You must verify identity before committing data.

[Incoming Payload Batch]
          │
          ▼
[Step 1: Write to Ephemeral Staging Table]
          │ (Verify unique natural keys & schema)
          ▼
[Step 2: Execute Atomic MERGE / UPSERT]
          │ (Match on Natural Key: update if exists, insert if new)
          ▼
[Step 3: Truncate / Drop Staging Partition]
          │ (Zero lingering intermediate state)
          ▼
[Production Warehouse Table (100% Deduplicated)]

1. Identify the natural primary key

Every business event possesses a unique identifier. In transactional systems, this is the invoice number, order ID, or payment transaction hash. In event-streaming systems, it is a deterministic composite key (such as user_id plus event_name plus timestamp).

Never rely on autoincrementing warehouse surrogate keys during raw ingestion. If an upstream record lacks a native ID, generate a deterministic hash from the immutable fields of the payload (such as MD5(CONCAT(account_id, transaction_time, amount))).

2. Stage before merging

Never stream API responses directly into core production tables. Land incoming batches into an isolated, ephemeral staging table. Staging tables provide a sandbox where you can validate data types, inspect record counts, and remove intra-batch duplicates before modifying production storage.

3. Execute an atomic MERGE

Instead of executing a simple insert, update existing rows and insert new ones in a single atomic database operation. In SQL, this is handled via the MERGE statement:

sql
MERGE INTO analytics.orders AS target
USING staging.orders_batch AS source
ON target.order_id = source.order_id
WHEN MATCHED THEN
  UPDATE SET 
    target.amount = source.amount,
    target.status = source.status,
    target.updated_at = source.updated_at
WHEN NOT MATCHED THEN
  INSERT (order_id, customer_id, amount, status, created_at)
  VALUES (source.order_id, source.customer_id, source.amount, source.status, source.created_at);

When ord_44921 already exists in analytics.orders, the database updates its current status and timestamp without creating a duplicate record. If the job runs five times in a row, the destination table contains exactly one clean row.

Partition replacement for batch workflows

For high-volume daily batch jobs where executing millions of individual row comparisons during a MERGE is computationally expensive, partition replacement provides an efficient alternative.

For ingestion jobs processing data partitioned by day, configure the write task to execute an atomic partition overwrite:

sql
-- Overwrite the entire daily partition atomically
INSERT OVERWRITE TABLE analytics.daily_traffic_summary
PARTITION (report_date = '2026-09-04')
SELECT 
    channel, 
    sessions, 
    conversions
FROM staging.daily_traffic_batch;

Running this task causes the database to replace the entire partition for that specific calendar date. If the job fails halfway through, the transaction rolls back. If the job runs three times due to scheduler retries, the final state of that daily partition remains identical to a single flawless execution.

Idempotency transforms failure recovery from an emergency into routine operations. When you know a pipeline can run ten times without corrupting production numbers, debugging network timeouts stops being stressful.

When your analytics team frequently uncovers data corruption caused by manual export errors, read our breakdown on how Excel silently alters CSV data. To design pipelines that handle failure gracefully and preserve data integrity, learn more about our specialized data engineering services.

Newsletter

Signal, not noise.

One email a month on data, AI and growth: the tactics we're actually using for clients, no fluff. Unsubscribe anytime.

By subscribing you agree to our Privacy Policy.

Have a project in mind?

Let's build the system
your growth runs on.