I have denormalized some some of the data I store in MongoDB, for the sake of reducing read-times. This will need to be refreshed whenever I do a write-operation, to keep it consistent with the underlying data.
To avoid any race conditions, I want to spin this off into a background task which would be single threaded. In Java, I would simply throw Command objects onto a queue, and have a reader-thread process them one at a time. But in Node, whenever I do any kind of DB operation it will happen asynchronously, and therefore I'm left with the race condition issue.
Example of what I want to do:
Application Thread
Background Thread
I'm still getting my head around Node's concurrency model (in case you couldn't tell!). How do I achieve something like this in Node.js.
I have a similar application where I use a child_process to fork the background jobs so:
then I would send the child process the next bid in the queue:
// Application Thread
var child = cp.fork('the background thread module');
child.send(nextBid);
On the background thread listen for the message:
// Background Thread
process.on('message', function(bid) {
// Do your background work
process.send(bid);
});
Then listen for background completion in the application:
// Application Thread
child.on('message', function(lastBid) {
// Check the queue and do it again
});