⚙️ What is a Redis Lock?
Definition:
A Redis lock is a mechanism to synchronize access to shared resources in a distributed system using Redis.
Goal:
- Prevent race conditions when multiple processes try to update the same resource
1️⃣ Basic Redis Lock
How it works:
-
Use the
SET key value NX PX <ttl>command:NX→ set only if key does not exist (prevents overwriting)PX→ set expiration time (time-to-live) to avoid deadlocks
-
Acquire the lock:
SET lock:order:123 "uuid-xyz" NX PX 5000- Returns
OKif lock acquired - Returns
nilif lock is already held
- Returns
-
Release the lock:
- Only the owner of the lock can release it to prevent accidental unlock:
if redis.call("get",KEYS[1]) == ARGV[1] then return redis.call("del",KEYS[1]) else return 0 end
Pros:
- Simple, fast
- Works for single Redis instance
Cons:
- Not safe in Redis cluster / multiple nodes
- Can fail if Redis crashes or network partition occurs
2️⃣ Redlock (Distributed Redis Lock)
Problem:
- A single Redis instance lock can fail in distributed environments → risk of split-brain / multiple owners
Solution: Redlock (proposed by Salvatore Sanfilippo, Redis creator)
How it works:
- Multiple Redis instances (e.g., 5 nodes)
- Client tries to acquire lock on majority of nodes (e.g., 3 out of 5)
- Lock has a short TTL to prevent deadlocks
- If lock is acquired on majority → success
- Unlock → release lock from all instances
Algorithm Steps (simplified):
- Get current time
- Try to acquire lock on N Redis nodes sequentially
- Calculate elapsed time, ensure majority acquired and TTL not expired
- If yes → lock acquired, else retry
Pros:
- Safer in distributed environments
- Reduces risk of multiple clients acquiring the lock
Cons:
- More complex
- Network latency can affect reliability
3️⃣ ⚡ Use Cases
| Use Case | Why Redis Lock / Redlock |
|---|---|
| Distributed job scheduling | Ensure only one worker processes a job |
| Updating shared resources | Prevent race conditions in counters, inventory, etc. |
| Leader election | Choose one master node in distributed system |
| Rate limiting | Ensure only one service instance updates counters |
4️⃣ 🔧 Key Best Practices
- Always use TTL → avoid deadlocks if client crashes
- Use unique values per client → ensure correct unlock
- Prefer Redlock in distributed Redis → more fault-tolerant
- Avoid long-running critical sections → lock should be short-lived
5️⃣ 🧩 Analogy
- Single Redis lock: “One key to a safe” → if the keyholder crashes, safe is stuck
- Redlock: “Multiple locks on multiple safes” → need majority keys to open → safer for distributed environment
✅ Key Takeaways
- Redis lock = simple locking mechanism on a single Redis instance
- Redlock = distributed locking across multiple Redis nodes for safety
- Always use TTL and unique identifiers
- Best for distributed coordination, job scheduling, and preventing race conditions