Skip to main content

Cloudsoft Zone Build, deploy and scale with practical guides to PHP, Node.js, React, DevOps and AI automation. Explore the guides

Cloudsoft Zone
DevOpsDevelopmentTutorial

Prevent duplicate queue jobs: durable admission and payment webhook recovery

Prevent duplicate queue jobs with durable operation identity, atomic admission and verified recovery rather than relying only on queue IDs, locks or a successful webhook response.

Nilesh Kushvaha

5 min read

Sourced Laravel artwork with a durable admission and recovery workflow for queue jobs and payment webhooks.
Image: Cloudsoft Zone layout; Laravel / Public domain
In this article

A webhook arrives twice and a job is retried after a network timeout. The queue accepts only one current job ID, but the payment provider already completed an earlier attempt whose response was lost.

To prevent duplicate queue jobs from producing duplicate business effects, define durable operation identity and recovery behavior. This guide focuses on the admission-to-processing protocol that connects verified webhooks, database records and a queue.

Our Laravel payment and webhook guide covers provider-specific handling. Here the emphasis is the operating gap between accepting an event and completing its side effect.

Verified delivery, provider event identity and business operation identity form separate deduplication boundaries.
An event-ID unique constraint alone does not define every business-state transition.

Step 1: Separate the three identities to prevent duplicate queue jobs

A delivery attempt, provider event and business operation are not always the same identity. Several delivery attempts can carry one event, and distinct related events can describe the same payment or order transition.

Identity Intended purpose
Delivery attempt Observe transport retries and receipt
Provider event Recognize a repeated verified event
Business operation Enforce one intended side effect or transition

Scope identities to the relevant provider account, environment and tenant or business resource. Do not let an arbitrary caller choose a key that suppresses another customer's legitimate operation.

Validate webhook authenticity before treating a claimed event ID as trustworthy. Preserve the raw-body requirements of the provider's signature verifier and keep secrets in protected server configuration.

Step 2: Make admission atomic and durable

Use the existing database transaction owner to record a verified event with a reviewed unique constraint. A read-then-insert application check alone can race when two deliveries arrive concurrently.

Store a bounded payload or protected reference according to retention needs, not a public log of payment details. Record admission and processing status separately so a received event is not mistaken for completed work.

If the same identity arrives with an incompatible canonical request, reject or flag the conflict according to the operation contract. Do not silently reuse a result for different instructions.

Keep the inbound handler fast, but define success acknowledgment against durable retention. If a broker is unavailable and no database record retains the work, returning success can lose the event.

Step 3: Close the dispatch gap with an outbox

Database commit and broker enqueue are separate actions. A process can commit admission and crash before dispatching; it can also enqueue before a transaction commits if the integration is wrong.

An outbox design records an owned dispatch intent in the same database transaction as admission. A publisher retries delivery using stable operation identity, and the consumer tolerates repeated messages.

Text
verify inbound authenticity
begin database transaction
  insert or locate durable event admission
  record eligible dispatch intent once
commit database transaction
acknowledge durable receipt

outbox publisher retries dispatch
consumer checks durable business state
consumer applies and records the verified outcome

This is a protocol outline, not executable payment code. The application must implement locking, unique constraints, authorization and failure handling through its existing owners.

Laravel after-commit dispatch avoids a worker seeing uncommitted records. It does not make the database and broker one atomic system or remove every post-commit crash window.

A durable outbox connects committed admission to retried dispatch and idempotent consumption.
After-commit dispatch prevents early reads but does not eliminate every delivery gap.

Step 4: Treat queue-level deduplication as one layer

Laravel unique jobs and overlap controls serve different purposes: dispatch uniqueness and concurrent execution are not identical guarantees. Verify the lock store and lifecycle used by the deployed application.

BullMQ job IDs prevent adding another job with the same retained ID in that queue. When a job record is removed, that retained deduplication boundary changes. It is not a permanent payment ledger.

Keep business idempotency in durable state with an explicit retention policy. Queue records and short-lived locks can support execution efficiency without becoming the only evidence that an external action already happened.

Step 5: Coordinate the business transition

Use an atomic state transition, unique business-operation constraint or appropriate transactional locking. Keep transactions bounded and avoid waiting on a slow external provider while holding unrelated rows locked.

For external requests, use the provider's supported idempotency mechanism with a stable operation key. Understand its retention and parameter-conflict behavior rather than assuming keys remain effective forever.

Our idempotent AI job guide applies the same principle to costly external calls: a retry is safe only within a defined operation contract.

Step 6: Reconcile ambiguous and unordered outcomes

Webhook order is not a reliable business-state sequence. Apply eligible transitions according to current authoritative state and retrieve provider state where the integration requires it.

A timeout can happen after an external action succeeds. Preserve the operation reference and inspect provider state before issuing another action with a different identity.

Failure rehearsal Required behavior
Same event delivered concurrently One durable admission, no duplicate effect
Crash after commit before dispatch Outbox eventually delivers the retained intent
Repeated consumer execution Final business state remains correct
External timeout after acceptance Reconciliation avoids a second effect
Events arrive out of order Valid state transitions remain consistent

A stable external operation key and provider-state lookup precede a verified final business transition.
Do not invent a new payment key simply because the response timed out.

Step 7: Verify the complete protocol

Use provider test environments and isolated application fixtures. Record the actual event, dispatch and business outcome counts without exposing real payment data.

Monitor retained admissions awaiting dispatch, operations stuck in an intermediate state and reconciliation failures. A low queue count can conceal work that never reached the broker.

For queue reliability and safe webhook processing fitted to your application, discuss the project with Cloudsoft Zone.

Featured image: Cloudsoft Zone technical layout with laravel artwork by Laravel (Public domain). Product mark resized proportionally for identification; no endorsement implied. Layout: CC BY-SA 4.0.

Frequently asked questions

  • Does a unique queue job ID guarantee exactly-once business effects?

    No. Queue retention can remove the deduplication record, and retries can occur after an external effect. Enforce durable business-state idempotency separately.

  • Does after-commit dispatch make the database and broker one transaction?

    No. It avoids exposing uncommitted data to the worker, but the database commit and broker delivery can still fail independently. A durable outbox addresses that gap.

  • Should a webhook handler acknowledge an event before retaining it?

    Define acknowledgment against durable admission. If nothing durable retains the event when dispatch fails, returning success can cause lost work. Keep the handler fast and bounded without silently discarding events.

Sources

  1. Stripe duplicate and unordered webhook delivery — Stripe
  2. Stripe idempotent request behavior — Stripe
  3. BullMQ job-ID retention boundary — BullMQ
  4. BullMQ idempotent job design — BullMQ
  5. Laravel unique jobs and transaction dispatch — Laravel
  6. Laravel transactional row locking — Laravel

New tools, once a week

One short email listing what joined the directory.

The weekly email about new tools and occasional newsletters. Unsubscribe at any time.

Comments

No comments yet. Be the first to join the conversation.

Leave a comment

Every comment is read by a moderator before it appears. Your email address is never published; it is used only to contact you about this comment.

Never published. Used only to contact you about this comment.

Loading the security check…

Comments are moderated before publication.