Skip to the content.

Redis Redlock is a distributed locking algorithm proposed by Redis creator Salvatore Sanfilippo (antirez).

It’s used to implement distributed locks safely in environments where you have multiple Redis instances (to avoid single point of failure).


πŸ”Ή Problem Redlock Solves

Imagine you want a lock (like a mutex) across multiple servers so that only one process at a time can access a shared resource (e.g., a file, payment transaction, inventory update).

πŸ‘‰ That’s why Redlock exists – to provide a safer, fault-tolerant distributed lock.


πŸ”Ή How Redlock Works (Step by Step)

Suppose you have N Redis masters (recommended: 5 for safety).

  1. Get timestamps – Client notes the current time in milliseconds.
  2. Try acquiring the lock in all Redis nodes

    • Use the command:

      SET resource_name unique_value NX PX 30000
      
      • NX β†’ set only if not exists (ensures lock).
      • PX 30000 β†’ lock expires automatically in 30s (avoid deadlocks).
      • unique_value β†’ random value (UUID) so only lock owner can release.
  3. Check how many locks were acquired

    • Client must succeed in at least (N/2 + 1) nodes (majority quorum).
  4. Calculate validity time

    • Actual lock validity = lock expiration time βˆ’ time already spent acquiring.
    • If validity > 0 and quorum achieved β†’ lock is acquired.
  5. If lock not acquired

    • Release any acquired locks and retry after a random delay (to avoid collisions).

πŸ”Ή Releasing the Lock

Only the client that set the lock can release it. It uses a Lua script (to check unique value before deleting):

if redis.call("get", KEYS[1]) == ARGV[1] then
   return redis.call("del", KEYS[1])
else
   return 0
end

This ensures no one else can delete your lock.


πŸ”Ή Why Redlock Is Safer


πŸ”Ή When to Use Redlock

βœ… Best for distributed systems where:


πŸ‘‰ In short: Redis Redlock is a distributed locking algorithm built on top of Redis to safely implement locks across multiple Redis nodes, ensuring fault-tolerance and avoiding race conditions.


Let’s compare Redis Redlock with Zookeeper locks (via Curator recipes) β€” both are widely used in distributed systems for coordination and locking.


πŸ”Ή 1. Core Idea


πŸ”Ή 2. How Lock Works

βœ… Redis Redlock

βœ… Zookeeper Lock (Curator)


πŸ”Ή 3. Consistency Model


πŸ”Ή 4. Failure Handling


πŸ”Ή 5. Performance


πŸ”Ή 6. Typical Use Cases

βœ… Redis Redlock

βœ… Zookeeper Lock (Curator)


πŸ”Ή 7. Summary Table

Feature Redis Redlock πŸš€ Zookeeper Lock πŸ”’
Consistency Eventual Strong (linearizable)
Fault Tolerance Good (multi-node) Very strong
Performance Very fast (in-memory) Slower (disk + quorum)
Auto-expiry Yes (via TTL) No (ephemeral node release)
Ease of Setup Simple (Redis already in infra) Complex (needs Zookeeper ensemble)
Use Case Fit High-performance but β€œbest effort” locking Strict correctness required

πŸ‘‰ Rule of Thumb:


πŸ”Ή Etcd Locks (Overview)

Etcd is a distributed key-value store (backed by the Raft consensus algorithm) widely used in Kubernetes for storing cluster state. It also provides distributed coordination primitives, including locks, through its gRPC API.

Etcd locks are typically implemented with the Etcd concurrency package:


πŸ”Ή Comparison: Redis Redlock vs Zookeeper vs Etcd

Feature / Aspect Redis Redlock πŸš€ Zookeeper πŸ”’ Etcd πŸ”‘
Consistency Model Eventual (AP-ish) Strong (CP) Strong (CP via Raft)
Lock Mechanism Keys with TTL & quorum Ephemeral sequential znodes Lease-based locks with sessions
Auto-expiry Yes (TTL on keys) Yes (ephemeral node disappears) Yes (lease expiry)
Failure Handling Lock may expire early while client still works Lock released only if session dies Lock released only if lease expires
Performance Very fast (in-memory) Medium (disk + quorum writes) Medium (quorum via Raft, slower than Redis)
Ease of Setup Easiest (Redis is common) More complex (needs ensemble) Medium (etcd cluster, but Kubernetes already uses it)
Typical Use Cases Lightweight job scheduling, cache invalidation Leader election, strict coordination Kubernetes leader election, cluster metadata, distributed consensus
Correctness Guarantees β€œBest effort” (may allow duplicates in rare cases) Very strong Very strong
CAP Theorem AP-ish (availability focus) CP (consistency focus) CP (consistency focus)

πŸ”Ή Real-World Usage

βœ… Redis Redlock

βœ… Zookeeper

βœ… Etcd


πŸ”Ή Rule of Thumb (When to Choose)


πŸ‘‰ So you can think of it like this: