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?

1
  • This question 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. Commented Aug 6, 2014 at 15:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
Thanks for your response. BTW, what is your recommendation with shared data (session for example) in node?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.