How does node.js solve db concurrency problem?

1,142 views
Skip to first unread message

Xi

unread,
Nov 14, 2011, 4:54:33 PM11/14/11
to nodejs
Hi:

I am new to node.js. Please forgive my ignorance.

I read it online that node.js automatically solves concurrency
problem. I was wondering if that is true, because the db calls are
still running in parallel, and we are still risking 2 threads
modifying the same data at the same time.

Is my understanding correct?

Thanks,

Mark Hahn

unread,
Nov 14, 2011, 8:23:03 PM11/14/11
to nod...@googlegroups.com
The threads are invisible to the javascript code you write for node.  So you do not have to think about threads at all.  See the node docs.


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Bradley Meck

unread,
Nov 14, 2011, 8:26:59 PM11/14/11
to nod...@googlegroups.com
If the database does not lock rows, it will be subject to concurrent edit conflicts. This is something that no external program can solve easily (token rings etc. with locks for each actor on the DB would be needed). However, when working with data in the Node process, since there is only a single thread accessing in memory data structures there is guaranteed locking for any blocking control flow (due to there being no preemption of threads on JS objects). So as long as you are working in places that do not fire off an async callback you can rest assured that your data has not changed from one point to the next.

Liam

unread,
Nov 14, 2011, 8:52:50 PM11/14/11
to nodejs
Node simplifies concurrency issues only for Javascript code, as it
only executes Javascript in the main thread.

You can indeed run multiple db calls in parallel, since Node has a
thread pool in which it executes async C++ bindings. (Not all C++
bindings are async, but those that call a db probably are.)

It is possible to make a C++ binding prevent simultaneous use of a
resource, but that's up to the binding writer.

Andi

unread,
Nov 15, 2011, 7:06:28 AM11/15/11
to nod...@googlegroups.com
It depends on your concrete case. Often, node is started as a cluster, and even in single process there can occur the necessity to add locks. Let me point to this post, where I have added a concrete example: https://groups.google.com/group/nodejs/browse_thread/thread/a549ffc345e6de48?pli=1
Another example are sessions. Getting, changing and saving a session server-side needs a way to queue this operation to prohibit data inconsistencies and loss. Requests with the same ID can happen at the same time. This problem is not existing with a single process and a function without callbacks. But as soon you have callbacks you cannot be sure.
Reply all
Reply to author
Forward
0 new messages