TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

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

12 pointsby devrimabout 13 years ago
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.<p>Code : http://pastie.org/3730760 Screencast : http://screencast.com/t/Or44Xie11Fnp<p>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.<p>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.<p>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.

6 comments

IsaacSchlueterabout 13 years ago
First of all, writing bug reports to Hacker News is usually a bit like complaining about the government at a bus stop. It's fun, and you'll get lots of agreement from crazy people, and maybe start a little riot, but writing your representative is a better way to effect change.<p>We have two mailing lists, and an issue tracker on github. If someone hadn't emailed this to me, I probably would not have seen it.<p>There are three problems I see with your test.<p>1) It serves the file twice, since you don't filter out the /favicon.ico url<p>2) you're converting the file contents to a string, which needs to be converted <i>back</i> into raw bytes to send.<p>3) your gist is in JavaScript, but I see that you're actually running CoffeeScript. I'm not sure what kind of wrapper CoffeeScript is adding to the equation.<p>I ran the same test serving the ChangeLog file from node's source folder with node v0.6.15 (to be released tomorrow) <a href="https://raw.github.com/joyent/node/v0.6/ChangeLog" rel="nofollow">https://raw.github.com/joyent/node/v0.6/ChangeLog</a><p>Here's the source code of the server.js: <a href="https://gist.github.com/2338851" rel="nofollow">https://gist.github.com/2338851</a><p>It did use more CPU than nginx, but not 100x as much. Requesting the /buffer url made it spike up to about 0.2% cpu usage. Requesting the / url made it spike up to about 0.3%. Even when I disabled the /favicon.ico check, its behavior is nowhere near what you're seeing.<p>When I ran it with coffee script, it actually uses this JavaScript: <a href="https://gist.github.com/2338874" rel="nofollow">https://gist.github.com/2338874</a>. So, there's an extra Function.call in there, but otherwise, it's pretty much identical. My coffeescript version is here: <a href="https://gist.github.com/2338879" rel="nofollow">https://gist.github.com/2338879</a><p>So, something is odd with your situation, and from your test, it's not clear what. I'd love to get more details.<p>On the more general note, clearly we in the node world are perfectly fine with using nginx to serve static assets. Qv. the nodejs.org home page. But node's performance here should be within an order of magnitude, or something is broken. This is not expected or normal performance that you're seeing. We run the "serve a big string over http" benchmark quite often, and so this was very surprising to me.
评论 #3814473 未加载
benologistabout 13 years ago
You could put nginx in front of nodejs and let it handle the static files, it's far better at it.<p>This is worth reading as well: <a href="http://engineering.linkedin.com/nodejs/blazing-fast-nodejs-10-performance-tips-linkedin-mobile" rel="nofollow">http://engineering.linkedin.com/nodejs/blazing-fast-nodejs-1...</a>
评论 #3806455 未加载
评论 #3806415 未加载
staunchabout 13 years ago
"complicate" your lives? nginx is an amazing piece of technology and it's extremely simple. It's <i>built</i> for serving static files (and doing other things) better than anything else that exists. Use the right tool for the job.
评论 #3806277 未加载
jameswyseabout 13 years ago
Node isn't really built for serving static files.<p>I'd say the easiest solution would be to stick varnish in front of your node application.
评论 #3806633 未加载
aristusabout 13 years ago
<a href="http://www.kernel.org/doc/man-pages/online/pages/man2/sendfile.2.html" rel="nofollow">http://www.kernel.org/doc/man-pages/online/pages/man2/sendfi...</a>
thinknliveabout 13 years ago
// response : http response stream // filename : the static file we want to serve<p>response.writeHead(200 /<i>, ...set http header info (mime, length etc)...</i>/ ); stream = fs.createReadStream( filename, { flags: 'r', start: start, end: end }); stream.pipe(response);