1

I have updated this question to be more specific based on feedback below

Is it possible in a MEAN stack application to have a race condition if two users both make an HTTP request that does the following: read a record, make some changes to the record based on business logic, and then write back to the DB? Or is it impossible due to the single threaded JS execution environment? Based on some comments below it sounds like it is a problem. Is there some way to implement transactions? Or maybe I need to create a job queue or use an actor-based approach. I'm trying to understand how this type of problem is typically solved with the MEAN stack.

ademartini
  • 1,311
  • 1
  • 14
  • 24
  • I'm actually more asking this question in terms of Node JS. I gave a specific example, where two users make an HTTP request that would read, modify, and write to a document. – ademartini Apr 13 '15 at 14:21
  • @ademartini The problem is, node.js never writes to a document, that's done by a 3rd party api or service (such as a database or the filesystem.) The same thing applies. While node.js won't send two writes at the same time, it will send a 2nd write before the first is done if you aren't careful. – Kevin B Apr 13 '15 at 14:41
  • I think this is a valid question, why downvotes? It makes sense to ask this given Node's mantra that it's single threaded. – N.B. Apr 13 '15 at 15:06
  • OK, when you say "changes the record based on business logic", is it important for that logic to have the *absolutely latest* value, or would the value from 1s ago be acceptable? and when you write back to the DB, how important is it that the value stored there is the result of the *latest* operation and not the one that happened a split-second before? it would help if you can describe what you're really doing. – Touffy Apr 14 '15 at 06:23

1 Answers1

3

OK, I'll answer the narrower part of your question. Node.js has indeed a single thread for the JavaScript engine, but it uses multiple threads for handling asynchronous actions such as… database (and other) I/O. It could very well ask two of its threads connect to the database to edit the same document at the same time.

Look here: Does node.js use threads/thread pool internally?

So no, Node doesn't save you from race conditions on async operations. MongoDB does, to some extent (for which you really should read the documentation because it's a complex topic).

Community
  • 1
  • 1
Touffy
  • 6,309
  • 22
  • 28
  • *"It could very well ask two of its threads"* I didn't think node.js itself would spawn threads, rather, it would make a request to the 3rd party process (a database) which could possibly use multiple threads. – Kevin B Apr 13 '15 at 14:35
  • No, node.js itself uses threads, just not for evaluating JavaScript. You can configure how many helper threads it will spawn, so I suppose you could set it to just one to avoid concurrency. – Touffy Apr 13 '15 at 14:39
  • @Touffy This link actually doesn't answer my question entirely. I'm really more concerned with the situation where I want to read, make some decisions about what to change and then write some changes to the record. So there are multiple operations happening. If multiple users can do this in parallel it sounds like it is a problem. Is there some way to implement transactions? Or maybe I need to create a job queue or use an actor-based approach. I'm trying to understand how this type of problem is typically solved with the MEAN stack. – ademartini Apr 13 '15 at 15:37
  • @Touffy I have modified the question to be more specific, as per your recommendation. – ademartini Apr 13 '15 at 16:55