Voice Agent Webhooks: Retries Without Duplicates
One voice command should create one result - even if its webhook arrives twice. Store the event and its job before returning 2xx, use the same event ID across retries, and protect each task or note write with a stable action ID.
The catch? Saving the event alone won’t prevent duplicates if a worker crashes after creating a task. Safe processing also needs:
- Guarded writes: Database constraints or destination idempotency keys that block repeated actions.
- Controlled recovery: Worker leases, retry limits, and dead-letter handling.
- Safe replays: Retained event IDs, action IDs, and destination record IDs.
- Failure tests: Duplicate deliveries, lost responses, and crashes must still leave one record per intended action.
Voice Agent Webhooks: Retry Without Duplicate Tasks
Store Event IDs Before Creating Tasks
Record Event IDs and Processing Status
Create a durable event row before creating any task. The sender must reuse the same event_id on every retry. Use that row as the source of truth each time.
| Field | What to store |
|---|---|
provider |
Name of the service that sent the event. |
source_account_id |
Account ID assigned by that service. |
event_id |
Source event ID, unchanged across retries. |
event_type |
Event category. |
source_object_id |
ID of the original voice note or recording. |
created_at |
Time the source event was created. |
received_at |
Time your receiver first received the event. |
status |
Processing state: pending, processing, completed, or failed. |
processed_at |
Completion time; leave empty until processing succeeds. |
result_reference |
Destination task or note ID, or a reference to related result rows. |
payload_hash |
Optional payload hash for diagnostics. |
A stored row means the event was received - not that processing finished. Check its status before deciding what to do next. Leave received_at unchanged on retries so it records the first arrival. If one event creates several tasks, store every destination ID in related result rows.
The worker should read this row before creating any task or note.
Block Duplicate Records in the Database
Let the database - not request timing - decide whether an event is new.
Use UNIQUE (event_id) only if event IDs are globally unique across every source you accept. Otherwise, use UNIQUE (provider, source_account_id, event_id). Record the event with an atomic insert, not separate checks followed by inserts.
Add a separate unique constraint for the intended action if different event IDs can describe the same action. Scope that constraint by provider, account, source_object_id, action identity, and version.
Give each intended task its own stable action identity, and track versioned updates separately. A constraint on the source object alone would block multiple tasks from the same request.
Use payload_hash only to flag changed content under an existing event ID - never as a deduplication key. Store the event before writing the target task or note, with database constraints that block duplicates from retries and race conditions.
sbb-itb-b6fe06e
Accept Events and Create Tasks Safely
Verify, Store, and Acknowledge Events
Once you store the event ID, the next step is to acknowledge receipt without creating a duplicate task.
A 2xx response means the event was durably accepted - not that processing is complete. Verify the signature against the raw request body using the sender’s documented method, including timestamp checks. Validate the payload, then store the event and its durable job in the same transaction.
receive(request):
if not verify_signature(request.raw_body, request.headers):
return 401
event = parse_and_validate(request.raw_body)
if invalid(event):
return 400
try:
transaction:
inserted = insert_event(
event.provider, event.source_account_id,
event.event_id, event.payload, status="pending"
) ON CONFLICT (provider, source_account_id, event_id) DO NOTHING
if inserted:
INSERT INTO jobs (provider, source_account_id, event_id, status)
VALUES (event.provider, event.source_account_id,
event.event_id, "pending")
else:
existing = load_event(
event.provider, event.source_account_id, event.event_id
)
except retryable_storage_error:
return 503
if inserted:
return 200
if existing.status in ("completed", "processing"):
return 200
return 503
For a duplicate delivery, check the existing row’s status before responding. Return 200 only if it’s completed or processing; otherwise, return 503.
Durable acceptance reserves the event. The worker still owns the one-time write and must guard against duplicates downstream.
Prevent Duplicate Writes During Worker Failures
An atomic worker claim keeps competing workers from taking the same task. Only one worker should be able to move it from pending to processing.
For external APIs, send the event ID as the idempotency key. After success, store the destination record ID. If the destination doesn’t support idempotency and the call times out or the worker crashes, stop and reconcile before retrying.
worker_claim = update_task_status(task_id, from="pending", to="processing")
if not worker_claim:
stop
if destination_supports_idempotency:
destination_id = call_destination(
payload=task_payload,
idempotency_key=event.event_id
)
else:
existing = search_destination_for_matching_record(event)
if existing:
destination_id = existing.id
else:
destination_id = call_destination(task_payload)
transaction:
store_result_reference(event.event_id, destination_id)
mark_event_completed(event.event_id)
mark_job_completed(task_id)
Use a destination action ID where supported. Task text isn’t a safe dedupe key. Without supported idempotency or reliable reconciliation, automatic retries can still create duplicate work.
Walk Through a Hypothetical OneKey Request
The same pattern applies to a task flow that starts with a voice command.
Hypothetical automation - not a documented OneKey webhook schema or retry policy: A founder captures a command in OneKey to review the Q3 budget with Sarah on Monday. Two concurrent deliveries reach the automation: the first commits the event and job; the duplicate hits the unique constraint and receives
503while the event is stillpending, or200once it isprocessingorcompleted. The worker creates one task and persists its destination record ID.
Set Retry Rules and Test Recovery
Set Backoff, Recovery, and Dead-Letter Rules
Accepted events still need retry rules when processing fails. Sender retries protect delivery. Worker retries protect processing. The sender retries after timeouts, lost connections, or retryable HTTP responses - even when the event has already been accepted. Workers retry processing failures after acceptance and queuing. Use capped exponential backoff with jitter.
For example, retry after 30 seconds, 1 minute, 2 minutes, 4 minutes, and so on, capped at 1 hour.
Set an attempt limit and a time limit, such as 10 attempts or 24 hours, whichever comes first. Retry network failures, rate limits, timeouts, and temporary downstream outages. Send permanent failures and exhausted retries to a dead-letter queue, along with their failure history.
Use a received → claimed → completed state machine. Return retryable failures to received and move exhausted retries to dead_letter. Recover abandoned claims with expiring leases, and require the active lease token on updates so stale workers can't overwrite newer state. Lease recovery doesn't prevent duplicate writes: every attempt must reuse the same action ID.
Keep Event IDs for Safe Replays
Keep dedupe records for the sender retry window and any supported manual-replay window. Store the original event ID, action ID, processing outcome, destination record ID, and a retrievable payload reference. Keep the same event ID and action ID across every replay, retry, and dead-letter recovery. If the destination's idempotency key has expired and you don't know whether the earlier write succeeded, check the destination or a stored mapping before retrying. Send unresolved cases for manual review.
Test these failure paths directly.
Test Duplicate Deliveries, Timeouts, and Crashes
For each intended action, assert one destination record, not just a successful HTTP response.
- Verify that invalid signatures never enter the queue.
- Check duplicate deliveries that arrive after the first delivery has been acknowledged.
- Force a timeout after creation and a crash before completion is recorded. Confirm that destination idempotency prevents another write.
- Crash between event storage and job publication. Confirm that the durable job or outbox still dispatches the work.
- Expire a lease during execution. Confirm that recovery succeeds and stale workers can't overwrite the new attempt.
- Replay dead-letter events with their original IDs. Test retention boundaries and expired destination idempotency keys.
- Log the event ID, event type, action ID, attempt number, status, and destination record ID. Don't log full transcript text.
Conclusion: Keep One Result per Intended Action
Store events before acknowledgment, check duplicates atomically, and protect destination writes separately. Keep event IDs and failure states long enough to support retries and replays.
How to Handle Webhooks in Spring Boot (Security, Retries and Idempotency)
FAQs
How do I assign action IDs when one command creates multiple tasks?
Use the Onekey-Delivery-Id webhook header to identify the event that creates the tasks. This ID stays the same across every retry of that event.
Before processing a delivery, check your database for the ID. If it’s already recorded, skip the delivery so repeated deliveries of the same event don’t create duplicate tasks.
What if a webhook retry contains a changed payload?
Webhook retries for the same event keep the exact same body and Onekey-Delivery-Id. The payload stays the same across retry attempts.
Use Onekey-Delivery-Id to check for duplicates. If you’ve already processed that delivery ID, you can safely ignore the retry to avoid creating duplicate tasks, notes, or actions.
How can I reconcile a task after an API timeout?
Before processing a webhook, check your local records for the Onekey-Delivery-Id from its headers. This ID stays the same across retries of a single event, so you can spot duplicate requests and avoid creating the same task twice.
For your own API calls, include an Idempotency-Key request header to prevent duplicate billing during retries.
