Is it a correct case of usage Redlock
See original GitHub issueI’ve just read about the redlock algorithm and can’t figure out is it a correct case of usage. Let’s say I have a few workers and an observable which spies on some database. I use timeout to randomly pick a worker:
const redlock = new Redlock([...], { retryCount: 0 });
...
sub.on('message', (channel, message) => setTimeout(async () => {
try {
var lock = await redlock.lock('db1' /* use parsed message in the real app */, 1000);
} catch (e) {
console.log(e);
}
if (lock) {
const watcher = new DatabaseObservable('db1').subscribe(
message => {
console.log(m); // something has changed in the database
},
err => {
lock.unlock(); // the connection to the database was interrupted
},
() => {
lock.unlock(); // the connection to the database was closed normally
}
);
try {
while (1) {
await sleep(800); // hmm... the lock here could become dead
lock = await lock.extend(1000);
}
} catch (e) { ... }
}
}, Math.random()));
sub.subscribe('watch')
When I need to create a new database observable I do redis.publish('watch', 'db1')
. For each database there can be only one observable at the same time. The lock unlocks once the connection to the database is interrupted or closed.
- Does Redlock fit my case?
- What is the right way here to extend locks?
- Connections to the databases could be interrupted. So I want to recover them. Can I set an interval which repeats
Redis.publish('watch', 'db1')
unless the key is locked? Something like:
// in the server process around a websocket connection
setInterval(() => {
if ( /* unlocked */) {
redis.publish('watch', 'db1')
}
}, 1000);
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Redlock | Redis
Redlock process provides good guarantees and no single point of failure, so you can be highly confident that single locks will be doled...
Read more >Is Redlock safe? - <antirez>
Martin's analysis of the algorithm concludes that Redlock is not safe. ... While it's hard to think at an use case, note that...
Read more >Is Redlock Safe? Reply to Redlock Analysis - Hacker News
In this case the lock is not superfluous and you have safety. Regarding CAS, I said that your argument (in support of redlock)...
Read more >Redlock — the silver bullet - Medium
Redlock use cases. The following is a partial list of Redlock use cases in Pipedrive application backend over a period of time.
Read more >How to do distributed locking - Martin Kleppmann
For example, a good use case is maintaining request counters per IP ... On the other hand, the Redlock algorithm, with its 5...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
2 - Setting the TTL is case-dependent. The main effects you need to consider are:
a) deadlock recovery time: the longer the TTL, the longer the resource stays locked if your app crashes before releasing the lock, or if a network issue makes unlocking impossible b) exclusivity window: the sorter the TTL, the greater the risk that a network issue makes extending impossible, in which case you should stop your task if possible
Generally:
Your current settings seem completely sane without knowing too much about what you’re doing.
3 - This is correct, and something I do frequently for a user-supplied task; however, I’d suggest also reading through #14, as I wouldn’t do anything destructive assuming that a lock is held by another process (vs. we simply can’t get a lock right now). I think I should probably add something about this to the readme.
Just out of curiosity, are working on coordination for therondb? It’s a really cool looking project!
Great to hear, and no problem! I’m going to be following Theron, it’s a really cool project.