Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx's speed, and how to replicate it

Tags:

nginx

erlang

I'm interested in this from more than an academic standpoint rather than a practical standpoint; I don't plan on creating a production webserver to compete with nginx. What I'm wondering is how exactly nginx is so fast. The top google response for this is this thread, but it merely links to a cryptic slideshow and a general covering of different io strategies. All other results seem to simply describe how fast nginx is, rather then the reason.

I tried building a simple erlang server to try to compete with nginx, but to no avail; nginx won out. All my server does is spawn a new process for each request, uses that process to read the file to a socket, then closes the file and kills the thread. It's not complicated, but given erlang's lightweight processes and underlying aio structure I thought it would compete, but nginx still wins out by a consistent 300 ms average under a heavy stress test.

What is nginx doing that my simple server isn't? My first thought would be keeping files in main memory instead of tossing them between requests, but the filesystem cache does this already so I didn't think it would make that great of difference. Am I wrong? Or is there something else that I'm missing?

like image 686
Mediocre Gopher Avatar asked Nov 14 '11 22:11

Mediocre Gopher


1 Answers

Turns out my little test server was quite competitive with nginx once I told it to read files in binary mode instead of list mode.

I think a lot of the discussion in the rest of this thread may be confusing for someone unfamiliar with erlang and erlang server design. I didn't want to delete the thread since there is good information about nginx in it (and I can't, it has answers already), but I encourage anyone looking into making an erlang based server to do some research and write lots of tests, and not go only what you've read here.

like image 111
Mediocre Gopher Avatar answered Sep 28 '22 11:09

Mediocre Gopher