UNPKG

8.73 kBMarkdownView Raw
1[![Continuous Integration](https://github.com/mike-marcacci/node-redlock/workflows/Continuous%20Integration/badge.svg)](https://github.com/mike-marcacci/node-redlock/actions/workflows/ci.yml?query=branch%3Amain++)
2[![Current Version](https://badgen.net/npm/v/redlock)](https://npm.im/redlock)
3[![Supported Node.js Versions](https://badgen.net/npm/node/redlock)](https://npm.im/redlock)
4
5# Redlock
6
7This is a node.js implementation of the [redlock](http://redis.io/topics/distlock) algorithm for distributed redis locks. It provides strong guarantees in both single-redis and multi-redis environments, and provides fault tolerance through use of multiple independent redis instances or clusters.
8
9- [Installation](#installation)
10- [Usage](#usage)
11- [Error Handling](#error-handling)
12- [API](#api)
13- [Guidance](#guidance)
14
15## Installation
16
17```bash
18npm install --save redlock
19```
20
21## Configuration
22
23Redlock is designed to use [ioredis](https://github.com/luin/ioredis) to keep its client connections and handle the cluster protocols.
24
25A redlock object is instantiated with an array of at least one redis client and an optional `options` object. Properties of the Redlock object should NOT be changed after it is first used, as doing so could have unintended consequences for live locks.
26
27```ts
28import Client from "ioredis";
29import Redlock from "./redlock";
30
31const redisA = new Client({ host: "a.redis.example.com" });
32const redisB = new Client({ host: "b.redis.example.com" });
33const redisC = new Client({ host: "c.redis.example.com" });
34
35const redlock = new Redlock(
36 // You should have one client for each independent redis node
37 // or cluster.
38 [redisA, redisB, redisC],
39 {
40 // The expected clock drift; for more details see:
41 // http://redis.io/topics/distlock
42 driftFactor: 0.01, // multiplied by lock ttl to determine drift time
43
44 // The max number of times Redlock will attempt to lock a resource
45 // before erroring.
46 retryCount: 10,
47
48 // the time in ms between attempts
49 retryDelay: 200, // time in ms
50
51 // the max time in ms randomly added to retries
52 // to improve performance under high contention
53 // see https://www.awsarchitectureblog.com/2015/03/backoff.html
54 retryJitter: 200, // time in ms
55
56 // The minimum remaining time on a lock before an extension is automatically
57 // attempted with the `using` API.
58 automaticExtensionThreshold: 500, // time in ms
59 }
60);
61```
62
63## Usage
64
65The `using` method wraps and executes a routine in the context of an auto-extending lock, returning a promise of the routine's value. In the case that auto-extension fails, an AbortSignal will be updated to indicate that abortion of the routine is in order, and to pass along the encountered error.
66
67The first parameter is an array of resources to lock; the second is the requested lock duration in milliseconds, which MUST NOT contain values after the decimal.
68
69```ts
70await redlock.using([senderId, recipientId], 5000, async (signal) => {
71 // Do something...
72 await something();
73
74 // Make sure any attempted lock extension has not failed.
75 if (signal.aborted) {
76 throw signal.error;
77 }
78
79 // Do something else...
80 await somethingElse();
81});
82```
83
84Alternatively, locks can be acquired and released directly:
85
86```ts
87// Acquire a lock.
88let lock = await redlock.acquire(["a"], 5000);
89try {
90 // Do something...
91 await something();
92
93 // Extend the lock.
94 lock = await lock.extend(5000);
95
96 // Do something else...
97 await somethingElse();
98} finally {
99 // Release the lock.
100 await lock.release();
101}
102```
103
104### Use in CommonJS Projects
105
106Beginning in version 5, this package is published primarily as an ECMAScript module. While this is universally accepted as the format of the future, there remain some interoperability quirks when used in CommonJS node applications. For major version 5, this package **also** distributes a copy transpiled to CommonJS. Please ensure that your project either uses either the ECMAScript or CommonJS version **but NOT both**.
107
108The `Redlock` class is published as the "default" export, and can be imported with:
109
110```ts
111const { default: Redlock } = require("redlock");
112```
113
114In version 6, this package will stop distributing the CommonJS version.
115
116## Error Handling
117
118Because redlock is designed for high availability, it does not care if a minority of redis instances/clusters fail at an operation.
119
120However, it can be helpful to monitor and log such cases. Redlock emits an "error" event whenever it encounters an error, even if the error is ignored in its normal operation.
121
122```ts
123redlock.on("error", (error) => {
124 // Ignore cases where a resource is explicitly marked as locked on a client.
125 if (error instanceof ResourceLockedError) {
126 return;
127 }
128
129 // Log all other errors.
130 console.error(error);
131});
132```
133
134Additionally, a per-attempt and per-client stats (including errors) are made available on the `attempt` propert of both `Lock` and `ExecutionError` classes.
135
136## API
137
138Please view the (very concise) source code or TypeScript definitions for a detailed breakdown of the API.
139
140## Guidance
141
142### Contributing
143
144Please see [`CONTRIBUTING.md`](./CONTRIBUTING.md) for information on developing, running, and testing this library.
145
146### High-Availability Recommendations
147
148- Use at least 3 independent servers or clusters
149- Use an odd number of independent redis **_servers_** for most installations
150- Use an odd number of independent redis **_clusters_** for massive installations
151- When possible, distribute redis nodes across different physical machines
152
153### Using Cluster/Sentinel
154
155**_Please make sure to use a client with built-in cluster support, such as [ioredis](https://github.com/luin/ioredis)._**
156
157It is completely possible to use a _single_ redis cluster or sentinal configuration by passing one preconfigured client to redlock. While you do gain high availability and vastly increased throughput under this scheme, the failure modes are a bit different, and it becomes theoretically possible that a lock is acquired twice:
158
159Assume you are using eventually-consistent redis replication, and you acquire a lock for a resource. Immediately after acquiring your lock, the redis master for that shard crashes. Redis does its thing and fails over to the slave which hasn't yet synced your lock. If another process attempts to acquire a lock for the same resource, it will succeed!
160
161This 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.
162
163To learn more about the the algorithm, check out the [redis distlock page](http://redis.io/topics/distlock).
164
165Also note that when acquiring a lock on multiple resources, commands are executed in a single call to redis. Redis clusters require that all keys exist in a command belong to the same node. **If you are using a redis cluster or clusters and need to lock multiple resources together you MUST use [redis hash tags](https://redis.io/topics/cluster-spec#keys-hash-tags) (ie. use `ignored{considered}ignored{ignored}` notation in resource strings) to ensure that all keys resolve to the same node.** Chosing what data to include must be done thoughtfully, because representing the same conceptual resource in more than one way defeats the purpose of acquiring a lock. Accordingly, it's generally wise to use a single very generic prefix to ensure that ALL lock keys resolve to the same node, such as `{redlock}my_resource`. This is the most straightforward strategy and may be appropriate when the cluster has additional purposes. However, when locks will always naturally share a common attribute (for example, an organization/tenant ID), this may be used for better key distribution and cluster utilization. You can also acheive ideal utilization by completely omiting a hash tag if you do _not_ need to lock multiple resources at the same time.
166
167### How do I check if something is locked?
168
169The purpose of redlock is to provide exclusivity guarantees on a resource over a duration of time, and is not designed to report the ownership status of a resource. 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. This is further complicated by retry behavior, and even moreso when acquiring a lock on more than one resource.
170
171That said, for many tasks it's sufficient to attempt a lock with `retryCount=0`, and treat a failure as the resource being "locked" or (more correctly) "unavailable".
172
173Note that with `retryCount=-1` there will be unlimited retries until the lock is aquired.