1. The problem: partial failures and batch replays
In survey dispatch pipelines (such as Qualtrics integrations for NPS, CSAT, and relationship-manager evaluations), batch re-runs are inevitable. A network drop mid-batch, 429/503 responses, or a manual re-run after an operational fix pushes the same audience through the worker again.
When the target API does not offer native idempotency on contact creation or dispatch triggers, blindly re-sending payloads creates duplicate records or repeat survey invitations. In regulated operations with strict contact cadence rules, that hurts both customer trust and downstream analytics.
2. The unique key comes from the business: client + survey run
A common mistake is generating a random UUID at request time: every retry produces a new identifier and offers zero protection on replay. Another is inventing an opaque technical token disconnected from domain rules, making audits painful when stakeholders ask why a customer was or was not contacted.
In my pipeline design, the unique key combines two explicit business pieces: the client unique identifier for that operation + the survey run identifier (for example, MBR-1042::RUN-2026Q4-NPS). What counts as unique follows the client business rule: a quarterly NPS cycle uses the cycle run ID, whereas a transactional service survey ties the run to the interaction or ticket.
3. Local dispatch log before calling the API
Because the guarantee cannot come from the external API, the pipeline maintains its own control table (qualtrics_dispatch_log) with the unique key (client + run) as its primary key. Before issuing any HTTP call, the worker reserves that key in the local log.
If the key already exists in the log, the contact is skipped with zero external API calls. The same key is sent in the extRef field to preserve end-to-end traceability when responses return.
-- Pipeline-owned dispatch log: unique key (client + run) is the PK
CREATE TABLE IF NOT EXISTS integration.qualtrics_dispatch_log (
dispatch_key TEXT PRIMARY KEY,
client_business_id TEXT NOT NULL,
survey_run_id TEXT NOT NULL,
survey_type TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'pending',
dispatched_at TIMESTAMP
);# Synthetic example: client + run key generation and local log check
from dataclasses import dataclass
from datetime import datetime, timezone
import sqlite3
import time
@dataclass(frozen=True)
class DispatchTarget:
client_business_id: str # Client identifier defined by business rules
survey_run_id: str # Specific survey run / cycle identifier
survey_type: str
channel: str
def build_dispatch_key(client_business_id: str, survey_run_id: str) -> str:
"""Deterministic unique key combining client + survey run."""
client = client_business_id.strip().upper()
run = survey_run_id.strip().upper()
if not client or not run:
raise ValueError("client_business_id and survey_run_id are required")
return f"{client}::{run}"
def try_reserve_dispatch(conn: sqlite3.Connection, target: DispatchTarget) -> tuple[str, bool]:
"""Check and reserve the key in the local log before calling the external API."""
key = build_dispatch_key(target.client_business_id, target.survey_run_id)
cur = conn.execute(
"""
INSERT INTO qualtrics_dispatch_log
(dispatch_key, client_business_id, survey_run_id, survey_type, status)
VALUES (?, ?, ?, ?, 'pending')
ON CONFLICT(dispatch_key) DO NOTHING
""",
(key, target.client_business_id, target.survey_run_id, target.survey_type),
)
conn.commit()
# rowcount == 0 means the key is already logged: skip dispatch
return key, cur.rowcount == 1
def dispatch_contact(conn: sqlite3.Connection, api_client, target: DispatchTarget) -> str:
dispatch_key, reserved = try_reserve_dispatch(conn, target)
if not reserved:
return "skipped_already_logged"
payload = {
"extRef": dispatch_key,
"embeddedData": {
"client_business_id": target.client_business_id,
"survey_run_id": target.survey_run_id,
"survey_type": target.survey_type,
"channel": target.channel,
},
}
for attempt in range(3):
status = api_client.post_contact(payload)
if status == 200:
conn.execute(
"""
UPDATE qualtrics_dispatch_log
SET status = 'sent', dispatched_at = ?
WHERE dispatch_key = ?
""",
(datetime.now(timezone.utc).isoformat(), dispatch_key),
)
conn.commit()
return "dispatched"
if status in (429, 503):
time.sleep(2 ** attempt)
continue
raise RuntimeError(f"Non-retryable HTTP {status} for {dispatch_key}")
raise RuntimeError(f"Retries exhausted for {dispatch_key}")4. Closing the loop on response reconciliation
Carrying the unique client + run key inside extRef solves a second classic integration pain point: matching incoming webhook or export responses back to the exact dispatch run without fragile email or timestamp heuristics.
In the analytics table, reconciliation joins directly on dispatch_key from the log table and pairs it with ON CONFLICT (response_id) DO NOTHING so response ingestion is equally safe to replay.
-- On response return, the same key closes the loop without duplicates
INSERT INTO dw_analytics.survey_response_fact (
response_id,
dispatch_key,
survey_run_id,
score_value
)
SELECT
stg.response_id,
log.dispatch_key,
log.survey_run_id,
stg.score_value
FROM integration.qualtrics_response_staging AS stg
INNER JOIN integration.qualtrics_dispatch_log AS log
ON log.dispatch_key = stg.ext_ref
ON CONFLICT (response_id) DO NOTHING;