<?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: Ahmad Hassan</title>
    <description>The latest articles on DEV Community by Ahmad Hassan (@ahmad129096).</description>
    <link>https://dev.to/ahmad129096</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%2F2883291%2F62a9aa7f-5f55-49d8-afef-72b513030ff8.png</url>
      <title>DEV Community: Ahmad Hassan</title>
      <link>https://dev.to/ahmad129096</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmad129096"/>
    <language>en</language>
    <item>
      <title>Two customers, one item left: preventing overselling with one PostgreSQL UPDATE</title>
      <dc:creator>Ahmad Hassan</dc:creator>
      <pubDate>Thu, 26 Nov 2026 17:42:51 +0000</pubDate>
      <link>https://dev.to/ahmad129096/two-customers-one-item-left-preventing-overselling-with-one-postgresql-update-4cjf</link>
      <guid>https://dev.to/ahmad129096/two-customers-one-item-left-preventing-overselling-with-one-postgresql-update-4cjf</guid>
      <description>&lt;p&gt;Two customers. One kurta left. Both press &lt;strong&gt;Place order&lt;/strong&gt; within the same second.&lt;/p&gt;

&lt;p&gt;In many stores, both orders go through. Somebody finds out later, usually the shop owner, on the phone, apologising.&lt;/p&gt;

&lt;p&gt;I'm building &lt;a href="https://www.ahmad-hassan.dev/case-studies/shopkit-pk" rel="noopener noreferrer"&gt;ShopKit PK&lt;/a&gt;, a platform that runs many online shops for Pakistani sellers from one codebase. Most of those sellers ship cash on delivery, where every wrong order becomes a refused parcel. So "never sell what you don't have" was a hard requirement, not a nice-to-have.&lt;/p&gt;

&lt;p&gt;Here's how it works, and the mistakes it avoids.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug everyone writes first
&lt;/h2&gt;

&lt;p&gt;The obvious version reads stock, checks it, then writes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inventoryLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findUnique&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;variantId&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="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="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onHand&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reserved&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;qty&lt;/span&gt;&lt;span class="p"&gt;)&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;OutOfStock&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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inventoryLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;variantId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;reserved&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;qty&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="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;Between the &lt;code&gt;findUnique&lt;/code&gt; and the &lt;code&gt;update&lt;/code&gt;, another request can do exactly the same check, see the same stock, and also pass. Two orders, one item.&lt;/p&gt;

&lt;p&gt;You can patch this with &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; and a transaction, but there's a simpler way that needs no explicit lock at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the check and the write one statement
&lt;/h2&gt;

&lt;p&gt;Stock in ShopKit is two numbers per variant: &lt;code&gt;on_hand&lt;/code&gt; and &lt;code&gt;reserved&lt;/code&gt;. What a customer can buy is &lt;code&gt;on_hand - reserved&lt;/code&gt;. A database check constraint keeps &lt;code&gt;0 â‰¤ reserved â‰¤ on_hand&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Reserving stock is a single conditional update:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;inventory_levels&lt;/span&gt;
   &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;reserved&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reserved&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nv"&gt;$q&lt;/span&gt;
 &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;store_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$s&lt;/span&gt;
   &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;variant_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$v&lt;/span&gt;
   &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;track_inventory&lt;/span&gt;
   &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;on_hand&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;reserved&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nv"&gt;$q&lt;/span&gt;
&lt;span class="k"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;on_hand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reserved&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If enough stock is left, one row comes back and the reservation is made. If not, zero rows come back and the line is out of stock. PostgreSQL takes a row lock for the duration of the update, so when two requests race, the second one re-checks the condition against the first one's result. &lt;strong&gt;Exactly one wins.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Note the &lt;code&gt;store_id&lt;/code&gt; in the &lt;code&gt;WHERE&lt;/code&gt;. Every store-owned table in ShopKit carries it (49 tables), and CI fails if a new table forgets. A bug in one shop can't touch another shop's stock.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multi-item orders: avoid the deadlock
&lt;/h2&gt;

&lt;p&gt;An order with several lines reserves several rows. If order A reserves shirt â†’ trousers and order B reserves trousers â†’ shirt at the same moment, each can end up holding one lock and waiting for the other. That's a deadlock.&lt;/p&gt;

&lt;p&gt;The fix is boring and reliable: &lt;strong&gt;always reserve lines in the same order&lt;/strong&gt;, sorted by &lt;code&gt;variant_id&lt;/code&gt;. Then two orders always queue in the same direction and can't wait on each other.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to reserve: not in the cart
&lt;/h2&gt;

&lt;p&gt;Reserving at add-to-cart sounds safer but isn't. Carts get abandoned, and stock gets locked by people who will never buy. ShopKit reserves when the order is &lt;strong&gt;placed&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cash on delivery:&lt;/strong&gt; the reservation has no expiry. The order is real.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Online payment:&lt;/strong&gt; the reservation expires at the payment deadline (a store setting).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Don't let correctness depend on a cron job
&lt;/h2&gt;

&lt;p&gt;Expired reservations have to be released. The tempting design is a cron job that sweeps them every few minutes. But on free hosting tiers cron is unreliable, and if it's late, items look sold out when they aren't.&lt;/p&gt;

&lt;p&gt;So expiry is handled &lt;strong&gt;lazily&lt;/strong&gt;: when a reservation for a variant fails, the code first releases that variant's expired reservations and tries again. The cron job still runs, but only to tidy up. If it never runs, stock is still correct.&lt;/p&gt;

&lt;p&gt;If a late payment webhook arrives after a reservation expired, the handler tries to reserve again. If the stock is still there, the order goes through; if not, the order is flagged instead of silently overselling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Double-clicks and flaky mobile networks
&lt;/h2&gt;

&lt;p&gt;Overselling's cousin is the duplicate order: a customer on a weak connection taps the button three times. Every checkout request carries an idempotency key. Inside the transaction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lockKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sb"&gt;`order-idem:${storeId}:${input.idempotencyKey}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;existing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findUnique&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;storeId_idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;storeId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;idempotencyKey&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="p"&gt;}&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="n"&gt;existing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;existing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// same order, no second reservation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In testing, many identical concurrent requests produce one order and one reservation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Put the check and the write in &lt;strong&gt;one statement&lt;/strong&gt; when you can. It's simpler than explicit locking.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Order your locks&lt;/strong&gt; to avoid deadlocks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reserve at order time&lt;/strong&gt;, not cart time.&lt;/li&gt;
&lt;li&gt;Design so correctness &lt;strong&gt;never waits on a background job&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Make checkout &lt;strong&gt;idempotent&lt;/strong&gt;; mobile networks will test it for you.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The full case study, with the architecture diagram, the per-domain routing and the outbox pattern, is here: &lt;strong&gt;&lt;a href="https://www.ahmad-hassan.dev/case-studies/shopkit-pk" rel="noopener noreferrer"&gt;ShopKit PK case study&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I'm Ahmad, a full-stack React and Node.js developer. If you're building something with tricky data or e-commerce rules, &lt;a href="https://www.ahmad-hassan.dev/#contact" rel="noopener noreferrer"&gt;let's talk&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>ecommerce</category>
      <category>node</category>
      <category>architecture</category>
    </item>
    <item>
      <title>This is my test article</title>
      <dc:creator>Ahmad Hassan</dc:creator>
      <pubDate>Wed, 19 Feb 2025 13:59:09 +0000</pubDate>
      <link>https://dev.to/ahmad129096/this-is-my-test-article-4eb0</link>
      <guid>https://dev.to/ahmad129096/this-is-my-test-article-4eb0</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fym09wghkzfcipd03wxwp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fym09wghkzfcipd03wxwp.jpg" alt=" " width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is my test blogs description&lt;/p&gt;

</description>
      <category>marketing</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
