I’m designing a very database-intensive application that is going to need to do a lot of reads to render a page. I know this is going to get slow as the database fills up and I have a lot of users hammering on it, so what can I do to speed that up?
Use memcached, a caching application originally developed by Danga Interactive to speed up LiveJournal (http://livejournal.com) but now in use all over the Web (including YouTube, Slashdot, Wikipedia, SourceForge, Facebook, Digg, Twitter, and http://nytimes.com). Memcached is a sophisticated caching engine designed to quickly return results from memory instead of making a trip to your database, which makes it fast. In answer to the question, “Is memcached fast?”, Danga says:
Very fast. It uses libevent to scale to any number of open connections (using epoll on Linux, if available at runtime), uses nonblocking network I/O, refcounts internal objects (so objects can be in multiple states to multiple clients), and uses its own slab allocator and hash table so virtual memory never gets externally fragmented and allocations are guaranteed O(1).
That’s one of those blocks of text that you either understand on your own or, like me, have to read a few times before ultimately giving up and accepting that you can move on without knowing what a slab allocator is. You can find more information about memcached, as well as downloads, at http://www.danga.com/memcached.
This is one of those places where it’s worth building in scalability from the beginning. Thanks to the memcached libraries available for most languages, implementing is really easy and won’t significantly slow down your initial 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.