r/node•
corruption93

Can someone correct me if I'm wrong here (Node vs Apache for lots of requests)

My general understanding (which is probably wrong) goes like this. Please correct me if I'm wrong!

Since node uses a single thread to handle many requests, it is better suited to handle a lot of requests because a new thread is not needed for every request and thus it can yield more performance. So if I was developing a single page web app that does a lot of AJAX in the background as an example, node would likely be a better candidate as it would be more efficient at relaying information from a database to the client, instead of a crazy amount of threads starting making the CPU slower (inefficient?).

So in this case it would be bad to do a lot of CPU work because requests will be blocked until the CPU on the thread is in an idle state.

I don't understand what the advantage of not doing servers this way is. From my last paragraph, it might make sense that that statement doesn't hold true for Apache, which would mean that you can do more CPU intensive stuff, because the other threads will still be responsive enough to handle their respective connections due to the CPU context switching. Is this true? If so, then I finally get it! Woohoo!

Posted Apr 3, 2015, 11:00 AM UTC

27
echospring•

crazy amount of threads starting

Apache uses thread pools, so the threads are already there (it starts them when apache starts and can change the size of the pool on demand). Also, threads aren't necessarily a bad thing. node uses threads for it's async i/o (libuv).

Though, comparing node and apache is like comparing apples and oranges. Yeah they both can serve web applications, but they are completely different.

The good thing is that both node and apache are free software so you can try both and compare them with the types of workloads you're expecting.

5
gileze33•

Yep, you've pretty much hit the nail on head. If you're doing work that doesn't require many CPU cycles per request, node is definitely the way forward. As you mention, if you say did this in a request, you're going to block every other request from processing:

for(var i=0; i<10000000000000000000; i++) { console.log('this is silly'); }

Another thing to note about the difference between say PHP and a node app is the fact that you typically boot your entire app with every request, and then forget about everything between every request (think DB connections etc) with a scripting language - with a node app you typically maintain connections and runtime config across the lifetime of the app running.

3
corruption93OP•

Cool, thanks for confirming. I also didn't think about the fact that node stays up and PHP scripts usually are completely reset. I definitely knew this from my limited experience working with both but it never clicked.

1
laggingreflex•

BTW you can use cluster to spawn different node processes which will load balance the requests if one of the processes gets too busy to respond. http://cjihrig.com/blog/scaling-node-js-applications/

2
geon•

Your comparison doesn't really make sense. Apache is a webserver, that can optionally run scripting engines (like PHP/Perl/etc). Node is a scripting engine that can optionally run a webserver.

If all you are doing is serving static files, then Apache would probably beat anything you can build on Node. Apache has been optimized for that exact use case for decades.

If you need to execute code for each request, Apache alone can't help you at all, so comparing to it doesn't make sense. You need to compare to Apache+PHP or Apache+Perl, or whatever language you choose to use.

Then it probably comes down to what your application is doing. If you don't need any IO, you could probably use fastcgi in C behind Apache and get great performance.

It all depends on your use case.

1
corruption93OP•

I was assuming apache + any regular scripting language like Perl, PHP or Python.

Why is Apache/PHP better at serving static files than with node? With node you can have a lot of requests with one thread which uses less resources right?

2
geon•

Apache is specifically built and optimized to serve static files. Unless the file beeing served is a script, no script runtime will be invoked at all. You don't need PHP, Perl or Python to serve html and css files.

I'm not sure about how Apache is implemented, but there's no fundamental reason it couldn't use asynchronous IO just like Node internally. Both of them are implemented i C, after all.

More info here: http://stackoverflow.com/questions/6634299/node-js-slower-than-apache

1