Online Book Reader

Home Category

Facebook Cookbook - Jay Goldman [40]

By Root 610 0
code creation. Plus, if you build memcached in from the beginning and get a tidal wave of popularity, you won’t have to scramble to track down all of the SQL queries in your code and rewrite them (unless you’re smart and build them all into a Database Acess/Abstraction Layer). A number of memcached libraries are available in about 12 different languages from http://www.danga.com/memcached/apis.bml.

Instructions on how to implement memcached are outside the scope of this book, so if you’re interested in more information I highly recommend Cal Henderson’s excellent Building Scalable Web Sites (O’Reilly). Cal was the architect of http://flickr.com, so he knows a thing or four hundred about building really scalable sites (and he’s a great guy to have a beer with!). He covers everything from providing content to an international audience all the way through buying hardware on a budget and coordinating a team of developers.

Advanced Caching with Nginx and memcached


—Ilya Grigorik (see his bio in Contributors)

Problem


I’ve built a popular destination (or an API server), and now I need to handle several thousand requests a second, but I don’t have the time to rearchitect my code, or worse, rewrite in a faster language.

Solution


Memcached, the darling of every web developer, is capable of turning almost any application into a speed demon. No matter which language you’re working in, your application server is usually the slowest part of the chain: no application server is faster than any web server, even if yours is written in C.

Nginx, a very popular HTTP and reverse-proxy server, by default comes prepackaged with a memcached module, which allows us to bypass the application server and serve cached responses from memcached directly. With minimal code changes, we’ve implemented this technique for AideRSS API servers, and immediately saw our request throughput improve by 400%—from 800 req/s to 3,700 req/s!

Discussion


Most popular open source web servers can be configured to serve cached data quickly and directly from one or more memcached server instances, rather than from your filesystem or an application server. Apache (see http://code.google.com/p/modmemcachecache/) and Lighttpd (http://trac.lighttpd.net/trac/wiki/Docs) require additional modules to enable this functionality, whereas Nginx (http://wiki.codemongers.com/Main) comes with native support and offers the most flexible implementation. A relative newcomer to the field, it is quickly gaining in popularity, and is currently the fourth most popular web server (http://survey.netcraft.com/Reports/200806/).

To get started, download the latest copy of the Nginx code base, and run configure, install—this takes less than a minute and has no additional dependencies. Also, make sure to browse the wiki and look at sample configuration files. If you’re coming from Apache or Lighttpd, you’ll be pleased to see that the configuration syntax is virtually identical.

Nginx comes with a built-in memcached module, which allows it to query the cache directly prior to forwarding the request to the application server. If the cache does not contain the item we are looking for, the memcached module will raise a 404 Not Found error, which we catch and redirect for processing on our application server:

upstream appserver { server 127.0.0.1:9010; }

server {

location / {

set $memcached_key $uri;

memcached_pass 127.0.0.1:11211;

error_page 404 = @dynamic_request;

}

location = @dynamic_request {

proxy_pass appserver;

}

}

You can set the key with which Nginx will query memcached via the memcached_key variable directly in your configuration file. Any Nginx variable can be used to create the key: uri, args, http_user_agent, etc.

More complex keys can also be created with the help of the Perl module, which allows you to execute Perl directly within Nginx. To enable this module, specify –with-http_perl_module when running configure. Once installed, we can execute arbitrary code on the incoming request. For example, you can create an MD5 hash of the request URI,

Return Main Page Previous Page Next Page

®Online Book Reader