<?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: Subhankar Roy</title>
    <description>The latest articles on DEV Community by Subhankar Roy (@s4roy).</description>
    <link>https://dev.to/s4roy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1315355%2F4aa0fcd6-faed-48e3-9be8-75996407d409.webp</url>
      <title>DEV Community: Subhankar Roy</title>
      <link>https://dev.to/s4roy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/s4roy"/>
    <language>en</language>
    <item>
      <title>How I Prevented Overselling with MongoDB Inventory Reservations</title>
      <dc:creator>Subhankar Roy</dc:creator>
      <pubDate>Thu, 26 Nov 2026 12:27:42 +0000</pubDate>
      <link>https://dev.to/s4roy/how-i-prevented-overselling-with-mongodb-inventory-reservations-11dc</link>
      <guid>https://dev.to/s4roy/how-i-prevented-overselling-with-mongodb-inventory-reservations-11dc</guid>
      <description>&lt;p&gt;Two customers can try to buy the last unit at the same time. On Elexify, one might be on the Next.js storefront while the other is on the Android app. Both use the same Node.js API, so checkout needs a single place to decide who gets the unit.&lt;/p&gt;

&lt;p&gt;I used MongoDB inventory reservations and atomic stock updates to make that decision at the database level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check stock and reserve it in the same write
&lt;/h2&gt;

&lt;p&gt;A checkout flow that reads stock and then updates it in a separate operation has a race condition. Two requests can both read &lt;code&gt;1&lt;/code&gt; before either writes, so both pass the availability check.&lt;/p&gt;

&lt;p&gt;Put the availability check in the update filter. Here’s a simplified Mongoose example. It assumes the API has already validated &lt;code&gt;quantity&lt;/code&gt; as a positive integer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nc"&gt;Inventory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;updateOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inventoryId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;available&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$gte&lt;/span&gt;&lt;span class="p"&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;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$inc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;available&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;quantity&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="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;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;session&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;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;matchedCount&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="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Insufficient stock"&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;MongoDB applies this update atomically to the inventory document. With one unit left and two requests for one unit each, the first successful update changes &lt;code&gt;available&lt;/code&gt; to &lt;code&gt;0&lt;/code&gt;. The other request can no longer match the stock condition.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep the reservation and stock update together
&lt;/h2&gt;

&lt;p&gt;When the reservation is stored in a separate document, run both writes in the same transaction and pass the same session to each operation. If creating the reservation fails, the stock update rolls back. For a cart with multiple products, the transaction can also roll back the entire reservation if one item is unavailable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make payment finalization safe to retry
&lt;/h2&gt;

&lt;p&gt;A payment gateway can send the same callback more than once or deliver events out of order. On Elexify, payment finalization is idempotent. One way to implement this is to condition each state change on the order’s current state and commit the order update and reservation consumption together. Once the order is paid, a repeated success callback should leave it paid without consuming its reservation again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test the last unit
&lt;/h2&gt;

&lt;p&gt;Start with one unit in stock and send two checkout requests concurrently. Assert that exactly one reservation succeeds and that available stock is &lt;code&gt;0&lt;/code&gt;. Then replay the successful payment callback and verify that the reservation is consumed only once. Elexify runs checkout concurrency and payment reconciliation tests in CI.&lt;/p&gt;

&lt;p&gt;For more on how the storefront and Android app share checkout and inventory, see my &lt;a href="https://subhankarroy.in/projects/elexify" rel="noopener noreferrer"&gt;Elexify engineering case study&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Further reading: &lt;a href="https://www.mongodb.com/docs/manual/core/write-operations-atomicity/" rel="noopener noreferrer"&gt;MongoDB’s guide to atomicity and transactions&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
