What experience do you have using nginx and memcached to optimize a web site? [closed] What experience do you have using nginx and memcached to optimize a web site? [closed] nginx nginx

What experience do you have using nginx and memcached to optimize a web site? [closed]


Assuming you have a bank of application servers upstream delivery data to the users.

upstream webservices {    server 10.0.0.1:80;    server 10.0.0.2:80;    server 10.0.0.3:80;}server {    ... default nginx stuff ...    location /dynamic_content {          memcached_pass localhost:11211;          default_type   text/html;          error_page     404 502 = @dynamic_content_cache_miss;          set $memcached_key $uri;    }    location @dynamic_content_cache_miss {          proxy_pass http://webservices;    }

What the above nginx.conf snippet does is direct all traffic from http://example.com/dynamic/* DIRECTLY to memcached server. If memcache has the content your upstream servers will not see ANY traffic.

If the cache hit fails with a 404 or 502 error (not in cache or memcache cannot be reached) then nginx will pass the request to the upstream servers. Since there are three servers in the upstream definition you also get transparent load balancing proxy as well.

Now the only caveat is that you have to make sure that your backend application servers keep the data in memcache fresh. I use nginx + memcached + web.py to create simple little systems that handle thousands of requests per minute on relatively modest hardware.

The general pseudo code for the application server is like this for web.py

class some_page:     def GET(self):         output = 'Do normal page generation stuff'         web_url = web.url().encode('ASCII')         cache.set(web_url, str(output), seconds_to_cache_content)         return output

The important things to remember in the above web.py / pseudo code is that content coming from memcached via nginx cannot be changed at all. nginx is using simple strings and not unicode. If you store unicode output in memcached, you'll get at the very least weird characters at the start and end of your cached content.

I use nginx and memcached for a sports related website where we get huge pulses of traffic that only last for a few hours. I could not get by without nginx and memcached. Server load during our last big Fourth of July sports event dropped from 70% to 0.6% after implementing the above changes. I can't recommend it enough.