<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sankalpp on Medium]]></title>
        <description><![CDATA[Stories by Sankalpp on Medium]]></description>
        <link>https://medium.com/@sankalpkombey125?source=rss-67de75e5f53b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nLaNOI3bSBkmOTlfZo077A.png</url>
            <title>Stories by Sankalpp on Medium</title>
            <link>https://medium.com/@sankalpkombey125?source=rss-67de75e5f53b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 04:09:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sankalpkombey125/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Distributed Locks are Leases: What They Can and Can’t Promise]]></title>
            <link>https://medium.com/@sankalpkombey125/distributed-locks-are-leases-what-they-can-and-cant-promise-159750a0a88d?source=rss-67de75e5f53b------2</link>
            <guid isPermaLink="false">https://medium.com/p/159750a0a88d</guid>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <dc:creator><![CDATA[Sankalpp]]></dc:creator>
            <pubDate>Sun, 25 Oct 2026 10:14:54 GMT</pubDate>
            <atom:updated>2026-10-25T17:24:26.240Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>Notes from building Latch: a small Redis-backed distributed lock</em></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zWaH3MSvwYpHcvWF--yi8w.png" /></figure><p>A friend of mine, who’s not particularly technical, saw my LinkedIn post where I was larping about my project and went:</p><p>“yoo, this looks sick. But what does it actually do?”</p><p>Fair question.</p><p>I’d built something involving Redis, lock acquisition, expiration times, and concurrent processes. There was a lot going on under the hood.</p><p>But explaining it without throwing a bunch of technical jargon at someone? That’s a skill of it’s own.</p><p>So, here’s my attempt.</p><h3><strong>What the hell is distributed locking, anyway?</strong></h3><p>Imagine you’re building an application that runs on multiple servers. Each server can run multiple processes, and each process can handle jobs independently.</p><p>Now imagine two processes trying to update the same invoice at the same time.</p><p>Both read the invoice’s current state. Both make changes. One writes its update, and then the other overwrites it. Depending on the operation, you might lose data, process the invoice twice, or end up with an inconsistent result.</p><p>The problem is that <strong>these processes don’t automatically know what the others are doing</strong>.</p><p>This is where distributed locking comes in.</p><p>A distributed lock gives multiple processes a way to coordinate access to a shared resource, even when those processes are running on different servers.</p><p>But how do they coordinate if they’re running independently?</p><p>You need a shared system that they can all communicate with.</p><p>That’s where Redis shines.</p><p>Redis acts as a central coordination point. Before a process performs a particular operation, it asks Redis to acquire a lock associated with that resource. Redis allows only one process to acquire that lock at a time. Other processes attempting to acquire the same lock must wait until it becomes available.</p><p>For example, if three processes need to update invoice: 42, they all attempt to acquire the same lock key. One succeeds, perform its work, and releases the lock. The others can then compete to acquire it.</p><p>This sounds straightforward: one process acquires the lock, does its work, and releases it.</p><p>But distributed systems introduce a problem that isn’t immediately obvious.</p><p>What happens if a process acquires the lock, pauses for several seconds, and its lock expires? Another process can acquire the same lock and begin working. But the original process might eventually resume and continue its operation, unaware that its ownership has expired.</p><p><strong>Redis knows the lock has expired. The original process might not.</strong></p><p>And that raises a more important question: does acquiring a distributed lock actually guarantee that only one process can affect a resource at a time?</p><p>To answer that, we need to distinguish between two goals: <strong>Efficiency and Correctness</strong>.</p><p>Imagine three workers generating the same report. If two workers do the same work simultaneously, you’ve wasted computing resources.</p><p>Now imagine those workers processing payments. If two workers process the same payment, you could charge someone twice.</p><p>In the first case, the system did unnecessary work. That’s an efficiency problem. In the second, it produced the wrong outcome. That’s a correctness problem.</p><p>A distributed lock can help with both, but the consequences of failure are very different.</p><p><strong>Preventing two processes from acquiring the same lock doesn’t necessarily prevent them from modifying the same resource.</strong> To understand why, let’s look at how Redis locks work.</p><h3><strong>How Redis Locks work</strong></h3><p>When a worker needs access to a resource, it asks Redis to acquire a lock:</p><pre>SET invoice:42 &lt;unique-token&gt; NX PX 5000</pre><p>Redis creates the key only if it doesn’t already exist (NX) and sets it to expire after five seconds (PX). If another worker already holds the lock, the operation fails.</p><p>The unique token identifies the owner. When releasing or renewing the lock, the worker must verify that the token still matches. Otherwise, an old worker could accidentally delete or extend a lock acquired by someone else.</p><p>This handles ownership safely, but it introduces another question: what happens when the lock expires while the worker is still running?</p><h3><strong>The problem with leases</strong></h3><p>A lock with a TTL is effectively a lease: permission to hold a lock for a limited period. Workers typically renew it periodically to maintain ownership.</p><p>But imagine Worker A acquires a lock and then possess a GC pause long enough for its lease to expire. Worker B acquires the now-available lock and starts working.</p><p>When Worker A resumes, it might continue executing its old operation, unaware that it has lost ownership.</p><p>Redis has behaved correctly. It expired A’s lock and allowed B to acquire it. But <strong>expiring a lock doesn’t stop the previous holder from executing code or writing to a resource.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ivysOG8wGht7_aJgQvkLfg.png" /></figure><p>I encountered this limitation while building Latch, my Redis-backed distributed locking library for Node.js.</p><p>Latch renews locks through a watchdog and reports ownership loss using an AbortSignal and a LockLostError. But cancellation is cooperative: application code must respond to the signal, and a blocked Node.js event loop can prevent the watchdog from detecting the loss in time.</p><p>I tested this by blocking the event loop beyond the lock’s TTL. Worker A’s lock expired, Worker B acquired it and wrote to the resource, and then Worker A resumed and wrote too.</p><p>Both writes landed.</p><p>That exposed an important gap: detecting ownership loss is useful, but it doesn’t guarantee that a stale worker cannot modify shared state.</p><p>To protect the resource itself, we need another mechanism: <strong>fencing tokens</strong>.</p><h3><strong>Fencing tokens: protecting against stale workers</strong></h3><p>We know that a worker can continue running even after its lock expires. We can’t always prevent that, but we can prevent its outdated operations from being accepted.</p><p>By using fencing tokens.</p><p>Every successful lock acquition receives an increasing number called a fencing token.</p><p>For example, Worker A acquires a lock with token 41. Its lease expires, and Worker B acquires the same lock with token 42.</p><p>Worker B writes the database first. The database records token 42. When Worker A eventually tries to write with token 41, the database rejects it because the token is older.</p><p>Worker A can still execute its code, but its stale write won’t be accepted.</p><p>One important catch: <strong>the resource itself must check the token</strong>. Simply generating fencing tokens doesn’t protect anything if the database or service ignores them.</p><h3><strong>How Latch handles this</strong></h3><p>Latch issues fencing tokens atomically during lock acquisition. This means successful acquisitions receive tokens in acquisition order.</p><p>However, Latch currently generates these tokens without enforcing them at the resource being protected. The application must validate them before accepting writes. The counter’s monotonicity also depends on Redis retaining its state across eviction and failover scenarios.</p><p>This is an important distinction: generating a token is one part of the solution; making the resource reject stale operations is the other.</p><p>So fencing is an important part of Latch’s design, but the complete solution requires the consumer to validate tokens correctly and atomically.</p><p>This raises another question: if distributed locks have these limitations, can we solve the problem by using multiple Redis servers instead of one?</p><h3>Does Redlock solve the problem?</h3><p>A single Redis instance can fail, potentially affecting lock availability. Redis introduced Redlock as an approch to acquiring locks across multiple independent Redis instances.</p><p>In its commonly described configuration, Redlock uses five instances and requires a majority to grant the lock within a limited validity period.</p><p>This helps address certain Redis failure scenarios, but it doesn’t eliminate the stale-worker problem.</p><p>A process can still pause, lose its lock, and resume after another process has acquired it. Redlock doesn’t automatically prevent that old process from writing to a database.</p><p>As Martin Kleppman discusses in <a href="https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html"><em>How to do Distributed Locking</em></a>, a lock alone isn’t enough when correctness depends on preventing stale operations.</p><p>Redlock and fencing tokens address different concerns. Redlock coordinates lock acquisition across multiple Redis instances; fencing tokens allow a resource to reject outdated operations when enforced correctly.</p><p>So, what should we use when correctness really matters?</p><p>There isn’t one solution for every situation. The right approach depends on what you’re protecting and which failures you need to tolerate.</p><p>If your data lives in a database, go with <strong>database transactions and constraints</strong>. They let the database enforce important rules directly, rather than relying entirely on an external lock.</p><p>For distributed coordination, systems like <strong>ZooKeeper</strong> provide primitives designed to manage ownership and coordination across processes.</p><p>The key is to choose a mechanism based on the guarantee you need, rather than assuming that acquiring a lock solves every concurrency problem.</p><h3><strong>Final thoughts</strong></h3><p>Building Latch started as an implementation problem. Acquire a lock, renew it, release it. Simple enough.</p><p>Then I started testing what happens when things go wrong.</p><p>A process pauses. A lease expires. Another worker takes over. The first process wakes up and carries on as if nothing happened.</p><p>The lock did its job. The system still failed.</p><p>A system can behave exactly as designed and still fall short of what you expect from it.</p><p>That’s the most interesting part about distributed systems. Understanding what a system can’t guarantee matters just as much as understanding what it can.</p><p>Latch is a relatively small piece of work in distributed systems. I’ve probably made it sound more grandiose than it actually is lol.</p><p>Anyways, here’s the repo: <a href="https://github.com/Sankalpkombey/latch"><em>click here</em></a></p><p>Still kinda new here. If you learned something new, consider a follow. more soon.</p><p>Until next time, keep tinkering!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=159750a0a88d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>