API Reference

Redlock

Distributed lock implementation over one or more Redis instances based on the Redlock algorithm.

Official Documentation ↗
CompatibilityThis documentation covers version redlock v5.x / Redis 6+ / Node 18 LTS. Check the official docs for the latest API surface.

Installation

npmnpm install redlock
pnpmpnpm add redlock
yarnyarn add redlock

Methods

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.

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.