---
title: "Comment by Ziwei Yao on \"System Design Drill Day 8: Distributed Locks with Redis and Fencing Tokens\" | Hashnode"
description: "Should lease acquisition and fencing-token allocation be atomic? With SET NX followed by a separate INCR, A could acquire, pause before INCR, and lose its lease"
url: "https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0/comment/6adf1e02559b843e076534ac"
---

[Discussion](https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0)

Comment [System Design Drill Day 8: Distributed Locks with Redis and Fencing Tokens](https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0)

[Oct 26](https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0/comment/6adf1e02559b843e076534ac)

Should lease acquisition and fencing-token allocation be atomic? With SET NX followed by a separate INCR, A could acquire, pause before INCR, and lose its lease. B acquires, gets token 1 and writes. A resumes, gets token 2, and its stale write passes fence\_token < :token. The stock check still prevents overselling, but does this break the stale-holder guarantee? I’d test a pause between those Redis commands.

[Anish Ansari](https://hashnode.com/@anish78)

[Oct 26](https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0/comment/6adf1e02559b843e076534ac#reply-6adf2af93dc566e46d0758de)

Good catch Ziwei, you're right that a pause between SET NX and INCR breaks the stale-holder guarantee, so the two should run together in one Lua script that takes the lock and returns the new token atomically. Injecting a pause there in a test is exactly how I'd prove the fix.

[Ziwei Yao](https://hashnode.com/@vitayao)

[Oct 27](https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0/comment/6adf1e02559b843e076534ac#reply-6ae00cc51e22047b4cea9126)

Thanks for checking that. I'd have the regression test assert that the stale holder's write is rejected after the newer holder has written, not just that stock stays non-negative. Those are separate guarantees, and the stock constraint could otherwise make the test pass while fencing is still broken.

[Anish Ansari](https://hashnode.com/@anish78)

[Oct 27](https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0/comment/6adf1e02559b843e076534ac#reply-6ae027a05217df75cd4e3be3)

Good point Ziwei, asserting that the stale holder's write is rejected after the newer holder has written is the real fencing test. Stock staying non-negative can hide a broken fence, so I'll add that check.

[Ziwei Yao](https://hashnode.com/@vitayao)

[Oct 27](https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0/comment/6adf1e02559b843e076534ac#reply-6ae060781b2db2d2f9307032)

A latch in the test could make the pause reproducible instead of depending on sleep timing. That would be a nice companion to the revised example.

[Anish Ansari](https://hashnode.com/@anish78)

[Oct 27](https://hashnode.com/posts/system-design-drill-day-8-distributed-locks-with-redis-and-fencing-tokens/6adec4d52d0de51d89be8ab0/comment/6adf1e02559b843e076534ac#reply-6ae07c7dd9806620b275a560)

Nice idea Ziwei, a latch makes the stale holder's pause deterministic instead of hoping a sleep lands in the right spot. I'll use that in the revised test.
