S
S
Stepan Zubashev2015-12-14 08:50:20
JavaScript
Stepan Zubashev, 2015-12-14 08:50:20

NodeJS & Lock & Race?

How can I organize something like this cheaply? Taking into account the fact that locking should work within the server (several instance's are raised).

await this.lock(token /* str */);
// ... some code
await this.unlock(token);

Those. I need an easy-to-use mechanism a la Serializable-transactions within a single server.
Initially I wanted to use memcached for this, but after a cursory search I came across information that it was not intended for such things. Then I looked towards "Serializable" transactions in PostrgeSQL, but I want a faster and simpler solution. Thoughts revolve around native file locks.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill, 2015-12-14
@faiwer

We usually use Redis for such purposes. They have an algorithm in the documentation .
The simplest option without expire:

redis.setnx(token, 'lock', function (err, acquired) {
  if (err) { /*...*/ }
  if (acquired) {
    // ... some code
    redis.del(token, function (err) { /*...*/ }); // unlock
  }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question