Queue workers may retry work after a timeout or lost acknowledgement, even when part of the job already ran. Make repeating the same operation safe by recording a stable business or provider event ID under a unique database constraint, then applying local changes in the same transaction.
DB::transaction(function () use ($event) {
$receipt = WebhookReceipt::firstOrCreate(
['provider_event_id' => $event['id']],
['payload' => $event],
);
if (! $receipt->wasRecentlyCreated) {
return;
}
// Apply the local state change in this transaction.
Subscription::whereKey($event['subscription_id'])
->update(['status' => $event['status']]);
});
For effects outside your database, such as charging a payment provider, send the same idempotency key on every retry. A unique queue job can prevent duplicate dispatch, but it does not replace idempotent job logic.