Live data from Hacker News

Why Nodejs serves a file with 80x more CPU usage than Nginx?

news.ycombinator.com

1–10 of 15 posts

Why Nodejs serves a file with 80x more CPU usage than Nginx?

#1
Take the same code that sits on nodejs.org home page. Serve a static file that is 1.8Mb. And do the same with Nginx, and watch the difference.

Code : http://pastie.org/3730760 Screencast : http://screencast.com/t/Or44Xie11Fnp

Please share if you know anything that'd prevent this from happening, so we don't need to deploy nginx servers and complicate our lives.

ps1. this test is done with node 0.6.12. out of curiosity, i downgraded to 0.4.12 just to check if it's a regression, on the contrary, it was worse. same file used 25% twice.

ps2. this post is not a nodejs hate - we use nodejs, and we love it, except this glitch which actually delayed our launch (made us really sad), and seemed quite serious to me. i've never read, heard, seen this mentioned before so we could prepare ourselves better.

Re: Why Nodejs serves a file with 80x more CPU usage than Nginx?

#6
post #5

"complicate" your lives? nginx is an amazing piece of technology and it's extremely simple. It's built for serving static files (and doing other things) better than anything else that exists. Use the right tool for the job.

i wasn't saying nginx is bad (nginx is awesome). i just wouldn't want to deploy and configure nginx with my app, if i can avoid it. in fact i got an awesome suggestion over at stackoverflow, that put the usage down to 0.3% http://d.pr/dXVR

Re: Why Nodejs serves a file with 80x more CPU usage than Nginx?

#8

You could put nginx in front of nodejs and let it handle the static files, it's far better at it. This is worth reading as well: http://engineering.linkedin.com/nodejs/blazing-fast-nodejs-1...

Besides that, node can listen on UNIX domain socket, while nginx can proxy the connection over UDS, therefore people can avoid the TCP stack. That should also shave off some latency. It may throw some 502 errors if is not configured properly, but with sysctl tricks or Ryan's patch for nginx (https://github.com/ry/nginx-ey-balancer) it is doable.

Re: Why Nodejs serves a file with 80x more CPU usage than Nginx?

#9

Node isn't really built for serving static files. I'd say the easiest solution would be to stick varnish in front of your node application.

That's true if you also want a caching proxy in front of your stack, but if you just want to serve static files, nginx in front of node.js is a perfectly acceptable solution.

Re: Why Nodejs serves a file with 80x more CPU usage than Nginx?

#10
post #6
post #5

"complicate" your lives? nginx is an amazing piece of technology and it's extremely simple. It's built for serving static files (and doing other things) better than anything else that exists. Use the right tool for the job.

i wasn't saying nginx is bad (nginx is awesome). i just wouldn't want to deploy and configure nginx with my app, if i can avoid it. in fact i got an awesome suggestion over at stackoverflow, that put the usage down to 0.3% http://d.pr/dXVR

If it's an app you in tend to package up, or is portable, that might be an acceptable solution, but in the long term, using Nginx or Varnish (or even Apache) is likely the better answer, unless it is your objective to build a static file server in Node.

Otherwise, even if you can speed it up, and/or minimize its memory usage, you're still wasting cycles and memory in your app that needn't be spent on it. To a point, this is perfectly fine, but at some point, you're (hopefully) going to want to scale, and I promise that this is the first thing you'll want to look at.

Post reply on HN