<?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: Philippe</title>
    <description>The latest articles on DEV Community by Philippe (@philippesj).</description>
    <link>https://dev.to/philippesj</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%2F4019668%2F649fa012-79dc-48b5-86aa-7cc524c0e042.png</url>
      <title>DEV Community: Philippe</title>
      <link>https://dev.to/philippesj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/philippesj"/>
    <language>en</language>
    <item>
      <title>FOR UPDATE SKIP LOCKED: the Postgres feature that saved my digital cloakroom</title>
      <dc:creator>Philippe</dc:creator>
      <pubDate>Tue, 07 Jul 2026 14:34:24 +0000</pubDate>
      <link>https://dev.to/philippesj/for-update-skip-locked-the-postgres-feature-that-saved-my-digital-cloakroom-3dm</link>
      <guid>https://dev.to/philippesj/for-update-skip-locked-the-postgres-feature-that-saved-my-digital-cloakroom-3dm</guid>
      <description>&lt;p&gt;Last year I watched a nightclub cloakroom melt down at 3am: two hundred people waving paper tickets, three staff members, and a lost-ticket dispute blocking the whole line. I ended up building &lt;a href="https://stashme.io" rel="noopener noreferrer"&gt;StashMe&lt;/a&gt;, a digital cloakroom system for venues — and the scariest technical problem turned out to be one SQL clause away from boring.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem: "give this guest the next free hanger"
&lt;/h2&gt;

&lt;p&gt;The core operation sounds trivial. But on a busy night you get bursts of simultaneous check-ins, and the naive version fails exactly when you need it most:&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="c1"&gt;-- DON'T: classic read-then-write race&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;hanger&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;venue_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'FREE'&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- two requests get the same hanger here&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;hanger&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'OCCUPIED'&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two guests, one hanger, one very awkward conversation at pickup.&lt;/p&gt;

&lt;p&gt;The usual reflexes all have drawbacks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;App-level mutex or queue&lt;/strong&gt; — works on one server, breaks the moment you scale horizontally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;&lt;/strong&gt; — correct, but every concurrent check-in now waits in line for the &lt;em&gt;same&lt;/em&gt; first row. Your throughput becomes serial exactly during the rush.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Serializable isolation&lt;/strong&gt; — correct too, but now you're writing retry loops for serialization failures.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The fix: FOR UPDATE SKIP LOCKED
&lt;/h2&gt;

&lt;p&gt;Postgres has had the answer built in since 9.5, and it deserves more love:&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;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;hanger&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;venue_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'FREE'&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;
&lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;SKIP&lt;/span&gt; &lt;span class="n"&gt;LOCKED&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;1&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;SKIP LOCKED&lt;/code&gt; changes the locking behavior in one crucial way: instead of &lt;em&gt;waiting&lt;/em&gt; on rows another transaction holds, the query simply skips them. So ten concurrent check-ins each lock a &lt;strong&gt;different&lt;/strong&gt; free hanger, in parallel, with no queuing and no deadlocks. Each transaction claims its row, updates it, commits.&lt;/p&gt;

&lt;p&gt;It's the same primitive that makes Postgres-based job queues (like Graphile Worker or Solid Queue) viable: many workers, one table, no fighting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using it from Prisma
&lt;/h2&gt;

&lt;p&gt;Prisma's query API doesn't expose &lt;code&gt;SKIP LOCKED&lt;/code&gt;, but &lt;code&gt;$queryRaw&lt;/code&gt; inside an interactive &lt;code&gt;$transaction&lt;/code&gt; works fine:&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="nx"&gt;db&lt;/span&gt;&lt;span class="p"&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;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;hanger&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$queryRaw&lt;/span&gt;&lt;span class="s2"&gt;`
    SELECT id, number FROM hanger
    WHERE venue_id = &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;venueId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; AND status = 'FREE'
    ORDER BY number
    FOR UPDATE SKIP LOCKED
    LIMIT 1`&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;hanger&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;NoFreeHangerError&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="c1"&gt;// claim it inside the same transaction&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 that matter:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The lock only lives as long as the transaction.&lt;/strong&gt; The SELECT and the UPDATE that claims the row must happen inside the same transaction, or you've just rebuilt the race with extra steps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An empty result doesn't mean "no free hangers."&lt;/strong&gt; It can also mean "every free hanger is currently locked by someone else" — which, at a cloakroom, is precisely the moment the last coats are being taken. Decide explicitly whether that's a "full" or a "retry once" for your domain.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  When to reach for it
&lt;/h2&gt;

&lt;p&gt;If you're building anything that hands out finite resources under concurrent load — seats, time slots, inventory units, jobs, coat hangers — &lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt; is usually the answer before you reach for Redis, advisory locks, or a message queue. The database you already run solves the problem you were about to add infrastructure for.&lt;/p&gt;

&lt;p&gt;The cloakroom system is live at &lt;a href="https://stashme.io" rel="noopener noreferrer"&gt;stashme.io&lt;/a&gt; if you're curious what it looks like in production. Happy to answer questions in the comments — I've since reused this exact pattern in two other projects and it hasn't blinked.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>webdev</category>
      <category>prisma</category>
    </item>
  </channel>
</rss>
