Making Laravel Queue Jobs Retry-Safe with Unique Locks and Idempotency Keys
Queue jobs in Laravel can run more than once, through duplicate dispatches, concurrent workers, retries after partial success, or dispatch before a transaction commits. The article uses an invoice-sending job to Strike at a third-party e-invoicing provider as its example, since a repeated run there would send duplicate invoices. It recommends dispatching only after the database transaction commits, using the afterCommit option or the after_commit queue setting so rolled-back data never triggers a job. It then covers the ShouldBeUnique interface, which uses a cache lock keyed by uniqueId() to drop duplicate dispatches. The piece goes on to describe an idempotency key that keeps retries safe even after a crash, since no single tool covers all four failure cases.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in