Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js: How to lock/synchronize a block of code?

Tags:

node.js

Let's take the simple code snippet:

var express = require('express');
var app = express();
var counter = 0;

app.get('/', function (req, res) {
   // LOCK
   counter++;
   // UNLOCK
   res.send('hello world')
})

Let's say that app.get(...) is called a huge number of times, and as you can understand I don't want the line counter++ to be executed concurrently by the two different threads.

Therefore, I want to lock this line that only one thread can have access to this line. My question is how to do it in node.js?

I know there is a lock package: https://www.npmjs.com/package/locks, but I'm wondering whether there is a "native" way of doing it without an external library.

like image 282
CrazySynthax Avatar asked Jun 22 '18 05:06

CrazySynthax


3 Answers

I don't want the line counter++ to be executed concurrently by the two different threads

That cannot happen in node.js with just regular Javascript coding.

node.js is single threaded and event-driven, so there's only ever one piece of Javascript code running at a time that can access that variable. You do not have to worry about the typical pre-emptive concurrency issues of multi-threaded systems.

That said, you can still have concurrency issues in node.js if you are using asynchronous code because the node.js asynchronous model returns control back to the system to process the next event and the asynchronous callback gets called on some future event. But, the concurrency issues are non-pre-emptive so you fully control when they can occur.

If you show us your actual code in your app.get() route handler, then we can advise more specifically about whether you do or don't have a concurrency issue there or not. And, if you do, we can advise on how to best deal with that.

Threads in the thread pool are all native code that runs behind the scenes. They only trigger actual Javascript to run by queuing events through the event queue. So, because all Javascript that runs is serialized through the event queue, you only get one piece of Javascript ever running at a time. The basic scheme of the event queue is that the interpreter runs a piece of Javascript until it returns control back to the system. At that point, the interpreter looks in the event queue and if there's an event waiting, it pulls that event out and calls the callback associated with that event. Meanwhile, if there is native code running in the background, when it completes, it adds an event to the event queue. That event is not processed until the current Javascript returns control back to the system and it can then grab the next event out of the event queue. So, it's this event-queue that serializes running only one piece of Javascript at a time.

Edit: Nodejs does now have WorkerThreads which enable separate threads of Javascript, but each thread has its own heap and its own variables so a variable from one thread cannot be directly accessed from another thread. You can configure shared memory that both WorkerThreads can access, but that isn't straight variables, but blocks of memory and if you want to use shared memory, then you do indeed need to code your own synchronization methods to make sure you are atomically accessing the variable. The code you show in your question is not using any of this so the access to the counter variable is already atomic and cannot be simultaneously accessed by any other Javascript, even if you are using WorkerThreads.

like image 182
jfriend00 Avatar answered Oct 17 '22 00:10

jfriend00


If you block thread none of the requests will execute all will be in the queue.

It 's not good practice to block the thread in Node.js

var express = require('express');
var app = express();
var counter = 0;

const getPromise = () => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve('Done')
        }, 100);
    });
}

app.get('/', async (req, res) => {
    const localCounter = counter++; 
    // Use local counter for rest of operation so value won't vary

    // LOCK: Use promise/callback 
    await getPromise(); // Not locked but waiting for getPromise to finish

    console.log(localCounter); // Same value before lock

    res.send('hello world')
})
like image 2
Rahul Sharma Avatar answered Oct 17 '22 00:10

Rahul Sharma


Node.js is single-threaded, which means that any single process running your app will not have data races like you anticipate. In fact, a quick inspection of the locks library shows that they use a boolean flag and a system of Array objects to determine whether something is locked or not.

You should only really worry about this if you plan on sharing data with multiple processes. In that case, you could use Alan's lockfile approach from this stackoverflow thread here.

like image 1
Andres Salgado Avatar answered Oct 17 '22 00:10

Andres Salgado