<?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: Allan Bontempo</title>
    <description>The latest articles on DEV Community by Allan Bontempo (@allanbontempo).</description>
    <link>https://dev.to/allanbontempo</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2295755%2F57f21a4b-2c16-4f0a-bf8d-d11caf120a28.jpeg</url>
      <title>DEV Community: Allan Bontempo</title>
      <link>https://dev.to/allanbontempo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/allanbontempo"/>
    <language>en</language>
    <item>
      <title>Race-Condition: How a Single SQL Line Eliminated 100 Lines of Retry and Lock Code</title>
      <dc:creator>Allan Bontempo</dc:creator>
      <pubDate>Wed, 27 May 2026 01:06:58 +0000</pubDate>
      <link>https://dev.to/allanbontempo/how-a-single-sql-line-eliminated-100-lines-of-retry-and-lock-code-46a6</link>
      <guid>https://dev.to/allanbontempo/how-a-single-sql-line-eliminated-100-lines-of-retry-and-lock-code-46a6</guid>
      <description>&lt;p&gt;hat "rare" bug that looks intermittent but is actually deterministic. It is just waiting for your pods to scale.&lt;/p&gt;

&lt;p&gt;I recently dealt with a textbook concurrency issue in a Java application running on multiple parallel instances, and the solution turned out to be much simpler than the workarounds we had piled on top of it. This is a walkthrough of the problem, the failed attempts, and the final fix, with code and diagrams so you can apply the same pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Setup
&lt;/h2&gt;

&lt;p&gt;The application stack:&lt;/p&gt;

&lt;p&gt;. Java with Quarkus and Hibernate (Panache)&lt;br&gt;
. Oracle as the relational database&lt;br&gt;
. Deployed on Kubernetes with 4 parallel pods consuming from a JMS queue&lt;br&gt;
. Each message triggers the creation of a record inside a parent entity, with a sequential number that resets per parent&lt;/p&gt;

&lt;p&gt;To keep things generic, think of it as "items inside an order":&lt;/p&gt;

&lt;p&gt;. Each order can have multiple items&lt;br&gt;
. Each item has a sequential number within the order (1, 2, 3, ...)&lt;br&gt;
. The numbering restarts at 1 for every new order&lt;br&gt;
. The primary key is composite: (order_id, item_number)&lt;/p&gt;

&lt;p&gt;The application had been running this way for years with a single instance. When the deployment scaled to four pods, primary key violations started showing up in production, intermittent and hard to reproduce.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Root Cause: SELECT MAX + 1
&lt;/h2&gt;

&lt;p&gt;Here is the original code (simplified):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="nf"&gt;nextItemNumber&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;em&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createQuery&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
            &lt;span class="s"&gt;"select max(i.itemNumber) from Item i where i.orderId = :orderId"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
            &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setParameter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"orderId"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getSingleResultOrNull&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1L&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;orElse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1L&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A textbook &lt;code&gt;SELECT MAX + 1&lt;/code&gt; pattern. The problem: &lt;strong&gt;SELECT acquires no lock&lt;/strong&gt;. Two pods can query at exactly the same moment, get the same value, and both try to insert the same number.&lt;/p&gt;

&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%2Fdksfv8f9ny0fme8limod.png" 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%2Fdksfv8f9ny0fme8limod.png" alt=" " width="800" height="682"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This worked for years with a single instance because there was no concurrency. The bug was always there. It just never had a chance to manifest.&lt;/p&gt;

&lt;h2&gt;
  
  
  First Attempt: Pessimistic Lock + Retry
&lt;/h2&gt;

&lt;p&gt;The first fix tried to add coordination between the pods using a pessimistic lock on the parent row, plus a retry on the duplicate exception.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Retry&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxRetries&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;delay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;retryOn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DuplicateItemException&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;delayUnit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChronoUnit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;SECONDS&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@Transactional&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Item&lt;/span&gt; &lt;span class="nf"&gt;createItem&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ItemPayload&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 1) Lock the parent row&lt;/span&gt;
    &lt;span class="n"&gt;em&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createNativeQuery&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""
        SELECT * FROM orders
        WHERE id = :id
        FOR UPDATE WAIT 5
        """&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setParameter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getSingleResult&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// 2) Get next number&lt;/span&gt;
    &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;nextNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nextItemNumber&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// 3) Insert the item&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;persist&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;itemNumber&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nextNumber&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PersistenceException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isUniqueViolation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&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="nf"&gt;DuplicateItemException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It reduced the frequency of failures, but did not eliminate them. Under high concurrency:&lt;/p&gt;

&lt;p&gt;. &lt;code&gt;WAIT 5&lt;/code&gt; would time out, throwing &lt;code&gt;LockTimeoutException&lt;/code&gt;&lt;br&gt;
. Retries piled up, with 5-second delays adding latency&lt;br&gt;
. After exhausting all retries, the operation still failed&lt;br&gt;
. Code complexity increased: a custom exception, retry annotation, unique-violation detection logic, error handling&lt;/p&gt;

&lt;p&gt;The fundamental problem was still there: &lt;code&gt;SELECT MAX + 1&lt;/code&gt; is a "read then write" pattern, and the lock was only mitigating the symptom, not removing the race.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Solution: Atomic UPDATE ... RETURNING
&lt;/h2&gt;

&lt;p&gt;The final solution was to add a counter column on the parent table and use an atomic &lt;code&gt;UPDATE ... RETURNING&lt;/code&gt; to generate the next number in a single operation.&lt;/p&gt;
&lt;h3&gt;
  
  
  Schema change
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="n"&gt;last_item_number&lt;/span&gt; &lt;span class="n"&gt;NUMBER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Backfill for existing rows&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_item_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;NVL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;item_number&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&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="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Semantics:&lt;/p&gt;

&lt;p&gt;. The column stores &lt;strong&gt;the last number used&lt;/strong&gt; (not "the next to use")&lt;br&gt;
. New orders start at 0 (no items created)&lt;br&gt;
. Each UPDATE increments and returns the new value, which is the number to assign&lt;/p&gt;
&lt;h3&gt;
  
  
  Code change
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Transactional&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="nf"&gt;nextItemNumber&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;em&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createNativeQuery&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""
            UPDATE orders
            SET last_item_number = last_item_number + 1
            WHERE id = :orderId
            RETURNING last_item_number INTO :next
            """&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setParameter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"orderId"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getSingleResult&lt;/span&gt;&lt;span class="o"&gt;()).&lt;/span&gt;&lt;span class="na"&gt;longValue&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That is the whole thing. Single statement, atomic by design.&lt;/p&gt;

&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%2F036xf961bxc7svsh5anw.png" 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%2F036xf961bxc7svsh5anw.png" alt=" " width="800" height="812"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Why It Works: The Mechanics of UPDATE Locking
&lt;/h2&gt;

&lt;p&gt;There are three properties that make this pattern bulletproof:&lt;/p&gt;
&lt;h3&gt;
  
  
  1. UPDATE acquires an automatic X-lock
&lt;/h3&gt;

&lt;p&gt;When you &lt;code&gt;UPDATE&lt;/code&gt; a row in Oracle (and most relational databases), the engine automatically acquires an exclusive lock on that row. Two concurrent transactions targeting the same row &lt;strong&gt;serialize by design&lt;/strong&gt;, without any explicit &lt;code&gt;FOR UPDATE&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. The lock is re-evaluated after waiting
&lt;/h3&gt;

&lt;p&gt;When Pod B unblocks (because Pod A committed), Oracle re-evaluates &lt;code&gt;SET counter = counter + 1&lt;/code&gt; using the committed value. So Pod B sees 4 (committed by A) and produces 5. It does not use the value it saw before being blocked.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. RETURNING is atomic with the update
&lt;/h3&gt;

&lt;p&gt;The value returned is the post-update value, computed in the same statement. There is no window between "increment" and "read the new value" where another transaction could interfere.&lt;/p&gt;

&lt;p&gt;Compare this with the &lt;code&gt;SELECT MAX + 1&lt;/code&gt; pattern, which separates the read and the write into two steps with no protection between them.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;SELECT MAX + 1&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;UPDATE counter + 1 RETURNING&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Acquires lock&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes, automatic&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serializes?&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes, on the row&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Two pods can get same value&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Detects conflict&lt;/td&gt;
&lt;td&gt;At INSERT (constraint violation)&lt;/td&gt;
&lt;td&gt;Never (no conflict possible)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Bonus Layer: Eliminating Redis Dependency
&lt;/h2&gt;

&lt;p&gt;The same application had another counter being generated through Redis (&lt;code&gt;INCR&lt;/code&gt; on a key formatted as &lt;code&gt;"{order_id}/{item_number}"&lt;/code&gt;). This was used for a secondary sequential number inside each item.&lt;/p&gt;

&lt;p&gt;That counter had similar problems:&lt;/p&gt;

&lt;p&gt;. &lt;strong&gt;No transactional atomicity with the database&lt;/strong&gt;: if the &lt;code&gt;INSERT&lt;/code&gt; rolled back, the Redis &lt;code&gt;INCR&lt;/code&gt; did not roll back. Result: gaps in the sequence (burned numbers).&lt;br&gt;
. &lt;strong&gt;Manual reconciliation logic&lt;/strong&gt;: a periodic check tried to detect divergence between Redis and the database and fix it. The reconciliation had bugs and missed certain edge cases.&lt;br&gt;
. &lt;strong&gt;Critical dependency in the hot path&lt;/strong&gt;: if Redis was unavailable, the consumer was stuck even when the database was healthy.&lt;/p&gt;

&lt;p&gt;The same counter pattern applied to the secondary table eliminated all of that:&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;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="n"&gt;last_subitem_number&lt;/span&gt; &lt;span class="n"&gt;NUMBER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Transactional&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="nf"&gt;nextSubItemNumber&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;itemNumber&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;em&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createNativeQuery&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"""
            UPDATE items
            SET last_subitem_number = last_subitem_number + 1
            WHERE order_id = :orderId AND item_number = :itemNumber
            RETURNING last_subitem_number INTO :next
            """&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setParameter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"orderId"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setParameter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"itemNumber"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemNumber&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getSingleResult&lt;/span&gt;&lt;span class="o"&gt;()).&lt;/span&gt;&lt;span class="na"&gt;intValue&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Redis is now used only for caching reference data, not for generating unique IDs in the transactional path.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Got Removed
&lt;/h2&gt;

&lt;p&gt;After applying the pattern in both layers, the following code disappeared:&lt;/p&gt;

&lt;p&gt;. &lt;code&gt;DuplicateItemException&lt;/code&gt; class&lt;br&gt;
. &lt;code&gt;@Retry&lt;/code&gt; annotation on the creation method&lt;br&gt;
. Custom &lt;code&gt;isUniqueViolation&lt;/code&gt; detection logic&lt;br&gt;
. Try/catch wrapping the &lt;code&gt;INSERT&lt;/code&gt; to convert exceptions&lt;br&gt;
. Redis dependency for sequential generation (still used for caching)&lt;br&gt;
. Reconciliation method that compared Redis state with the database (a routine with edge cases that needed maintenance)&lt;br&gt;
. Configuration properties related to Redis TTL for these counters&lt;br&gt;
. Approximately 100 lines of error handling and ceremony&lt;/p&gt;

&lt;p&gt;The code that remained is more declarative and easier to reason about. The race condition is gone, not by mitigation but by design.&lt;/p&gt;

&lt;h2&gt;
  
  
  When NOT to Use This Pattern
&lt;/h2&gt;

&lt;p&gt;This is not a silver bullet. The pattern works well when:&lt;/p&gt;

&lt;p&gt;. The number you are generating belongs to a clear "parent" entity that already exists in the database&lt;br&gt;
. The numbering is scoped to that parent (resets per parent)&lt;br&gt;
. The transaction boundary fits naturally around the increment and the insert&lt;br&gt;
. You can afford serialization on the parent row (concurrent creates within the same parent serialize)&lt;/p&gt;

&lt;p&gt;It does not fit when:&lt;/p&gt;

&lt;p&gt;. The ID needs to be generated before any database row exists (consider snowflake, UUID v7, or external sequence)&lt;br&gt;
. You need globally unique IDs across all entities (use a sequence, not a counter)&lt;br&gt;
. The serialization overhead on the parent row is unacceptable for your workload (rare, but possible in extreme scenarios where you would need application-level sharding anyway)&lt;/p&gt;

&lt;p&gt;For the common case of "sequential numbers inside a parent entity in a multi-instance application," the counter column with atomic UPDATE is hard to beat.&lt;/p&gt;

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

&lt;p&gt;A lot of race conditions in legacy systems trace back to &lt;code&gt;SELECT MAX + 1&lt;/code&gt; inherited from single-instance days. The cost of patching with explicit locks and retries is high and never fully resolves the underlying issue.&lt;/p&gt;

&lt;p&gt;A well-placed counter column with &lt;code&gt;UPDATE ... RETURNING&lt;/code&gt; is:&lt;/p&gt;

&lt;p&gt;. &lt;strong&gt;Simpler&lt;/strong&gt;: one SQL statement replaces a multi-step routine&lt;br&gt;
. &lt;strong&gt;Safer&lt;/strong&gt;: serialization is guaranteed by the database, not by application logic&lt;br&gt;
. &lt;strong&gt;Faster&lt;/strong&gt;: no retry latency, no lock timeout cascades&lt;br&gt;
. &lt;strong&gt;More transparent&lt;/strong&gt;: the intent is clear in the code&lt;/p&gt;

&lt;p&gt;Relational databases were designed for this pattern decades ago. Before reaching for distributed locks, queues, or eventual consistency, it is worth checking whether the database already has the tools to solve the problem natively. In many cases, it does.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;. Oracle documentation on &lt;code&gt;UPDATE ... RETURNING INTO&lt;/code&gt;: &lt;a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/RETURNING-INTO-clause.html" rel="noopener noreferrer"&gt;docs.oracle.com&lt;/a&gt;&lt;br&gt;
. Oracle row-level locking behavior: &lt;a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/cncpt/data-concurrency-and-consistency.html" rel="noopener noreferrer"&gt;Concepts Guide&lt;/a&gt;&lt;br&gt;
. Martin Kleppmann on the limits of distributed locks (Redlock): &lt;a href="https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html" rel="noopener noreferrer"&gt;martin.kleppmann.com&lt;/a&gt;&lt;br&gt;
. MicroProfile Fault Tolerance specification: &lt;a href="https://microprofile.io/specifications/microprofile-fault-tolerance/" rel="noopener noreferrer"&gt;microprofile.io&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;If you have dealt with similar concurrency issues in your projects, what was your solution? Did you go with a database-native approach or a distributed lock?&lt;/p&gt;




</description>
      <category>java</category>
      <category>oracle</category>
      <category>concurrency</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
