1 | [![npm version](https://badge.fury.io/js/redlock.svg)](https://www.npmjs.com/package/redlock)
|
2 | [![Build Status](https://travis-ci.org/mike-marcacci/node-redlock.svg)](https://travis-ci.org/mike-marcacci/node-redlock)
|
3 | [![Coverage Status](https://coveralls.io/repos/mike-marcacci/node-redlock/badge.svg)](https://coveralls.io/r/mike-marcacci/node-redlock)
|
4 |
|
5 | Redlock
|
6 | =======
|
7 | This 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 | ###High-Availability Recommendations
|
10 | - Use at least 3 independent servers or clusters
|
11 | - Use an odd number of independent redis ***servers*** for most installations
|
12 | - Use an odd number of independent redis ***clusters*** for massive installations
|
13 | - When possible, distribute redis nodes across different physical machines
|
14 |
|
15 |
|
16 | Installation
|
17 | ------------
|
18 | ```bash
|
19 | npm install --save redlock
|
20 | ```
|
21 |
|
22 | Configuration
|
23 | -------------
|
24 | Redlock can use [node redis](https://github.com/mranney/node_redis), [ioredis](https://github.com/luin/ioredis) or any other compatible redis library to keep its client connections.
|
25 |
|
26 | A 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 firstused, as doing so could have unintended consequences for live locks.
|
27 |
|
28 | ```js
|
29 | var client1 = require('redis').createClient(6379, 'redis1.example.com');
|
30 | var client2 = require('redis').createClient(6379, 'redis2.example.com');
|
31 | var client3 = require('redis').createClient(6379, 'redis3.example.com');
|
32 | var Redlock = require('redlock');
|
33 |
|
34 | var redlock = new Redlock(
|
35 | // you should have one client for each redis node
|
36 | // in your cluster
|
37 | [client1, client2, client3],
|
38 | {
|
39 | // the expected clock drift; for more details
|
40 | // see http://redis.io/topics/distlock
|
41 | driftFactor: 0.01,
|
42 |
|
43 | // the max number of times Redlock will attempt
|
44 | // to lock a resource before erroring
|
45 | retryCount: 3,
|
46 |
|
47 | // the time in ms between attempts
|
48 | retryDelay: 200
|
49 | }
|
50 | );
|
51 | ```
|
52 |
|
53 |
|
54 | Usage (promise style)
|
55 | ---------------------
|
56 |
|
57 |
|
58 | ###Locking & Unocking
|
59 |
|
60 | ```js
|
61 |
|
62 | // the string identifier for the resource you want to lock
|
63 | var resource = 'locks:account:322456';
|
64 |
|
65 | // the maximum amount of time you want the resource locked,
|
66 | // keeping in mind that you can extend the lock up until
|
67 | // the point when it expires
|
68 | var ttl = 1000;
|
69 |
|
70 | redlock.lock(resource, ttl).then(function(lock) {
|
71 |
|
72 | // ...do something here...
|
73 |
|
74 | // unlock your resource when you are done
|
75 | return lock.unlock();
|
76 | });
|
77 |
|
78 | ```
|
79 |
|
80 |
|
81 | ###Locking and Extending
|
82 |
|
83 | ```js
|
84 | redlock.lock('locks:account:322456', 1000).then(function(lock) {
|
85 |
|
86 | // ...do something here...
|
87 |
|
88 | // if you need more time, you can continue to extend
|
89 | // the lock as long as you never let it expire
|
90 | return lock.extend(1000).then(function(lock){
|
91 |
|
92 | // ...do something here...
|
93 |
|
94 | // unlock your resource when you are done
|
95 | return lock.unlock();
|
96 | });
|
97 | });
|
98 |
|
99 | ```
|
100 |
|
101 |
|
102 | Usage (disposer style)
|
103 | ----------------------
|
104 |
|
105 |
|
106 | ###Locking & Unocking
|
107 |
|
108 | ```js
|
109 | var using = require('bluebird').using;
|
110 |
|
111 | // the string identifier for the resource you want to lock
|
112 | var resource = 'locks:account:322456';
|
113 |
|
114 | // the maximum amount of time you want the resource locked,
|
115 | // keeping in mind that you can extend the lock up until
|
116 | // the point when it expires
|
117 | var ttl = 1000;
|
118 |
|
119 | using(redlock.disposer(resource, ttl), function(lock) {
|
120 |
|
121 | // ...do something here...
|
122 |
|
123 | }); // <-- unlock is automatically handled by bluebird
|
124 |
|
125 | ```
|
126 |
|
127 |
|
128 | ###Locking and Extending
|
129 |
|
130 | ```js
|
131 | using(redlock.disposer('locks:account:322456', 1000), function(lock) {
|
132 |
|
133 | // ...do something here...
|
134 |
|
135 | // if you need more time, you can continue to extend
|
136 | // the lock until it expires
|
137 | return lock.extend(1000).then(function(extended){
|
138 |
|
139 | // Note that redlock modifies the original lock,
|
140 | // so the vars `lock` and `extended` point to the
|
141 | // exact same object
|
142 |
|
143 | // ...do something here...
|
144 |
|
145 | });
|
146 | }); // <-- unlock is automatically handled by bluebird
|
147 |
|
148 | ```
|
149 |
|
150 |
|
151 | Usage (callback style)
|
152 | ----------------------
|
153 |
|
154 |
|
155 | ###Locking & Unocking
|
156 |
|
157 | ```js
|
158 |
|
159 | // the string identifier for the resource you want to lock
|
160 | var resource = 'locks:account:322456';
|
161 |
|
162 | // the maximum amount of time you want the resource locked,
|
163 | // keeping in mind that you can extend the lock up until
|
164 | // the point when it expires
|
165 | var ttl = 1000;
|
166 |
|
167 | redlock.lock(resource, ttl, function(err, lock) {
|
168 |
|
169 | // we failed to lock the resource
|
170 | if(err) {
|
171 | // ...
|
172 | }
|
173 |
|
174 | // we have the lock
|
175 | else {
|
176 |
|
177 |
|
178 | // ...do something here...
|
179 |
|
180 |
|
181 | // unlock your resource when you are done
|
182 | lock.unlock();
|
183 | }
|
184 | });
|
185 |
|
186 | ```
|
187 |
|
188 |
|
189 | ###Locking and Extending
|
190 |
|
191 | ```js
|
192 | redlock.lock('locks:account:322456', 1000, function(err, lock) {
|
193 |
|
194 | // we failed to lock the resource
|
195 | if(err) {
|
196 | // ...
|
197 | }
|
198 |
|
199 | // we have the lock
|
200 | else {
|
201 |
|
202 |
|
203 | // ...do something here...
|
204 |
|
205 |
|
206 | // if you need more time, you can continue to extend
|
207 | // the lock until it expires
|
208 | lock.extend(1000, function(err, lock){
|
209 |
|
210 | // we failed to extend the lock on the resource
|
211 | if(err) {
|
212 | // ...
|
213 | }
|
214 |
|
215 |
|
216 | // ...do something here...
|
217 |
|
218 |
|
219 | // unlock your resource when you are done
|
220 | lock.unlock();
|
221 | }
|
222 | }
|
223 | });
|
224 |
|
225 | ```
|
226 |
|
227 | API Docs
|
228 | --------
|
229 |
|
230 | ###`Redlock.lock(resource, ttl, callback)`
|
231 | - `resource (string)` resource to be locked
|
232 | - `ttl (number)` time in ms until the lock expires
|
233 | - `callback (function)` callback returning:
|
234 | - `err (Error)`
|
235 | - `lock (Lock)`
|
236 |
|
237 |
|
238 | ###`Redlock.unlock(lock, callback)`
|
239 | - `lock (Lock)` lock to be released
|
240 | - `callback (function)` callback with no returning arguments
|
241 |
|
242 |
|
243 | ###`Redlock.extend(lock, ttl, callback)`
|
244 | - `lock (Lock)` lock to be extended
|
245 | - `ttl (number)` time in ms to extend the lock's expiration
|
246 | - `callback (function)` callback returning:
|
247 | - `err (Error)`
|
248 | - `lock (Lock)`
|
249 |
|
250 |
|
251 | ###`Lock.unlock(callback)`
|
252 | - `callback (function)` callback with no returning arguments
|
253 |
|
254 |
|
255 | ###`Lock.extend(ttl, callback)`
|
256 | - `ttl (number)` time in ms to extend the lock's expiration
|
257 | - `callback (function)` callback returning:
|
258 | - `err (Error)`
|
259 | - `lock (Lock)`
|
260 |
|