Redis & Caching (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
Distributed Locks in Redis: Single-Node vs Redlock.
senior LevelSystem Design
In Simple Words:
- Single-Node Lock:
SET resource_key random_token NX PX 10000(Acquires lock if Not eXists with 10s auto-expiry). Released using a Lua script that verifies the token matches before deleting. - Redlock Algorithm: For multi-node independent Redis clusters. Acquires the lock in parallel across $N$ independent masters. Lock is granted only if acquired on a majority ($\ge \frac{N}{2} + 1$) within a strict time budget.