Distributed Locks with Redis Redlock
When single-instance Redis locks fail, Redlock trade-offs, and why we still prefer database advisory locks for money.
We needed exclusive access to reconcile batch jobs across pods. Mutex in memory does not work with horizontal scale.
Single-Instance Lock
var acquired = await db.StringSetAsync(
lockKey, lockToken, expiry: TimeSpan.FromSeconds(30),
when: When.NotExists);
Works until Redis fails over or clock skew causes double acquisition.
Redlock (Multi-Master)
Acquire on N independent Redis nodes. Quorum required. More resilient, more complex, debated in the community (Martin Kleppmann’s critique is worth reading).
Our Choice
| Use case | Lock mechanism |
|---|---|
| Cache rebuild | Single Redis SET NX |
| Batch reconciliation | PostgreSQL pg_advisory_lock |
| Payment idempotency | DB unique constraint |
Money paths use the database. Redis locks are for best-effort exclusivity where duplicate work is annoying but not catastrophic.
redis , locking , distributed-systems · Redis , .NET