Redlock
Distributed lock implementation over one or more Redis instances based on the Redlock algorithm.
Official Documentation ↗redlock v5.x / Redis 6+ / Node 18 LTS. Check the official docs for the latest API surface.Installation
npm install redlockpnpm add redlockyarn add redlockMethods
No methods match this filter.
Overview
Redlock coordinates mutual exclusion across processes using Redis keys with expiry. It is used to prevent duplicate handling of a job or to serialize access to a shared resource.
Core Methods
acquire takes resource keys and a ttl, returning a lock you must release. using wraps a function so acquisition and release happen around it, and retry settings control how long acquisition is attempted.
Usage Notes
Always set a ttl, release locks in a finally block, and extend long critical sections. Keep critical sections short and idempotent so a lost lock does not cause double effects.
Common Pitfalls
A lock that expires mid-work allows overlap, so work must tolerate it. Without fencing tokens, a paused process can act after losing its lock. Treat Redlock as a coordination aid, not an absolute safety guarantee.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/mike-marcacci/node-redlock@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:06.745Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Redis does its thing and fails over to the slave which hasn’t yet synced your lock. (https://cdn.jsdelivr.net/gh/mike-marcacci/node-redlock@latest/README.md)
- This is why redlock allows you to specify multiple independent nodes/clusters: by requiring consensus between them, we can safely take out or fail-over a minority of nodes without invalidating active locks. (https://cdn.jsdelivr.net/gh/mike-marcacci/node-redlock@latest/README.md)
- For example, if you are on the smaller side of a network partition you will fail to acquire a lock, but you don’t know if the lock exists on the other side; all you know is that you can’t guarantee exclusivity on yours. (https://cdn.jsdelivr.net/gh/mike-marcacci/node-redlock@latest/README.md)
FAQ
- Why use multiple Redis instances?
- The algorithm considers a lock held only if a majority of independent instances grant it, which reduces the chance that a single failed node causes two holders. A single instance is a weaker guarantee.
- What if work outlasts the TTL?
- The lock can expire while work continues, letting another holder proceed. Extend the lock with an extension mechanism or keep critical sections shorter than the TTL.
- Is Redlock a strong guarantee?
- Lock-based mutual exclusion over Redis has known caveats around clock and pause assumptions. For strict safety, use fencing tokens or a consensus store rather than relying on the lock alone.
Report a problem with this API docOpens a pre-filled issue on GitHub.