A queue job that runs twice is not a rare edge case. It is the normal behaviour of a queue. A worker times out, the job is released, and Laravel runs it again. If the job sends an email, charges a card, or creates a record, the second run repeats the side effect. The fix is to make the job idempotent: running it twice has the same effect as running it once.
Queues deliver at least once, not exactly once
Laravel queues, Redis queues, and most queue systems guarantee at-least-once delivery. A job is removed from the queue only after the worker reports success. Anything that interrupts the worker between doing the work and reporting success, a timeout, a killed process, a server restart, leaves the job to be retried. You cannot configure this away. The retry_after setting on a Redis queue controls how long a job can run before it is released for another attempt, and $timeout on the job controls how long the worker waits before killing it. Design every job as if it will run twice, because sooner or later it will.
What idempotent means in practice
An idempotent job produces the same result no matter how many times it runs with the same input. Sending the same invoice email twice is not idempotent; creating a payment record with a unique reference, where the second attempt finds the existing record and stops, is. The test is simple: if you run the job three times in a row with the same payload, does anything change after the first run? If yes, the job is not idempotent yet. This applies to every side effect the job has: emails, API calls, database writes, files.
Give every job a unique key
Start by deciding what makes one run of a job unique. Usually it is the thing the job acts on: the invoice id, the order id, the user id plus the action. Build a key from it, for example invoice-email:1042, and use that key for every guard that follows. Generate the key from the payload, not from a random value, so a retry of the same job produces the same key. If the job can legitimately run twice for the same record with different inputs, include the input in the key.
Guard the side effect with a lock and a flag
The pattern has two parts: take a lock so two workers never run the same job at the same time, then check a flag on the record before doing the work and set it right after. Here is a job that sends an invoice email:
class SendInvoiceEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(public int $invoiceId) {}
public function handle(): void
{
$key = "invoice-email:{$this->invoiceId}";
$lock = Cache::lock($key, 300);
if (! $lock->acquire()) {
return; // Another worker is already handling it.
}
try {
$invoice = Invoice::findOrFail($this->invoiceId);
if ($invoice->email_sent_at !== null) {
return; // Already sent by an earlier attempt.
}
Mail::to($invoice->customer_email)->send(new InvoiceMail($invoice));
$invoice->update(['email_sent_at' => now()]);
} finally {
$lock->release();
}
}
}
The lock prevents concurrent duplicates. The email_sent_at flag prevents a retry from sending the email again. The flag must live in the same database as the record it guards, not in the cache, so it survives a cache flush and a worker restart. If the side effect is a charge or a payment, the flag should be the payment reference itself, stored with a unique constraint so the database rejects a second insert even if two processes race.
Close the gap between doing the work and marking it done
The pattern above has one honest weakness: if the worker dies between sending the email and setting the flag, a retry sends the email again. For emails that is annoying; for charges it is a real problem. The stronger pattern claims the work in the database before doing it. Insert a row into a small table with a unique key, for example job_idempotency_keys with columns for the key, a status, and timestamps, then do the work, then mark the row completed. A retry sees the existing row and stops. A crashed claim is a row stuck in processing, so give claims an expiry: a claim older than 60 minutes can be re-claimed by a later attempt. The database, not the worker's memory, is the source of truth about what already happened.
Patterns that do not work
A few approaches look safe and are not. Checking a flag in the cache only fails when the cache is flushed or the key expires. Relying on $tries = 1 fails because a timeout or a worker restart still re-runs the job, and setting tries to one means a genuinely failed job never gets a second chance. Trusting an external API to deduplicate for you only works if that API actually accepts an idempotency key and you actually send one. If the provider supports idempotency keys, pass your job key through. If it does not, guard the call on your side with the claim row above.
The takeaway
Assume every queue job will run twice. Give each job a key derived from its payload, lock on that key while it runs, and record completion in the database where a retry can see it. Use a claim row with a stale re-claim window when the gap between doing the work and marking it done matters. Read why Laravel queue jobs fail silently if you have not set up failed job handling yet, because idempotency and failure visibility go together.