question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it a correct case of usage Redlock

See original GitHub issue

I’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.

  1. Does Redlock fit my case?
  2. What is the right way here to extend locks?
  3. 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:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mike-marcaccicommented, May 31, 2016

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:

  • if the resource you are locking is infrequently contested (like the ID of a task), you probably want to use longer TTL (say, a minute) and not worry about extending
  • if your task keeps track of progress or can be safely paused/resumed, use a small TTL and extend frequently
  • never cut an extension closer than say ~50ms, as a brief burst of open files, a large garbage collection event, or a network event might add enough latency to miss your window

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!

0reactions
mike-marcaccicommented, Jun 8, 2016

Great to hear, and no problem! I’m going to be following Theron, it’s a really cool project.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found