<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: osama</title>
    <description>The latest articles on DEV Community by osama (@osamafawzy).</description>
    <link>https://dev.to/osamafawzy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4200225%2F3dfff3fe-5faf-4f85-b1d9-e4b8c058caae.jpg</url>
      <title>DEV Community: osama</title>
      <link>https://dev.to/osamafawzy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/osamafawzy"/>
    <language>en</language>
    <item>
      <title>Laravel 13 for queue-heavy apps: a reviewer's checklist for the new queue features</title>
      <dc:creator>osama</dc:creator>
      <pubDate>Tue, 10 Nov 2026 13:36:02 +0000</pubDate>
      <link>https://dev.to/osamafawzy/laravel-13-for-queue-heavy-apps-a-reviewers-checklist-for-the-new-queue-features-1ahe</link>
      <guid>https://dev.to/osamafawzy/laravel-13-for-queue-heavy-apps-a-reviewers-checklist-for-the-new-queue-features-1ahe</guid>
      <description>&lt;p&gt;One production problem I traced wasn't a bug in one line. It was a request that waited on service A, which waited on B, which waited on a slow C, until everything timed out. The fix was to do only the required action synchronously and move the rest to queues.&lt;/p&gt;

&lt;p&gt;So when a Laravel release changes queues, the question I care about is: does this make queue behaviour easier or harder to review?&lt;/p&gt;

&lt;p&gt;Laravel 13 (released March 17, 2026, PHP 8.3+) adds a few features that move queue policy next to the code. This is a review of what the official 13.x docs say, plus the traps I'd check for in code review. It is not a report from a production migration.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;code&gt;Queue::route&lt;/code&gt;: one place for where jobs run
&lt;/h3&gt;

&lt;p&gt;Before, the queue and connection for a job were often decided at each dispatch site (&lt;code&gt;-&amp;gt;onQueue('video')&lt;/code&gt;) or in properties on the job. Laravel 13 lets you set them centrally, usually in a service provider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Support&lt;/span&gt;\&lt;span class="nc"&gt;Facades&lt;/span&gt;\&lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;boot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ProcessPodcast&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'redis'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'podcasts'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// An interface, trait or parent class routes every job that uses it&lt;/span&gt;
    &lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RequiresVideo&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'video'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also route many classes at once with an array, and the docs say a job can still override its route.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The trap: argument order.&lt;/strong&gt; The method signature is &lt;code&gt;route($class, $queue = null, $connection = null)&lt;/code&gt;: the queue comes before the connection. But the array form uses &lt;code&gt;[connection, queue]&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nc"&gt;ProcessPodcast&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'redis'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'podcasts'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// [connection, queue]&lt;/span&gt;
    &lt;span class="nc"&gt;ProcessVideo&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;   &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'videos'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;              &lt;span class="c1"&gt;// queue only&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So &lt;code&gt;Queue::route(ProcessPodcast::class, 'redis', 'podcasts')&lt;/code&gt; sends the job to a &lt;em&gt;queue&lt;/em&gt; named &lt;code&gt;redis&lt;/code&gt; on a &lt;em&gt;connection&lt;/em&gt; named &lt;code&gt;podcasts&lt;/code&gt;. In review, I'd ask for named arguments, as the docs use. (One caveat: Laravel's backward-compatibility promise doesn't cover parameter names.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Routing is a default, not a lock-in.&lt;/strong&gt; The docs say routing "can still be overridden by the job on a per-job basis", so a one-off dispatch can still go somewhere else:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;ProcessPodcast&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$podcast&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;onConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'redis'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;onQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'high'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The docs don't spell out the full precedence between a route and dispatch-time &lt;code&gt;onQueue()&lt;/code&gt; / &lt;code&gt;onConnection()&lt;/code&gt;. If a job's placement matters, I'd pin it with a test: &lt;code&gt;Queue::fake()&lt;/code&gt; and &lt;code&gt;Queue::assertPushedOn('high', ProcessPodcast::class)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Related: &lt;code&gt;Queue::forward&lt;/code&gt;.&lt;/strong&gt; The current 13.x docs also let you forward a whole queue to another queue and/or connection, so you can change queue infrastructure without touching jobs or dispatch sites:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;forward&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'reports'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'reports.fifo'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'sqs'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;forward&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'payments'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'sqs'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;route&lt;/code&gt; maps job classes; &lt;code&gt;forward&lt;/code&gt; maps queue names. The docs add that an explicit connection configured on a job takes precedence over a forwarded connection.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Job attributes: retry and timeout rules at the top of the class
&lt;/h3&gt;

&lt;p&gt;The 13.x docs configure these settings with attributes in &lt;code&gt;Illuminate\Queue\Attributes&lt;/code&gt; (Tries, Backoff, Timeout and FailOnTimeout are new in 13):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Contracts&lt;/span&gt;\&lt;span class="nc"&gt;Queue&lt;/span&gt;\&lt;span class="nc"&gt;ShouldQueue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Foundation&lt;/span&gt;\&lt;span class="nc"&gt;Queue&lt;/span&gt;\&lt;span class="nc"&gt;Queueable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Queue&lt;/span&gt;\&lt;span class="nc"&gt;Attributes&lt;/span&gt;\&lt;span class="nc"&gt;Backoff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Queue&lt;/span&gt;\&lt;span class="nc"&gt;Attributes&lt;/span&gt;\&lt;span class="nc"&gt;FailOnTimeout&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Queue&lt;/span&gt;\&lt;span class="nc"&gt;Attributes&lt;/span&gt;\&lt;span class="nc"&gt;MaxExceptions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Queue&lt;/span&gt;\&lt;span class="nc"&gt;Attributes&lt;/span&gt;\&lt;span class="nc"&gt;Timeout&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Queue&lt;/span&gt;\&lt;span class="nc"&gt;Attributes&lt;/span&gt;\&lt;span class="nc"&gt;Tries&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;#[Tries(25)]&lt;/span&gt;
&lt;span class="c1"&gt;#[MaxExceptions(3)]&lt;/span&gt;
&lt;span class="c1"&gt;#[Backoff([1, 5, 10])]&lt;/span&gt;
&lt;span class="c1"&gt;#[Timeout(120)]&lt;/span&gt;
&lt;span class="c1"&gt;#[FailOnTimeout]&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProcessPodcast&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;ShouldQueue&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Queueable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What each one means, from the docs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;#[Tries(25)]&lt;/code&gt;: up to 25 attempts. It takes precedence over the worker's &lt;code&gt;--tries&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;#[MaxExceptions(3)]&lt;/code&gt;: the job can be released many times (for example, when an API rate-limits it), but fails after 3 unhandled exceptions.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;#[Backoff([1, 5, 10])]&lt;/code&gt;: wait 1 s, then 5 s, then 10 s for every later retry. With no backoff, a failed job is retried immediately.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;#[Timeout(120)]&lt;/code&gt;: overrides the worker's &lt;code&gt;--timeout&lt;/code&gt; (default 60 s).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;#[FailOnTimeout]&lt;/code&gt;: by default a timeout uses one attempt and the job is retried. With this attribute, a timeout fails the job for good.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Attributes are optional; &lt;code&gt;tries()&lt;/code&gt; / &lt;code&gt;backoff()&lt;/code&gt; methods remain for dynamic logic. Stacking all five attributes on one class is my example; the docs show them one or two at a time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I'd check in review:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Timeout vs &lt;code&gt;retry_after&lt;/code&gt;.&lt;/strong&gt; The job timeout must be several seconds shorter than the connection's &lt;code&gt;retry_after&lt;/code&gt;. If it isn't, a slow job can be picked up again while it's still running, and processed twice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;#[FailOnTimeout]&lt;/code&gt; is not idempotency.&lt;/strong&gt; Picture a capture job: Laravel asks the gateway to capture 500 EGP, the gateway charges the card, and the connection drops before the response arrives. The job times out, and Laravel doesn't know whether the money moved. &lt;code&gt;#[FailOnTimeout]&lt;/code&gt; stops the automatic retry after a timeout, but it doesn't guarantee the external operation didn't complete, and exceptions still retry up to &lt;code&gt;#[Tries]&lt;/code&gt;. For payment jobs, send an idempotency key where the gateway supports one, and reconcile the payment status with the gateway before attempting another capture. (I covered the callback side of this in &lt;a href="https://dev.to/osamafawzy/webhooks-arrive-twice-and-out-of-order-making-payment-callbacks-duplicate-safe-in-laravel-4of2"&gt;an earlier article&lt;/a&gt;.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Timeouts on the HTTP client too.&lt;/strong&gt; Blocking calls like outgoing HTTP may not respect the job timeout, so set connect and request timeouts on the client.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PCNTL.&lt;/strong&gt; Job timeouts need the PCNTL extension on the worker.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. A related cache feature: &lt;code&gt;Cache::touch&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This one isn't a queue feature, but queue-heavy apps often keep coordination data in the cache, and its expiry matters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;touch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'key'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;touch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'key'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;addHours&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It extends the lifetime of an existing item without reading and storing it again. Two things to watch:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It returns &lt;code&gt;false&lt;/code&gt; when the key doesn't exist. It's not an upsert, so the caller has to handle the miss.&lt;/li&gt;
&lt;li&gt;It's not for locks. A long job holding a cache lock should use &lt;code&gt;Cache::lock(...)-&amp;gt;refresh()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. The upgrade notes a queue-heavy app should read
&lt;/h3&gt;

&lt;p&gt;These are in the 13.x upgrade guide, and they're the kind of change that breaks a listener or empties a cache after deploy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;JobAttempted&lt;/code&gt; event now has &lt;code&gt;$exception&lt;/code&gt; instead of the boolean &lt;code&gt;$exceptionOccurred&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;QueueBusy&lt;/code&gt; event's &lt;code&gt;$connection&lt;/code&gt; is now &lt;code&gt;$connectionName&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A new cache &lt;code&gt;serializable_classes&lt;/code&gt; option is &lt;code&gt;false&lt;/code&gt; in the new skeleton. If you cache PHP objects, you allow-list their classes.&lt;/li&gt;
&lt;li&gt;Default cache and Redis key prefixes, and the session cookie name, now use hyphens when an app relies on the framework's fallback values. Most apps already define these in their config; if yours doesn't, set &lt;code&gt;CACHE_PREFIX&lt;/code&gt;, &lt;code&gt;REDIS_PREFIX&lt;/code&gt; and &lt;code&gt;SESSION_COOKIE&lt;/code&gt; explicitly to keep the old behaviour, or you may start with a cold cache and logged-out users. Check this twice when several apps share the same Redis.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The checklist
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Routes use named arguments (or the array form, read carefully), and placements that matter are pinned by a test&lt;/li&gt;
&lt;li&gt;[ ] Every job's timeout is shorter than &lt;code&gt;retry_after&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;[ ] Jobs that aren't safe to repeat use &lt;code&gt;#[FailOnTimeout]&lt;/code&gt; and have a guard against running twice&lt;/li&gt;
&lt;li&gt;[ ] Payment jobs use idempotency keys and reconcile status before another attempt&lt;/li&gt;
&lt;li&gt;[ ] Jobs calling rate-limited APIs pair high &lt;code&gt;#[Tries]&lt;/code&gt; with &lt;code&gt;#[MaxExceptions]&lt;/code&gt; and a backoff&lt;/li&gt;
&lt;li&gt;[ ] HTTP clients inside jobs have their own timeouts&lt;/li&gt;
&lt;li&gt;[ ] Listeners on &lt;code&gt;JobAttempted&lt;/code&gt; / &lt;code&gt;QueueBusy&lt;/code&gt; are updated&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;CACHE_PREFIX&lt;/code&gt;, &lt;code&gt;REDIS_PREFIX&lt;/code&gt;, &lt;code&gt;SESSION_COOKIE&lt;/code&gt; and &lt;code&gt;serializable_classes&lt;/code&gt; are set before deploying&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which queue setting has bitten you in production?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sources: Laravel 13.x docs, &lt;a href="https://laravel.com/docs/13.x/releases" rel="noopener noreferrer"&gt;Release notes&lt;/a&gt;, &lt;a href="https://laravel.com/docs/13.x/queues" rel="noopener noreferrer"&gt;Queues&lt;/a&gt;, &lt;a href="https://laravel.com/docs/13.x/cache" rel="noopener noreferrer"&gt;Cache&lt;/a&gt;, &lt;a href="https://laravel.com/docs/13.x/upgrade" rel="noopener noreferrer"&gt;Upgrade guide&lt;/a&gt;, &lt;a href="https://api.laravel.com/docs/13.x/Illuminate/Support/Facades/Queue.html" rel="noopener noreferrer"&gt;Queue facade API reference&lt;/a&gt; (accessed November 10, 2026).&lt;/em&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>queues</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Webhooks arrive twice and out of order: making payment callbacks duplicate-safe in Laravel</title>
      <dc:creator>osama</dc:creator>
      <pubDate>Tue, 03 Nov 2026 05:49:53 +0000</pubDate>
      <link>https://dev.to/osamafawzy/webhooks-arrive-twice-and-out-of-order-making-payment-callbacks-duplicate-safe-in-laravel-4of2</link>
      <guid>https://dev.to/osamafawzy/webhooks-arrive-twice-and-out-of-order-making-payment-callbacks-duplicate-safe-in-laravel-4of2</guid>
      <description>&lt;p&gt;In my last post I wrote about proving a payment callback is real: verify the signature, then ask the gateway for the status instead of trusting the payload.&lt;/p&gt;

&lt;p&gt;That solves "who sent this?". It doesn't solve the next problem: a perfectly valid callback can arrive &lt;strong&gt;twice&lt;/strong&gt;, &lt;strong&gt;late&lt;/strong&gt;, or &lt;strong&gt;before&lt;/strong&gt; the event it depends on.&lt;/p&gt;

&lt;p&gt;This isn't theory. Tabby's webhook docs say it directly:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"the same event may occasionally arrive twice"&lt;/p&gt;

&lt;p&gt;"the delivery order is not guaranteed. A closed event can arrive before the authorized one."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Tabby also retries a failed delivery up to 4 more times. Other gateways have their own retry rules, so I treat every gateway's callbacks as "at least once, in any order" and design the handler for that.&lt;/p&gt;

&lt;p&gt;On a multi-branch e-commerce and POS platform I work on (40+ branches, 2M+ transactions a month), the callback handlers are built around four guards. Here is the general approach, with a simplified order/items/stock example.&lt;/p&gt;

&lt;h3&gt;
  
  
  The scenario
&lt;/h3&gt;

&lt;p&gt;A customer pays for an order with three items. The gateway sends a "paid" callback. Your handler:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;marks the order as paid,&lt;/li&gt;
&lt;li&gt;decrements stock for each item,&lt;/li&gt;
&lt;li&gt;sends a confirmation (email, SMS, push).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now the same callback arrives twice, 40 ms apart, on two different PHP workers. If nothing stops it, stock drops twice and the customer gets two confirmations. On a busy day, that's a stock count nobody trusts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why the obvious check isn't enough
&lt;/h3&gt;

&lt;p&gt;Most handlers start like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'reference'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$reference&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;firstOrFail&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Paid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;response&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;noContent&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// already handled&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;markAsPaid&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;decrementStock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looks safe, and it works when callbacks arrive seconds apart. But when two requests arrive together, both read &lt;code&gt;status = pending&lt;/code&gt; before either one writes &lt;code&gt;paid&lt;/code&gt;. Both pass the check. This is a classic check-then-act race: the check and the write are two separate steps, and the other request runs between them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Guard 1: a transaction and a row lock
&lt;/h3&gt;

&lt;p&gt;The fix is to make "read the status, decide, write" one step that only one request can run at a time for the same order. In Laravel that means a transaction plus &lt;code&gt;lockForUpdate()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Illuminate\Support\Facades\DB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="no"&gt;DB&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$reference&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$remote&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'reference'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$reference&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;lockForUpdate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;firstOrFail&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// Re-check INSIDE the lock. The second request waits here&lt;/span&gt;
    &lt;span class="c1"&gt;// until the first one commits, then sees the new status.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Paid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;markAsPaid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$remote&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;decrementStock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;attempts&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What changed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;lockForUpdate()&lt;/code&gt; adds &lt;code&gt;FOR UPDATE&lt;/code&gt; to the SELECT. The second request blocks on that row until the first transaction commits or rolls back.&lt;/li&gt;
&lt;li&gt;The status check now runs &lt;strong&gt;after&lt;/strong&gt; the lock is taken, so the second request sees &lt;code&gt;paid&lt;/code&gt; and stops.&lt;/li&gt;
&lt;li&gt;If anything throws, the transaction rolls back and the lock is released. Laravel's docs recommend exactly this pairing: pessimistic locks inside a transaction.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;attempts: 3&lt;/code&gt; retries the closure if the database reports a deadlock.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The trade-off: a lock makes requests for the &lt;strong&gt;same order&lt;/strong&gt; wait for each other. Requests for different orders don't block each other, because the lock is on one row. For payment callbacks that's a good deal. Keep the locked section short, though: no HTTP calls, no emails, no PDF generation inside it.&lt;/p&gt;

&lt;p&gt;That's also why the gateway re-check from my last post happens &lt;strong&gt;before&lt;/strong&gt; the transaction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Outside the lock: never hold a row lock while waiting on a network call.&lt;/span&gt;
&lt;span class="nv"&gt;$remote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$gateway&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fetchPayment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$reference&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// status, amount, currency&lt;/span&gt;

&lt;span class="no"&gt;DB&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$reference&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$remote&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...lock, re-check, write&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;attempts&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Guard 2: lock the stock too
&lt;/h3&gt;

&lt;p&gt;Stock has the same race, with one difference: two &lt;strong&gt;different&lt;/strong&gt; orders can compete for the same product. A customer online and a cashier in a branch can both sell the last unit in the same second.&lt;/p&gt;

&lt;p&gt;So inside the same transaction, I lock the product rows before touching stock:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;decrementStock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Order&lt;/span&gt; &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Lock every product in this order, in a fixed order (by id),&lt;/span&gt;
    &lt;span class="c1"&gt;// so two transactions never wait on each other in a circle.&lt;/span&gt;
    &lt;span class="nv"&gt;$products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;whereIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$items&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;pluck&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'product_id'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;orderBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;lockForUpdate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;keyBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$items&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$products&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$product&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Paid, but the stock is gone. Don't throw: a rollback here&lt;/span&gt;
            &lt;span class="c1"&gt;// would fail the callback and the gateway would retry forever.&lt;/span&gt;
            &lt;span class="c1"&gt;// Flag it so a human decides: refund, backorder, or transfer.&lt;/span&gt;
            &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;flagForReview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'insufficient_stock'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nv"&gt;$product&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;decrement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'stock'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details matter here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lock in a consistent order.&lt;/strong&gt; If transaction A locks product 5 then product 9, while transaction B locks 9 then 5, each can end up waiting for the other. That's a deadlock. Sorting by id before locking avoids that circle. &lt;code&gt;attempts: 3&lt;/code&gt; is the safety net if one still happens.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Don't throw for a business problem.&lt;/strong&gt; An exception rolls everything back, the handler returns a 500, and the gateway retries a callback that will never succeed. Record the problem and let a person handle it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Or let one UPDATE do the check
&lt;/h4&gt;

&lt;p&gt;For a single counter like stock, there's a lighter option: put the check inside the UPDATE itself, so reading, checking and writing happen in one SQL statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$affected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'stock'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'&amp;gt;='&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;decrement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'stock'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$item&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$affected&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// The condition failed: someone else took the stock first.&lt;/span&gt;
    &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;flagForReview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'insufficient_stock'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This runs as &lt;code&gt;UPDATE products SET stock = stock - ? WHERE id = ? AND stock &amp;gt;= ?&lt;/code&gt;. The database applies it atomically, so two concurrent requests can't both pass the &lt;code&gt;stock &amp;gt;= ?&lt;/code&gt; check for the last unit. One updates the row, the other gets &lt;code&gt;0&lt;/code&gt; affected rows. No explicit lock, no window between reading and writing.&lt;/p&gt;

&lt;p&gt;The rule behind it: &lt;strong&gt;never update based on a value you read earlier unless the UPDATE checks that value is still true.&lt;/strong&gt; The &lt;code&gt;WHERE stock &amp;gt;= ?&lt;/code&gt; condition does that for stock. The version column in Guard 4 below does the same for a whole row.&lt;/p&gt;

&lt;p&gt;When to use which: a conditional UPDATE is great for one counter. When you need to read several rows, make a decision across them and write several changes together, the transaction plus &lt;code&gt;lockForUpdate()&lt;/code&gt; is clearer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Guard 3: allow only forward status changes
&lt;/h3&gt;

&lt;p&gt;Duplicates are one problem. Order is another. If "captured" arrives before "authorized", a naive handler processes "captured", then gets "authorized" a few seconds later and moves the order &lt;strong&gt;backwards&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I give the payment status explicit allowed transitions and ignore anything that isn't one of them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;enum&lt;/span&gt; &lt;span class="nc"&gt;PaymentStatus&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;Pending&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;Authorized&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'authorized'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;Captured&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'captured'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;Failed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'failed'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;Refunded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'refunded'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;canTransitionTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;self&lt;/span&gt; &lt;span class="nv"&gt;$next&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;in_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Pending&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Authorized&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Captured&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Failed&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Authorized&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Captured&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Failed&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Captured&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Refunded&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Failed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Refunded&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And inside the lock:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;payment_status&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;canTransitionTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$remote&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// duplicate or late event: nothing to do, acknowledge it&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now a late "authorized" after "captured" is simply ignored, and a duplicate "captured" is ignored too, because &lt;code&gt;Captured → Captured&lt;/code&gt; isn't an allowed transition. One rule covers both problems.&lt;/p&gt;

&lt;p&gt;Each gateway names its statuses differently (Tabby, for example, sends lowercase statuses in webhooks and uppercase ones from its retrieve endpoint), so map them to your own enum at the edge and keep the rules in one place.&lt;/p&gt;

&lt;p&gt;Re-querying the gateway helps here as well: when a late event arrives, you ask the gateway for the &lt;strong&gt;current&lt;/strong&gt; status, not the status in that old event. The transition rule then stops anything that would move the order backwards.&lt;/p&gt;

&lt;h3&gt;
  
  
  Guard 4: a version column for writers that don't lock
&lt;/h3&gt;

&lt;p&gt;The lock protects the callback handler from other callback handlers. But callbacks aren't the only code that writes to an order. An admin panel action, a scheduled job, or a support tool might load the order, wait a little, and save it back. If that code doesn't take the lock, it can overwrite the status your callback just set.&lt;/p&gt;

&lt;p&gt;A version column catches this. Every write increments it, and every write only succeeds if the version is still the one you read:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$updated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'version'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
        &lt;span class="s1"&gt;'payment_status'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'version'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$updated&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Someone changed this order after we read it.&lt;/span&gt;
    &lt;span class="c1"&gt;// Reload and decide again; never overwrite blindly.&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StaleOrderException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is optimistic locking: it doesn't block anyone, it just refuses to save stale data. Row locks (pessimistic) protect the hot path where conflicts are likely: two callbacks for the same order. The version column (optimistic) protects the rest of the system where conflicts are rare but expensive.&lt;/p&gt;

&lt;h3&gt;
  
  
  Side effects after commit
&lt;/h3&gt;

&lt;p&gt;Emails, SMS and push notifications can't be rolled back. If you send them inside the transaction and the transaction then fails, the customer gets "payment confirmed" for an order that isn't confirmed.&lt;/p&gt;

&lt;p&gt;Laravel can hold a queued job until the transaction commits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;SendOrderConfirmation&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;afterCommit&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the transaction rolls back, the job is discarded. You can also turn this on for a whole queue connection with the &lt;code&gt;after_commit&lt;/code&gt; option.&lt;/p&gt;

&lt;h3&gt;
  
  
  What to return to the gateway
&lt;/h3&gt;

&lt;p&gt;Your response code tells the gateway whether to retry, so be deliberate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;200&lt;/strong&gt; when you processed the event, or deliberately ignored it as a duplicate or late event. Tabby's docs say to acknowledge with 200 only in those two cases.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-200&lt;/strong&gt; when you couldn't process it yet and want a retry. Tabby gives the example of a callback for a payment you haven't saved yet: respond with something like 404 and the retry arrives after your order exists. Conveniently, &lt;code&gt;firstOrFail()&lt;/code&gt; produces a 404 in Laravel.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Don't be slow.&lt;/strong&gt; Tabby times out after 1 minute and counts that as a failure. Short locked sections and queued side effects keep you well under it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The checklist
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Verify the callback and re-query the gateway (previous post). Do the HTTP call before the transaction.&lt;/li&gt;
&lt;li&gt;Wrap "read, decide, write" in &lt;code&gt;DB::transaction(..., attempts: 3)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lockForUpdate()&lt;/code&gt; the order row and re-check the status inside the lock.&lt;/li&gt;
&lt;li&gt;Lock stock rows in a fixed order, or use one conditional UPDATE (&lt;code&gt;WHERE stock &amp;gt;= ?&lt;/code&gt;); flag business problems instead of throwing.&lt;/li&gt;
&lt;li&gt;Allow only forward status transitions; ignore duplicates and late events with a 200.&lt;/li&gt;
&lt;li&gt;Add a version column so writers that don't lock can't overwrite newer data.&lt;/li&gt;
&lt;li&gt;Dispatch emails and notifications &lt;code&gt;afterCommit()&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;None of this is exotic. It's standard database tooling, used deliberately. The point is to design for "at least once, in any order" from day one, instead of finding out from a customer who got charged once and confirmed twice.&lt;/p&gt;

&lt;p&gt;How do you handle this in your projects: row locks, a unique table of processed event IDs, or something else? I'd like to hear what's worked for you.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Sources: &lt;a href="https://docs.tabby.ai/pay-in-4-custom-integration/webhooks" rel="noopener noreferrer"&gt;Tabby webhooks&lt;/a&gt; · &lt;a href="https://laravel.com/docs/13.x/queries#pessimistic-locking" rel="noopener noreferrer"&gt;Laravel pessimistic locking&lt;/a&gt; · &lt;a href="https://laravel.com/docs/13.x/database#database-transactions" rel="noopener noreferrer"&gt;Laravel database transactions&lt;/a&gt; · &lt;a href="https://laravel.com/docs/13.x/queues#jobs-and-database-transactions" rel="noopener noreferrer"&gt;Laravel jobs &amp;amp; database transactions&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>webhooks</category>
      <category>payments</category>
    </item>
  </channel>
</rss>
