1

People says that single-threaded languages like Javascript can not got that issue. However, nodejs provides cluster to fork multiple workers. Is this gonna cause race condition problem?

And also, I have an example that causes my confusion sometimes:

ALLSESSIONS = {};
//session id saved in cookie
ALLSESSIONS[sessionid] = 'somevalue';

var SESSION = ALLSESSIONS[sessionid];
... do stuff for sometimes
console.log(SESSION);

I'm afraid that when another request reaches node while the current request is still running, it may overwrite the SESSION variable with its own session id, then the result of current request is undetermined.

Is this gonna happen in node?

user3828771
  • 1,603
  • 3
  • 14
  • 14
  • [This question](http://stackoverflow.com/questions/21438207/can-node-js-code-result-in-race-conditions) answers some of your questions about race conditions. Yes, you could have two separate requests that conflict, if you write the program that way, but for the most part your requests shouldn't write to global variables. That isn't an issue introduced by `cluster` though, that can also occur when writing asynchronous code incorrectly. If you have any real code you'd like us to debug, show us that, because your example isn't very descriptive. – Adam Aug 06 '14 at 15:40

1 Answers1

1

When another request reaches node while the current request is still running, it will not overwrite the session variable with its own.

Cluster allows you to have multiple workers, and these workers will all be separate from each other. This means that if your ALLSESSIONS variable is scoped to a single worker, then it will actually only contain session information for the given worker. You would need your ALLSESSIONS variable to be scoped at the application (pre-fork) level in order to share it across workers.

However, keeping session information in memory is probably not a good idea to begin with! If your worker or application threads crash, then you've lost your session data. It may be better to use a persistent store of some sort.

In general, race conditions are likely to occur if you have a globally scoped variable being written to and read from multiple request threads.

Hamilton Lucas
  • 419
  • 2
  • 5
  • From your point, if I store session in files, `SESSION` variable is still global scope, because all workers can access any files in system. Am I thinking right? – user3828771 Aug 06 '14 at 16:26
  • Yes - if you were to store the sessions in files, the `SESSION` variable would be essentially global scope. All workers would be accessing those files - and it could cause race conditions if you were not careful to 'block' on the I/O of those files. – Hamilton Lucas Aug 06 '14 at 16:45
  • Thanks for your response. BTW, what is your recommendation with shared data (session for example) in node? – user3828771 Aug 06 '14 at 16:51
  • I've used both mongodb and a postgres session store with pretty good luck. Installing mongodb on a server running node is usually pretty easy and the community has provided a few packages to make it pretty straightforward to use with the express framework. See [connect-mongo](https://github.com/kcbanner/connect-mongo). – Hamilton Lucas Aug 06 '14 at 17:00