redislock is a Go package that acquires, refreshes, and releases distributed locks through Redis. It documents fencing tokens and application-owned lock renewal patterns.
Overview
redislock offers a simplified distributed locking implementation backed by Redis for Go programs. It is intended for coordinating exclusive work across processes or services that share a Redis instance.
Optional fencing tokens can be requested via a fence key so each lock acquisition returns a monotonically increasing value you can attach to writes at the protected resource. The library mints tokens, but enforcing stale-token rejection remains the resource’s responsibility. On Redis Cluster, fence and lock keys must share a hash slot.
The package does not ship a background watchdog to extend TTLs automatically. Long-held locks expect your application to run a ticker that calls Refresh on an interval roughly one third of the TTL, handle Refresh errors such as loss of ownership, and call Release when work completes.
Documentation points to examples for typical usage and discusses trade-offs of token monotonicity across Redis failovers.
Key features
- Redis-backed distributed locks
- Optional fencing tokens
- Lock refresh for long work
- Explicit release by caller
- Documented ErrNotObtained handling
- Cluster hash-tag guidance
- No bundled watchdog goroutine
- GoDoc reference material
Use cases
- Single-writer job coordination
- Cron overlap prevention
- Leader election style exclusivity
- Fenced writes after lock loss
- Cross-service critical sections