3

Before, when I was using Django, I needed a queue to do heavy stuff behind the scenes. (could take 1 second or more, hits databases, requests, etc).

If I'm using Node.js, do I still need a queue? Can I do all the heavy stuff in node.js , asynchronously?

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

2 Answers2

2

You probably need a queue if you're doing heavy lifting.

The thing about the nature of node.js is that there's one event queue, and (for the most part) there's only one thing going on at a time.

The good thing about node.js is that a lot of library code is asynchronous - for example, your database driver will make a request to the database, then let something else happen in the node event loop. So you can process new incoming requests while waiting for the database to get back to you.

The bad thing about node.js is that your code also has to worry about blocking the event queue. Because a large operation (be that a computation, or a synchronous database call) means that your node.js application isn't serving any incoming requests.

So, if you're doing something "big", you'll want to dump that out to an event queue so you don't block the main loop.

Beyond things like calculations, I would even say you want to queue up things where you don't know how long something may take. For example, one of my recent projects was a state based workflow app in Ruby on Rails. When the state objects changed state any number of functions would be called to push the workflow along. In node.js I would queue these state transitions up, because I don't know many functions will be added to the state transition callback as the application develops and business adds new functionality to the app.

I cover a fair bit of this blocking stuff in a white-paper I wrote on node.js. It covers other stuff too, but the async part has a lot of links from the community and best practices as I understand this stuff

RyanWilcox
  • 13,890
  • 1
  • 36
  • 60
0

Since you do the heavy lifting outside your server app, it doesn't block the server.

You may need a queue later on if you have more and more concurrent connections, so you can distribute the work between processes. Kue is a good Node.js based queue.

alessioalex
  • 62,577
  • 16
  • 155
  • 122