Fast web server? "nginx"

Last week me and my co-worker came upon a task that needed a very fast web server to process huge amount of requests. The process was not complicated and the database backend was very simple. The first thing came to our mind was that apache is too big for this purpose. So we looked for a faster and smaller web server. Spending some time on google got us to a Russian web server called "nginx".
I started to play with it. The I wrote a simple script and then from another machine I called that script in a loop for 10000 times. The result was weird. There are several articles out there talking about nginx and its benefits for fast processes. But the result of my tests were very different than what I had expected.
I want to spend a little more time on this and then I will write detail information about my tests and the result.



And here comes the unexpected result:
I was working on a Centos 5 machine with Apache+MySQL+PHP, all from default installations of Centos. Of course I needed to add serveral modules and extensions to make it work for Druapl. On the same machine I could install nginx plus another version of PHP, compiled from source code with fastcgi support. I will try to write a "HowTo" article about the steps I took for this installation. Also I added another PHP to use as "Apache+PHP+FastCGI".
The scripts I wanted to test were pretty easy. A write request and a read request. I added memcache support to all three systems and I started to do two different tests on each combination, one with direct work to MySQL database and the other one with saving data to memcache and the retrieving from it.
Then I ran two concurrent scripts which they put 10000 requests to each combination above. Here is the result:

10000 write + 10000 read requests
nginx+memcache 97 sec
apache+mysql 85 sec
apache+FastCGI+MySQL 80 sec
apache+memcache 120 sec
apache+FastCGI+memcache 92 sec

I was surprised to see the result. I was expecting something really fast with nginx. I decided to do some other tests with complicated MySQL tables. Then the result made sense. With 400000 records in three tables and running a join query, MySQL takes something like 420 sec to retrieve the result, and this happens every time you run the script. But if you save the result into memcache and then retrieve it, it will take less than 5 sec which was amazing. If you bring nginx into the game and run the scripts again, you will get approximately half of the numbers you have got with apache, even with FastCGI mode enabled.

After these tests, I believe I can safely say that the internal cache process of MySQL works great with small tables. (less than 5000 records). If you are working in this kind of situation, forget about nginx and don't bother the complicated process of installing it and php and so on, go old school with the classic apache+MySQL. You don't even need memcache in your game. But if you are working with huge amount of data, then nginx+memcache is definitely the answer.

Note: If you want to use php memached object in nginx, you need to compile php with memcahe enabled. After php5, you need to use option --enable-memcache[=DIR] and use the memcache source directory in DIR clause.
If you want to have both apache and nginx in your server, don't forget to use --prefix option when compiling php for nginx and install it to a location other than the defalt location)