1

I'm writing a Node.js app that - long story short - prioritizes different IO processes, depending on a user's activity/location on a website. So far, everything is running great - important queued items run first, while less important things run later.

The tricky part is there's tons of things happening at a time - users log in, users go to a different page, users do something that requires immediate processing, etc. For the most part I'm not worried about how Node.js manages the event queue, but in certain cases I am. I'm imagining a situation where I've got a whole bunch of stuff in my event queue (possibly because a bunch of IO batches just returned, and are waiting to be processed), and a user does an action on my site that requires an immediate IO request, and then immediate processing when that request returns.

I've never written a Node.js application that would requires me to really worry about scaling, so I'm not 100% confident in its ability to process my events in the correct order. Is this a silly thing to worry about? Will Node.js process things so incredibly fast that I will never realistically notice the blocking, or is it very possible that as I scale I will begin to see substantial delays in important things being processed?

If it is a legitimate worry, are there any interfaces within Node.js where I can manually prioritize my event queue?

Update:

@Ricardo Tomasi brought up a good point in the comments, that if my app is really that CPU intensive, it probably doesn't belong in Node.js. That made me realize my app really isn't that CPU intensive, and probably will run fine.

To further prove it to myself I did a little benchmark:

var a = new Date().getTime() + 1000; 
var b = new Date().getTime(); 
var c = 0; 
while(b < a) { 
    c++; 
    b = new Date().getTime();
} 
console.log(c + " loops in 1 second."); 

I was just under 200,000 loops, which sufficiently proved to me that I have nothing to worry about. However, I'm still interested to know if there is any way to manually manage the Node.js event queue. Does anyone know?

jwegner
  • 7,043
  • 8
  • 34
  • 56
  • 2
    If you're doing intensive CPU tasks, offload them to a different process. Events will fire in the order they are added, but that's within one event loop - any other events can block it. – Ricardo Tomasi Feb 23 '12 at 22:26
  • Nothing I'm doing is incredibly CPU intensive - I'm just doing a lot! I'm just considering the situation where I might have 1000 users ask for data at the same time.. Certainly #1000 will have some sort of delay, regardless of how CPU intensive my app is. – jwegner Feb 23 '12 at 22:43
  • What kind of test is that? Put some stress on your actual system and see where the bottlenecks are. It's likely that you'll be IO-bound rather than CPU-bound, but you'll never know until you measure. – Scott Wegner Feb 23 '12 at 23:41
  • @ScottWegner The premise behind Node.js is that it does IO in a non-blocking way. It continuously runs javascript on the main thread, and utilizes sub threads (or sub processes more like) for IO. When the IO finishes, it then calls the main thread, and drops back in the event queue to be handled. That's why Node.js is so beautiful. – jwegner Feb 24 '12 at 03:26

0 Answers0