Interview's MateSystem Design
Interview's Mate

System Design

Architecture & trade-offs

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics/Distributed Locks & Unique IDs
DISTRIBUTED SYSTEMS & ARCHITECTURE · SUBTOPIC

Distributed Locks & Fencing Tokens

Avoid deadlocks and zombie writers using leases and monotonic fencing tokens.

IntermediatePart of Distributed Locks & Unique IDs
Practice in AI HLD
Subtopics in Distributed Locks & Unique IDs (2)
OverviewLocks & FencingSnowflake & IDs

In plain words

Giving someone a temporary hotel keycard with a version number: if they take too long and the front desk issues Card #34 to the next guest, the door lock automatically rejects Card #33.

Think of it like…

Hotel keycard encoder invalidating older card serial numbers.

Loading interactive diagram
Loading interactive exercise

Theory & how it works

Efficiency locks vs. Correctness locks

If you just want to avoid sending a reminder email twice ("efficiency"), a simple Redis SET NX PX is fine. If duplicate action loses money ("correctness"), use a consensus-backed lock (etcd/ZooKeeper) with fencing tokens or database uniqueness constraints.

Key points & practical guide

Summary checklist

  • Always attach a TTL lease to distributed locks
  • Check ownership value inside a Lua script before deleting
  • Use fencing tokens when correctness matters

When to use it

  • Preventing double-runs of cron jobs, inventory holds, leader coordination

Watch out for

  • Using DEL lock unconditionally - Worker A might delete Worker B’s newly acquired lock

Quick check

Loading quiz
Parent topic
Distributed Locks & Unique IDs
Next subtopic
Snowflake IDs, UUIDv7 & Ticket Servers