InterviewHubPRO
Section Overview
Q48

Question 48 of 60

Part 4: In-Memory Caching & Fast Data Stores (Redis) (41 - 50)

80%
Databases & System Design•Part 4: In-Memory Caching & Fast Data Stores (Redis) (41 - 50)

Distributed Locks in Redis (Redlock):

In Simple Words: Acquire lock with atomic SET key random_token NX PX 30000. Release using a Lua script verifying random_token matches before deleting to avoid unlocking someone else's expired lock.

lua
-- Safe Lua unlock script:
if redis.call("get", KEYS[1]) == ARGV[1] then
  return redis.call("del", KEYS[1])
else
  return 0
end