<?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: Dmitry Golovin</title>
    <description>The latest articles on DEV Community by Dmitry Golovin (@dgolovindev).</description>
    <link>https://dev.to/dgolovindev</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%2F4271439%2Fe37667b9-ca41-417c-b3ae-8394d3b3520b.jpg</url>
      <title>DEV Community: Dmitry Golovin</title>
      <link>https://dev.to/dgolovindev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dgolovindev"/>
    <language>en</language>
    <item>
      <title>Distributed reentrant locks for Node.js — Part 1</title>
      <dc:creator>Dmitry Golovin</dc:creator>
      <pubDate>Mon, 30 Nov 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/dgolovindev/part-1-cross-worker-reentrancy-and-multi-key-locking-2nk3</link>
      <guid>https://dev.to/dgolovindev/part-1-cross-worker-reentrancy-and-multi-key-locking-2nk3</guid>
      <description>&lt;p&gt;&lt;em&gt;The lock that grew out of the code: cross-worker reentrancy, multi-key locking, and fixed TTLs.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Code: &lt;a href="https://github.com/dgolovin-dev/drlock" rel="noopener noreferrer"&gt;dgolovin-dev/drlock&lt;/a&gt;. &lt;a href="https://github.com/dgolovin-dev/drlock/blob/main/CONTRIBUTING.md" rel="noopener noreferrer"&gt;Issues and PRs are welcome&lt;/a&gt;. Part 1&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is a game backend. Clients hold long-lived connections. Players enter tournaments, trade items, and move value through a ledger. The code needed a lock around shared player state, and eventually that lock had to cross worker boundaries. Its ownership model changed twice, local reentry ended up cheap, and this is where the model breaks. Part 1 covers the origin and the two pivots; later parts cover the production machinery in depth: the TTL, the watchdog, the store contract, and metrics. The code is a reference implementation, not a drop-in library: decisions you can take apart and adapt.&lt;/p&gt;

&lt;h2&gt;
  
  
  A local lock, not a database lock
&lt;/h2&gt;

&lt;p&gt;The first version ran on one server, in one process. Inventory and game statistics were guarded by a per-user lock. The point was not to wrap the whole operation in one bigger transaction; it was to stop two requests for the same player from modifying the same state in parallel.&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="c1"&gt;// Worker A: player API handler&lt;/span&gt;
&lt;span class="n"&gt;userServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'shop:buy-item'&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="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${playerId}`&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="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="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;readBalance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerId&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;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;itemId&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;balance&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;price&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;InsufficientFunds&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="nf"&gt;debit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// not locked internally&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;addItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// not locked internally&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;The writes were small, isolated transactions: debit currency, buy an item, update a statistic. They were not independent at the application level, though: a check could read inventory and balance, then write based on both, and another request could change one in between. The lock kept that read-check-write sequence whole.&lt;/p&gt;

&lt;p&gt;The lock was local and separate from the database from the start. We did not use transaction-scoped database locks: bad experience on MySQL under load, and we expected to move to Cassandra or something else. PostgreSQL was tempting later (fail-fast, transactional scripts), but it would have made the database the definition of the lock. We wanted the store replaceable; MySQL was just the first implementation in the stack.&lt;/p&gt;

&lt;p&gt;Reentrancy did not start as a feature. The first attempt was one lock around the whole operation, but it could be missed on a path; when business logic was reused, a function or service call could acquire a lock the caller already held. A shop purchase is the simplest example: the purchase holds the user lock, checks the transaction, then calls inventory to grant the item and profile to debit the balance. Each of those calls could also run on its own, so each needed its own lock.&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="c1"&gt;// Worker A: purchase API handler&lt;/span&gt;
&lt;span class="n"&gt;userServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'shop:purchase'&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="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;storeItemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;txId&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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${playerId}`&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="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="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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="n"&gt;shop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getGoods&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;storeItemId&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;shop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;checkTransaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;storeItemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;txId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// re-enters lock&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;inventory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                   &lt;span class="c1"&gt;// re-enters lock&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;debit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                        &lt;span class="c1"&gt;// re-enters lock&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="c1"&gt;// shop.checkTransaction takes the same lock again&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;checkTransaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;storeItemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;txId&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;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${playerId}`&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="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="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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without reentrancy, nested locking leaves two choices, and both are bad. The first is to release and reacquire the lock between steps, spelling out the sequence by hand: shop lock, check, release; profile lock, debit, release; inventory lock, grant, release. The caller can follow it, but the lock no longer covers the operation as a whole, and every new step has to remember the pattern.&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="c1"&gt;// Worker A: same purchase, without reentrancy&lt;/span&gt;
&lt;span class="n"&gt;userServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'shop:purchase'&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="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;storeItemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;txId&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="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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="n"&gt;shop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getGoods&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;storeItemId&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;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${playerId}`&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;shop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;checkTransaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;storeItemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;txId&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;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${playerId}`&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;debit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${playerId}`&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;inventory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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;The second approach launches the parts asynchronously and lets them queue on the lock. They run in whatever order they reach it. A fair lock would keep that arrival order, but arrival order comes from scheduling, not the business logic. Add a call, a retry, or another worker, and the sequence changes. Real-money purchases made the failure visible: the purchase had no single owner. The shop checked the transaction, started the credit and grant asynchronously, and released the lock. The coins and the item still landed in either order; profile and inventory were already waiting on the same lock, and when it released, they raced. Events had no purchase context, so clients and tests rebuilt state from pieces. The refcount let the purchase hold one lock across several steps instead of a lock/release pair per operation.&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="c1"&gt;// Worker A: store callback handler&lt;/span&gt;
&lt;span class="n"&gt;paymentServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'store:paid'&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="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;storeItemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;txId&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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${playerId}`&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="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="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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="n"&gt;shop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getGoods&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;storeItemId&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;shop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;checkTransaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;storeItemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;txId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;credit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// queues on the lock&lt;/span&gt;
    &lt;span class="n"&gt;inventory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// queues on the lock&lt;/span&gt;
    &lt;span class="c1"&gt;// awaited, this deadlocks without reentry&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;On one server, the local map was the whole truth: if the same player appeared in two processes, the lock would not know. That case did not exist yet. Local-first was an accident of deployment, but it kept the lock small: it only had to answer a local question — is this key already held in this process? — and that was cheap. The cost of being wrong was contained. That is the takeaway, not that local locks scale. They do not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code reuse, local reentry, TTL, and the first logs
&lt;/h2&gt;

&lt;p&gt;The shop purchase was not special: the same collision showed up wherever business operations were reused. The fix was to put locks wherever they were required and make the second acquire cheap. It was not a concurrency problem; it was the same task asking for the same thing.&lt;/p&gt;

&lt;p&gt;Reentrancy lived in the manager as a local counter for the same key and execution context: first acquire set one, nested acquire incremented, inner release decremented, outer release freed. No lock flag traveled through the business code.&lt;/p&gt;

&lt;p&gt;A fixed TTL and a max acquire time arrived together: the TTL bound hold time, the acquire timeout bounded wait time. With &lt;code&gt;runLocked&lt;/code&gt; doing the release, a double release was not a manual-unlock mistake; it usually meant the lock had expired or been replaced. The log was a signal that the lock had died, not that the caller had made a mistake. Logs were cheap then.&lt;/p&gt;

&lt;p&gt;The app-store receipt flow shows the shape. Before App Store Server Notifications, the backend validated purchases against Apple's old &lt;code&gt;verifyReceipt&lt;/code&gt; endpoint — a slow HTTP call that we throttled to stay under Apple's rate limit. Restore purchases sent every transaction the client had; testers had hundreds. The lock kept one player's checks sequential, so a restore could not fire a burst of parallel requests to Apple while other players checked in parallel. Without a cache, every restore revalidated the same receipts, and one player's checks went to the store API one at a time. The problem was acquisition wait, not hold time: each receipt queued behind the previous one for the same player. The logger showed that accumulated wait, which pushed us to add a cache; deduplication layered cleanly on top of the serialization later.&lt;/p&gt;

&lt;p&gt;Reentrancy, TTL, the acquire timeout, and basic logs were operational responses to reuse and failure, and they arrived before distribution, which is why they survived it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redesign #1: one task, several workers
&lt;/h2&gt;

&lt;p&gt;Load grew, bots appeared, and we added workers. The protocol stayed stateful: a player is pinned to a worker, so most actions stay local and bus traffic stays low. The local lock survived, and a distributed lock did not appear at the same time. The first cross-player operations ran without one. Adding a friend: A's worker takes A's lock, writes the request, notifies B, and releases. B's worker takes B's lock, confirms, creates the paired request, notifies A, and releases. If both requests exist, they are friends. Each lock covers one local step, and nobody waits while holding one.&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="c1"&gt;// Worker A: client calls the user API&lt;/span&gt;
&lt;span class="n"&gt;userServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'friend:request'&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="n"&gt;fromUserId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toUserId&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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${fromUserId}`&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="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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;friendRequests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromUserId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toUserId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'friend:request'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;fromUserId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toUserId&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="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Worker B: serviceBus delivers the event&lt;/span&gt;
&lt;span class="n"&gt;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'friend:request'&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;fromUserId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toUserId&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${toUserId}`&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="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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;friendRequests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;confirm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toUserId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fromUserId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'friend:request.ack'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;fromUserId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toUserId&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="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Worker A: notify the user that the request was accepted&lt;/span&gt;
&lt;span class="n"&gt;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'friend:request.ack'&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;fromUserId&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="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;userServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'friend:confirmed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;fromUserId&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;The marketplace model was simple: a player listed an item, and any other player could buy it. The first implementation was asynchronous: the buyer's worker debited the coins and emitted a purchase event; the seller's worker credited the coins, removed the item, and emitted it back; the buyer's worker added the item. Sequential locks, one at a time, fire-and-forget.&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="c1"&gt;// Worker A: client calls the market API&lt;/span&gt;
&lt;span class="n"&gt;userServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:buy'&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="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${buyerId}`&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="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="n"&gt;coins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="nf"&gt;debit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:take'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Worker B: takes the coins and sends the item&lt;/span&gt;
&lt;span class="n"&gt;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:take'&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${sellerId}`&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="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="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;credit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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="nf"&gt;removeItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:item'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Worker A: adds the item&lt;/span&gt;
&lt;span class="n"&gt;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:item'&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${buyerId}`&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="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="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;addItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="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;That worked until the buyer's API needed a synchronous result — success or failure — so a step had to wait for another worker. Then the lock was held across the wait. The purchase is the clearest case: A takes A's lock, sends coins to B, and waits for the item. B takes B's lock, adds the coins, sends the item, and releases. A receives the item, puts it in inventory, and releases. It looks fine until both players send coins to each other at the same time. A holds A's lock and waits for B; B holds B's lock and waits for A. Deadlock.&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="c1"&gt;// Worker A handles A -&amp;gt; B; worker B handles B -&amp;gt; A&lt;/span&gt;
&lt;span class="n"&gt;userServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:buy'&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="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${buyerId}`&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="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="n"&gt;coins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="nf"&gt;debit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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;received&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;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:take'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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="nf"&gt;addItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;received&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="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Callee worker: takes the seller's lock&lt;/span&gt;
&lt;span class="n"&gt;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:take'&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${sellerId}`&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="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="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;credit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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="nf"&gt;removeItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="n"&gt;itemId&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="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;pre data-lang="mermaid"&gt;&lt;code&gt;sequenceDiagram
    participant A as Worker A
    participant Store as Lock store
    participant B as Worker B

    A-&amp;gt;&amp;gt;Store: acquire(player:A)
    Store--&amp;gt;&amp;gt;A: held
    B-&amp;gt;&amp;gt;Store: acquire(player:B)
    Store--&amp;gt;&amp;gt;B: held
    A-&amp;gt;&amp;gt;Store: acquire(player:B)
    Note over Store: held by B, A waits
    B-&amp;gt;&amp;gt;Store: acquire(player:A)
    Note over Store: held by A, B waits
    Note over A,B: deadlock: A waits on B, B waits on A&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;The workaround was to release the lock before waiting and reacquire after.&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="c1"&gt;// Worker A: release A's lock before the waiting request&lt;/span&gt;
&lt;span class="n"&gt;userServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:buy'&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="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="n"&gt;coins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${buyerId}`&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;debit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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="c1"&gt;// market:take is the same callee handler as above&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;received&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;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:take'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${buyerId}`&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;addItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;received&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;That is easy to write once and easy to forget in the next handler, and the bugs floated between runs. To make the misses visible, the TTL got a watchdog: if a lock was still held at the deadline, it logged, released, and let the flow continue. It caught the deadlocks we missed but did not make the design feel correct. We fixed the flows to be deadlock-free, but the lock still felt fragile, inconvenient, and wrong; every handler required too much thought about lock order and release points.&lt;/p&gt;

&lt;p&gt;The deeper problem was ownership. The interaction was one logical operation, but its context lived inside a single worker: A's worker held A's lock, B's worker held B's lock, and their sequence lived in messages. If A's worker died after the debit but before B's credit, the next worker had to reconstruct what happened. The lock belonged to the context, not the worker, but a context bounded by one worker says nothing once it crosses to another.&lt;/p&gt;

&lt;p&gt;Redesign #1 changed that scope: the context no longer lived inside one worker but within the call chain, passed with each message. Most distributed locks are owned by a process or worker; here the owner was a logical execution that could travel from worker to worker. That is where the store came in. Its record, one per key, was &lt;code&gt;{ contextId, workers, expiresAt }&lt;/code&gt;: the logical owner, the workers that joined it, and the key's deadline. Worker B entered the same context, the store added B to the holder set, and B re-entered the lock instead of blocking. The purchase acquired both locks up front — pessimistic locking — instead of releasing and reacquiring.&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;contextId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`market:${id}`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Worker A: takes the buyer's lock, then the seller's, in sequence&lt;/span&gt;
&lt;span class="n"&gt;userServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:buy'&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="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runInContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contextId&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="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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${buyerId}`&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="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="n"&gt;coins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${sellerId}`&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="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="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;debit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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;received&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;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:take'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;contextId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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;await&lt;/span&gt; &lt;span class="nf"&gt;addItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;received&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="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="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Worker B: joins the same context and re-enters the seller's lock&lt;/span&gt;
&lt;span class="n"&gt;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:take'&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;contextId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runInContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contextId&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="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${sellerId}`&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="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="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;credit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;coins&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="nf"&gt;removeItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="n"&gt;itemId&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="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;pre data-lang="mermaid"&gt;&lt;code&gt;sequenceDiagram
    participant A as Worker A
    participant Store as Lock store
    participant B as Worker B

    A-&amp;gt;&amp;gt;Store: acquire(player:A, context=job:881)
    Store--&amp;gt;&amp;gt;A: held
    A-&amp;gt;&amp;gt;Store: acquire(player:B, context=job:881)
    Store--&amp;gt;&amp;gt;A: held
    A-&amp;gt;&amp;gt;B: call with contextId=job:881
    B-&amp;gt;&amp;gt;Store: acquire(player:B, context=job:881)
    Store--&amp;gt;&amp;gt;B: joined, no block
    Note over Store: contextId=job:881&amp;lt;br/&amp;gt;workers={A, B}&amp;lt;br/&amp;gt;expiresAt fixed
    B-&amp;gt;&amp;gt;Store: release(player:B, worker B)
    A-&amp;gt;&amp;gt;Store: release(player:B, worker A)
    A-&amp;gt;&amp;gt;Store: release(player:A, worker A)&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;It changed the question the lock was answering. Before, it was "which process holds this key?" After, it was "which logical execution holds this key, and which processes are currently part of it?" The second question is the one the code actually cared about.&lt;/p&gt;

&lt;p&gt;The mechanical cost was a context id on every message and a holder set in the store. The conceptual cost was larger: the lock belonged to an execution, not a worker, and could have several participants. That helps a handoff, but it has to be request/response: the caller waits for the callee before continuing. Fire-and-forget messages create parallel branches, and a lock will not put them back in order.&lt;/p&gt;

&lt;p&gt;After the redesign, the task code became linear again: start, acquire, do a step, hand off the context, continue. The lock knew the operation was the same even when the process was not. With Redis, one context could take several locks in sequence. In the purchase, A's worker takes A's lock, then B's, sends the coins, and waits for the item. B's worker re-enters B's held lock, adds the coins, returns the item, exits. A adds the item, releases B, releases A. The release-before-wait dance was gone. The remaining problem was order: every caller had to keep the same key order, a convention rather than a guarantee. It pushed the next redesign.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redesign #2: multi-key locks
&lt;/h2&gt;

&lt;p&gt;The purchase was still between two players — the buyer and the seller — but the two locks had to be taken as one set, not one after another.&lt;/p&gt;

&lt;p&gt;The natural implementation was sequential: lock the buyer, lock the seller. Under contention, two purchases could take the same keys in opposite order. Player A buys B's listing while B buys A's listing: one transaction locks A then B, the other locks B then A. Each holds its first key and waits for the second. Neither can finish. The first reproducible deadlocks appeared here: the textbook &lt;a href="https://en.wikipedia.org/wiki/Deadlock" rel="noopener noreferrer"&gt;hold-and-wait cycle&lt;/a&gt;.&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="c1"&gt;// Worker A: lock the buyer, then the seller, in sequence&lt;/span&gt;
&lt;span class="n"&gt;userServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:buy'&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="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${buyerId}`&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="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="n"&gt;coins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`player:${sellerId}`&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="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="c1"&gt;// debit the buyer, transfer the item, credit the seller&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="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;Without a multi-key lock, the fix was local: sort the keys at every call site and acquire them in the same order (&lt;a href="https://en.wikipedia.org/wiki/Deadlock_prevention_algorithms" rel="noopener noreferrer"&gt;resource ordering&lt;/a&gt;). That breaks the cycle but spreads the same boilerplate everywhere. We added a multi-key lock to centralize it. The first version only sorted and acquired in order: it removed the inconsistent-order deadlock, but still waited while holding a partial set. If the second key was busy, the first stayed locked.&lt;/p&gt;

&lt;p&gt;There was a fork. We could build a fair lock with a queue, or keep all-or-nothing and optimize how long locks stayed held. Fair locks are more complex and slower — see &lt;a href="https://redisson.pro/docs/data-and-services/locks-and-synchronizers/" rel="noopener noreferrer"&gt;Redisson's FairLock&lt;/a&gt; or &lt;a href="https://github.com/microsoft/etcd3/blob/main/src/mutex.ts" rel="noopener noreferrer"&gt;etcd3 Mutex&lt;/a&gt;. Fairness increases acquire wait: you wait for everyone ahead of you. And if the queue is per key, earlier locks are held while waiting for later ones, so hold time grows too. We chose the second path.&lt;/p&gt;

&lt;p&gt;The first all-or-nothing version was a retry loop: take the set one key at a time; if a key is busy, release what you took and retry. Then we batched it into one Redis MULTI: one round-trip instead of N, and a shorter partial window. Pass the full set; if any key is busy, release the partial set and retry the batch. It never waits while holding a partial set, so a hold-and-wait cycle cannot form. Sequential acquisitions in app code can still deadlock; this path cannot.&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="c1"&gt;// Worker A: buy a listed item from another player&lt;/span&gt;
&lt;span class="n"&gt;userServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:buy'&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="n"&gt;buyerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&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="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sb"&gt;`player:${buyerId}`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sb"&gt;`player:${sellerId}`&lt;/span&gt;&lt;span class="p"&gt;]&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="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="n"&gt;coins&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sellerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// both players or neither; partial results are released&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;pre data-lang="mermaid"&gt;&lt;code&gt;sequenceDiagram
    participant Task
    participant Store

    Task-&amp;gt;&amp;gt;Store: acquire([k1, k2])
    Store--&amp;gt;&amp;gt;Task: k1 held, k2 busy
    Task-&amp;gt;&amp;gt;Store: release k1 (partial)
    Note over Task: wait for k2 (poll or wakeup)
    Task-&amp;gt;&amp;gt;Store: acquire([k1, k2]) retry
    Store--&amp;gt;&amp;gt;Task: held&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;The partial set is briefly visible. That is why there is no FIFO fairness: a batch can retry while single-key work slips through, and a hot key can starve it. For this workload, that was the right trade.&lt;/p&gt;

&lt;p&gt;The TTL tightened here too. With multi-player operations, we settled on roughly 10–20 seconds: the upper end of observed hold times plus room for a retry, chosen to fail before a stuck task could block a group of players for too long. That number is not universal.&lt;/p&gt;

&lt;p&gt;Call-site traces found the long holds. A lock was supposed to cover an in-memory update, but the trace showed it wrapping a call to an external service. We stopped using the data lock for service calls. Data operations kept their player lock; service calls took a separate lock. A slow remote call no longer sat inside the critical section for player state. Automated tests covered the failure modes: partial acquisition, release on failure, expiry, and stale release.&lt;/p&gt;

&lt;p&gt;After this redesign, locks became easier to use, not harder. A group operation could take all its keys in one place and reason about one outcome. The lock had grown a new unit, the key set, but the caller's code got simpler.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two different problems called "lock"
&lt;/h2&gt;

&lt;p&gt;There is a temptation to use a lock for every consistency problem; it leads to bad designs. Two jobs get called "lock", with different costs.&lt;/p&gt;

&lt;p&gt;The first is mutual exclusion around a critical step. Two workers want to update the same player, send the same receipt to Apple, or apply the same purchase. The step is bounded, the key set is known, and holding a lock for its duration is acceptable.&lt;/p&gt;

&lt;p&gt;The second is workflow correctness over several steps. Charge the card, reserve inventory, write the ledger, send mail. If step three fails, you may need to undo step two. The textbook answer is a &lt;a href="https://microservices.io/patterns/data/saga.html" rel="noopener noreferrer"&gt;SAGA&lt;/a&gt;. We did not use one. An async method acquired the locks pessimistically and called the operations in order. The order was code statement order, not a business process. Remote calls looked local: the request carried the context, re-entered the lock, and serialized exceptions, so &lt;code&gt;try/catch&lt;/code&gt; worked.&lt;/p&gt;

&lt;p&gt;That is weaker than a SAGA: no durable state or resume. A hard remote failure can kill the process and expire locks, but the bus retries, so it is rare. SAGAs mean real compensation code; not worth it, and nobody would have given us the time. Preflight plus a pessimistic lock closed most of the gap.&lt;/p&gt;

&lt;p&gt;A timed reservation — TCC's Try/Confirm, or the reservation pattern — covers similar ground differently. Freeze the asset (coins, inventory) with a TTL, do the rest of the work, then confirm the freeze into a real debit. If something fails, the freeze expires on its own. This is not a SAGA: there is no explicit compensation, just expiry. It is not a lock either: it reserves value instead of serializing access. It works when the asset can sit frozen for the TTL and the confirmation is idempotent. See &lt;a href="https://karthiksukumaran.com/blog/try-confirm-cancel/" rel="noopener noreferrer"&gt;TCC&lt;/a&gt; and &lt;a href="https://appscale.blog/en/blog/microservices-pattern-two-phase-commit-alternatives-saga-tcc-outbox-reservation-2026" rel="noopener noreferrer"&gt;Reservation-Then-Commit&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;None of this makes the lock a correctness proof. The lock serializes cooperating clients — a coordination and efficiency mechanism. It does not make the write correct: a stale holder can keep running after its lease expires. Correctness belongs to the resource, which guards itself with a version or a &lt;a href="https://en.wikipedia.org/wiki/Compare-and-swap" rel="noopener noreferrer"&gt;compare-and-swap&lt;/a&gt;; &lt;a href="https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html" rel="noopener noreferrer"&gt;Kleppmann&lt;/a&gt; draws the same line. Neither replaces the other.&lt;/p&gt;

&lt;p&gt;A softer alternative is a delta transaction — a &lt;a href="https://en.wikipedia.org/wiki/Compare-and-swap" rel="noopener noreferrer"&gt;compare-and-set&lt;/a&gt; on the invariant. Instead of reading a balance, checking it, and writing a new value, you apply a guarded change:&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;accounts&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;100&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="mi"&gt;42&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If one row is affected, the change applied; if zero, the invariant failed. Deltas pass more often than version checks because they reject only when the invariant is actually violated: two concurrent debits of 100 from a balance of 250 can both pass, where a version check would reject one.&lt;/p&gt;

&lt;p&gt;A delta is not a &lt;a href="https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html" rel="noopener noreferrer"&gt;fencing token&lt;/a&gt;. It guards an invariant at write time and checks the affected row count. Fencing is about rejecting a writer that held an expired lease. They are related defenses, not the same mechanism.&lt;/p&gt;

&lt;p&gt;There is a separate cache problem. You can read and update the cache under a lock, plain or read-write, or overwrite the cached value only when a version tag is newer. We usually chose the version tag; when a tag was hard to form, we used a lock, which is simpler to write but adds a couple of round-trips. Cache convergence is separate from lock, version/fencing, and delta.&lt;/p&gt;

&lt;p&gt;In practice, we reached for neither a SAGA nor fencing tokens; delta transactions and timed freezes covered the cases we hit. They are simpler and recover on their own: a rejected delta fails cheaply, an unconfirmed reservation expires. That is not a universal replacement — multi-step flows over non-reservable resources still need compensation, and a stale writer on an unversioned resource still needs a version check or a fence — but for our cases it was enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  The lock domain: context, not thread
&lt;/h2&gt;

&lt;p&gt;A lock needs an answer to one question before it needs a store: who is the same owner?&lt;/p&gt;

&lt;p&gt;A Java &lt;code&gt;ReentrantLock&lt;/code&gt; answers with a thread. A thread that already holds the lock can enter again; another thread cannot. A JavaScript async lock answers with a task or a call chain, because there is no thread-local storage to hang ownership on. The runtime gives us promises and &lt;a href="https://nodejs.org/api/async_context.html" rel="noopener noreferrer"&gt;AsyncLocalStorage&lt;/a&gt;, not threads.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;drlock&lt;/code&gt; answers with a portable context id. The context is the logical execution: a request, an RPC call, a timer call, or a job that moves between workers. The context id can cross an HTTP boundary, a gRPC call, or a queue message. The worker id is only where the context is currently running.&lt;/p&gt;

&lt;p&gt;Another model works without a stable owner. Each acquire returns a unique lease id, and the task carries the &lt;code&gt;[key, leaseId]&lt;/code&gt; pairs it holds, including across worker boundaries. Release is by &lt;code&gt;[key, leaseId]&lt;/code&gt;; reentry means finding the matching key. I have seen this work in production, so it is not a straw man. It avoids a stable owner id, but the pair list leaks into every call and message. It is also harder to inspect: with a context id, the store shows which context holds which key during a deadlock investigation; with lease pairs, you have to ask every worker what it holds. One context id is simpler to pass, reason about, and inspect.&lt;/p&gt;

&lt;p&gt;That distinction is the whole design. The store record keeps a holder set because a context can have more than one worker inside it at a time, but the logical owner is still the context.&lt;/p&gt;

&lt;p&gt;Context ids need to be stable, unique, and one-shot. Stable means the same logical task uses the same id on every step. Unique means two unrelated executions never collide. One-shot means you do not reuse an id for a new execution after the first one finishes; a retry that is part of the same task can reuse it, a new task cannot.&lt;/p&gt;

&lt;p&gt;The library generates a UUID by default. In one deployment, we used a custom generator from a worker label, the process start time, and a counter: &lt;code&gt;&amp;lt;pod-name&amp;gt;:&amp;lt;process-start-time&amp;gt;:&amp;lt;counter&amp;gt;&lt;/code&gt;. The pod name came from a &lt;a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="noopener noreferrer"&gt;StatefulSet&lt;/a&gt;, the start time separated restarts, and the counter separated jobs in one process. It was collision-free under those assumptions; reuse pod names without a start time and it stops being safe.&lt;/p&gt;

&lt;p&gt;The failure modes are symmetric. A context that is too broad lets unrelated work join the same lock and bypass mutual exclusion. A context that is too narrow loses reentrancy and turns a nested call into a deadlock. A collision joins two unrelated executions and gives them shared locks they never asked to share.&lt;/p&gt;

&lt;p&gt;A request id can be a good context if the whole request is one logical execution. It is a bad context if the request forks into independent jobs that must not share locks. In that case, the fork needs new context ids, not a broader one.&lt;/p&gt;

&lt;p&gt;The context is not a transaction. It can touch several keys over its lifetime, and those keys are not atomic as a group unless you acquire them as a group. It is also independent of the store. You can move from Redis to PostgreSQL without changing what the context means; only the record layout and atomic operations change. That portability was the point of separating the domain from the backend.&lt;/p&gt;

&lt;p&gt;It is also not a thread. Two branches of the same context can run concurrently, which is useful for a handoff and dangerous for parallel work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Local reentry is almost free
&lt;/h2&gt;

&lt;p&gt;The cheap local reentry was not a design goal. It fell out of the local-first implementation.&lt;/p&gt;

&lt;p&gt;When a context already holds a key in this process, a nested acquire for the same key does not touch the store. It increments an in-memory count. The store is touched only at the boundaries: zero to one on the first acquire, one to zero on the final release. No new store lease, no extra watchdog, no network round-trip.&lt;/p&gt;

&lt;p&gt;The practical effect is usually one store round-trip per request or task, not one per lock call. A task can acquire a broad lock at the edge, then call business operations that lock the same key without another store round-trip; those calls do not need to know whether the caller already holds it.&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="c1"&gt;// Worker A: request handler&lt;/span&gt;
&lt;span class="n"&gt;userServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'request'&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="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="c1"&gt;// Outer acquire: one store round-trip&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'player:4172'&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="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="c1"&gt;// Inner acquire: local refcount only&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'player:4172'&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="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="c1"&gt;// critical section&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="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;pre data-lang="mermaid"&gt;&lt;code&gt;sequenceDiagram
    participant Code
    participant Manager
    participant Store

    Code-&amp;gt;&amp;gt;Manager: runLocked(key)
    Manager-&amp;gt;&amp;gt;Store: acquire (count 0 to 1)
    Store--&amp;gt;&amp;gt;Manager: held
    Code-&amp;gt;&amp;gt;Manager: nested runLocked(key)
    Note over Manager: local count 1 to 2, no store call
    Code-&amp;gt;&amp;gt;Manager: inner release
    Note over Manager: local count 2 to 1
    Code-&amp;gt;&amp;gt;Manager: outer release
    Manager-&amp;gt;&amp;gt;Store: release (count 1 to 0)
    Store--&amp;gt;&amp;gt;Manager: free&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;This is also why nested same-key acquires stopped self-deadlocking. In a non-reentrant lock, a call that takes the same key as its caller waits forever. With local reentry, the nested call increments a counter and continues.&lt;/p&gt;

&lt;p&gt;A database-first design might have made a different choice. If every reentry went to the store, the lock would be simpler to reason about across processes, but a nested call would pay a round-trip and possibly create another lease. Our local-first path avoided that cost by accident.&lt;/p&gt;

&lt;p&gt;Local reentry is local. If worker B joins the same context, B touches the store because it has no local count for that key yet; the join is not free. The cheap path is only for the worker that already holds the key.&lt;/p&gt;

&lt;p&gt;Reentry is also keyed by context, not by process. Two different contexts in the same process still contend for the same key, because they are different owners.&lt;/p&gt;

&lt;p&gt;A nested acquire can request a longer TTL, but it does not extend the existing lease. The deadline was fixed at the first acquisition. If the inner code asks for 30 seconds while the outer lease has 10 seconds left, the manager warns, because the inner code may assume a longer lifetime than the context actually has. That warning is deliberate. Silently extending the lease would turn a nested call into a renewal mechanism.&lt;/p&gt;

&lt;p&gt;Cheap reentry is the reason fine-grained locking became practical. We could put a lock around the operation that needed it, including inside business-logic functions, instead of pushing the lock up to the caller or dropping it for speed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cross-process reentrancy: sequential steps, not parallel branches
&lt;/h2&gt;

&lt;p&gt;The context id was stable and unique. Generating it was never the hard part. The hard part was what happened when two workers entered the same context at the same time.&lt;/p&gt;

&lt;p&gt;Worker A acquires &lt;code&gt;player:4172&lt;/code&gt; under context &lt;code&gt;job:881&lt;/code&gt;. Worker B joins the same context and acquires the same key. The store adds B to the holder set; B does not block. Both workers believe they hold the lock, and both are right: the context holds it. The lock does not serialize them; they may run concurrently.&lt;/p&gt;

&lt;p&gt;That is the deliberate sharp edge. Reentrancy is for sequential steps inside one task, not parallel branches. Worker A does step one, hands the context to B, and B does step two; the join makes that handoff possible without a second ownership model. If two branches of the same context run at once, they join instead of contending, and the mutual exclusion the lock was supposed to provide is gone for that pair.&lt;/p&gt;

&lt;p&gt;The same property can break ordering: a payment meant to run before releasing inventory can run after it, because the lock is an identity, not a barrier.&lt;/p&gt;

&lt;p&gt;Many tasks can run in parallel; each stays sequential. The rule is about the shape inside one context, not the number of contexts.&lt;/p&gt;

&lt;p&gt;To pass the context, put the id in the HTTP header, the gRPC metadata, or the queue message, and call &lt;code&gt;runInContext(id, fn)&lt;/code&gt; on the other side. On the receiving side, the id is not enough; the worker must enter it before acquiring any lock. Calling &lt;code&gt;runLocked&lt;/code&gt; outside a context creates a fresh context, which will contend instead of joining.&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="c1"&gt;// Worker A: start the logical task&lt;/span&gt;
&lt;span class="n"&gt;userServer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:start'&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="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="n"&gt;contextId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`market:${id}`&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;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runInContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contextId&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="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'player:4172'&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="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="c1"&gt;// first step&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="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Worker B: service event joins the same context&lt;/span&gt;
&lt;span class="n"&gt;serviceBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'market:continue'&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;contextId&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runInContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contextId&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="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;locks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'player:4172'&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="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="c1"&gt;// both workers may run concurrently; sequence the handoff yourself&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="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;Independent work should get its own context and contend for the key, or be routed through a queue so the ordering is explicit; joining unrelated work gives it shared locks and shared reentrant state it never asked for.&lt;/p&gt;

&lt;p&gt;A join also does not extend the TTL. The deadline belongs to the logical task and is fixed at the first acquisition; otherwise a task could hop between workers and extend itself indefinitely. If the task needs more time, choose a larger TTL before the first acquire.&lt;/p&gt;

&lt;p&gt;A join also does not prevent duplicate execution. Worker A can hand off to B and keep running, and both can do the same step. The lock will not stop them, because they are the same context. The handoff has to be sequenced in application code: use a request/response call and wait for B before A continues, or make A stop, or make the step idempotent, or let a queue enforce the order.&lt;/p&gt;

&lt;p&gt;That is the trade-off I would make again. Cross-process reentrancy made the task code linear. It made sequence depend on the handoff. Name the edge and keep it sequential.&lt;/p&gt;

&lt;p&gt;Applicability is the ironic part. This does not make the lock useless for a monolith: one worker owns a player, and nested calls re-enter the same key without another store round-trip. The established tools cover this: &lt;a href="https://github.com/redisson/redisson" rel="noopener noreferrer"&gt;Redisson&lt;/a&gt;'s &lt;code&gt;RLock&lt;/code&gt; and &lt;a href="https://curator.apache.org/docs/recipes-shared-reentrant-lock/" rel="noopener noreferrer"&gt;Curator&lt;/a&gt;'s &lt;code&gt;InterProcessMutex&lt;/code&gt; (both Java) are mature reentrant distributed locks, though their reentrancy is tied to a thread or process. On Node the field is thinner: &lt;a href="https://github.com/mike-marcacci/node-redlock" rel="noopener noreferrer"&gt;&lt;code&gt;node-redlock&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://www.npmjs.com/package/redis-semaphore" rel="noopener noreferrer"&gt;&lt;code&gt;redis-semaphore&lt;/code&gt;&lt;/a&gt; are usable distributed mutexes but not reentrant; &lt;a href="https://github.com/rogierschouten/async-lock" rel="noopener noreferrer"&gt;&lt;code&gt;async-lock&lt;/code&gt;&lt;/a&gt; is reentrant only inside one process; &lt;a href="https://github.com/smilecc/node-redisson" rel="noopener noreferrer"&gt;&lt;code&gt;node-redisson&lt;/code&gt;&lt;/a&gt; adds a watchdog but does not make reentrancy the abstraction. Even for a monolith, this is not a solved choice on Node.&lt;/p&gt;

&lt;p&gt;What became less useful was cross-worker reentrancy. That part fits microservices well: a task fans out across services, and the context carries ownership through the chain. But we were moving toward a scalable monolith. After distributed locks arrived, most data operations ran locally on the worker where the task started; other workers only got notifications that player data had changed. That removed most cross-worker locking. It is still needed for tricky flows that cannot be pinned to one worker, but it is no longer the default: the cross-worker part fits the architecture we moved away from better than the one we moved toward.&lt;/p&gt;

&lt;p&gt;The next part starts with the lease itself: why the TTL is fixed, what the watchdog can and cannot stop, and how multi-key acquisition avoids hold-and-wait without pretending to be fair.&lt;/p&gt;

&lt;h2&gt;
  
  
  See also
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Martin Kleppmann, &lt;em&gt;How to do distributed locking&lt;/em&gt; — efficiency vs correctness locks, fencing tokens, the Redlock critique. &lt;a href="https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html" rel="noopener noreferrer"&gt;https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Redis, &lt;em&gt;Distributed Locks with Redis&lt;/em&gt; — the Redlock algorithm. &lt;a href="https://redis.io/docs/latest/develop/use/patterns/distributed-locks/" rel="noopener noreferrer"&gt;https://redis.io/docs/latest/develop/use/patterns/distributed-locks/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Salvatore Sanfilippo (antirez), &lt;em&gt;Is Redlock safe?&lt;/em&gt; — the other side of the fencing argument. &lt;a href="https://antirez.com/news/101" rel="noopener noreferrer"&gt;https://antirez.com/news/101&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Apache Curator, &lt;em&gt;Shared Reentrant Lock&lt;/em&gt; — a ZooKeeper reentrant lock. &lt;a href="https://curator.apache.org/docs/recipes-shared-reentrant-lock/" rel="noopener noreferrer"&gt;https://curator.apache.org/docs/recipes-shared-reentrant-lock/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Redisson, &lt;em&gt;Locks and synchronizers&lt;/em&gt; — reentrant, fair, MultiLock, watchdog. &lt;a href="https://redisson.pro/docs/data-and-services/locks-and-synchronizers/" rel="noopener noreferrer"&gt;https://redisson.pro/docs/data-and-services/locks-and-synchronizers/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Alibaba Cloud, &lt;em&gt;Implementation Principles and Best Practices of Distributed Lock&lt;/em&gt; — a practical survey. &lt;a href="https://www.alibabacloud.com/blog/implementation-principles-and-best-practices-of-distributed-lock_600811" rel="noopener noreferrer"&gt;https://www.alibabacloud.com/blog/implementation-principles-and-best-practices-of-distributed-lock_600811&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Microsoft, &lt;em&gt;etcd3 Mutex&lt;/em&gt; — a queued mutex with a fencing token and lease keepalive. &lt;a href="https://github.com/microsoft/etcd3/blob/main/src/mutex.ts" rel="noopener noreferrer"&gt;https://github.com/microsoft/etcd3/blob/main/src/mutex.ts&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;C. Gray and D. Cheriton, &lt;em&gt;Leases: An Efficient Fault-Tolerant Mechanism for Distributed File Cache Consistency&lt;/em&gt; (1989). &lt;a href="https://doi.org/10.1145/74850.74870" rel="noopener noreferrer"&gt;https://doi.org/10.1145/74850.74870&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Mike Burrows, &lt;em&gt;The Chubby lock service for loosely-coupled distributed systems&lt;/em&gt;. &lt;a href="https://research.google/pubs/pub27897/" rel="noopener noreferrer"&gt;https://research.google/pubs/pub27897/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Michael Naeem, &lt;em&gt;Coordinating Work with Advisory Locks&lt;/em&gt; — PostgreSQL advisory-lock patterns. &lt;a href="https://blog.michaelsam94.com/postgres-advisory-locks-patterns/" rel="noopener noreferrer"&gt;https://blog.michaelsam94.com/postgres-advisory-locks-patterns/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;microservices.io, &lt;em&gt;Saga&lt;/em&gt;. &lt;a href="https://microservices.io/patterns/data/saga.html" rel="noopener noreferrer"&gt;https://microservices.io/patterns/data/saga.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Karthik Sukumaran, &lt;em&gt;Try-Confirm/Cancel (TCC): Reserve First, Commit Later&lt;/em&gt;. &lt;a href="https://karthiksukumaran.com/blog/try-confirm-cancel/" rel="noopener noreferrer"&gt;https://karthiksukumaran.com/blog/try-confirm-cancel/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;AppScale, &lt;em&gt;2PC Alternatives: Saga vs TCC vs Outbox vs Reservation&lt;/em&gt;. &lt;a href="https://appscale.blog/en/blog/microservices-pattern-two-phase-commit-alternatives-saga-tcc-outbox-reservation-2026" rel="noopener noreferrer"&gt;https://appscale.blog/en/blog/microservices-pattern-two-phase-commit-alternatives-saga-tcc-outbox-reservation-2026&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Node.js, &lt;em&gt;Async context tracking&lt;/em&gt; (AsyncLocalStorage). &lt;a href="https://nodejs.org/api/async_context.html" rel="noopener noreferrer"&gt;https://nodejs.org/api/async_context.html&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>node</category>
      <category>typescript</category>
      <category>backend</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
