This week, memcached, a piece of software that prevents much of the Internet from melting down, turns 10 years old. Despite its age, memcached is still the go-to solution for many programmers and sysadmins managing heavy workloads. Without memcached, Ars Technica would likely be unable to serve this article to you at all.

Brad Fitzpatrick wrote memcached for LiveJournal way back in 2003 (check out the initial CVS commit here). While waiting for new hardware to help save the site from being overloaded, Fitzpatrick realized that he had plenty of unused RAM spread across LiveJournal's existing servers. He wrote memcached to take advantage of this spare memory and lighten the load on the site.

memcached is a distributed in-memory key-value store that uses a very simple protocol for storing and retrieving arbitrary data from memory instead of from a filesystem. To store a value, a program connects to the memcached server on the default port of 11211 and issues a series of basic commands. (Note: a binary protocol is also supported.)

# telnet 127.0.0.1 11211 -> set hello 0 60 5 -> world <- STORED -> get hello <- world

The above log connects to the server, sets a key named hello (which expires in 60 seconds), and then fetches it back again. Simple, right? It is, but this becomes very useful when trying to avoid hitting an already overloaded database or filesystem. Additionally, through consistent hashing of keys, memcached clients are able to distribute values across multiple memcached servers.

For example, let's imagine that we have an author's information stored in a database. We could query the database on every pageview for the information, or we could run the query once and store the result in memcached for future requests. The basic logic looks like this:

$author_info = cache_get($author_id); if ($author_info == false) { $author_info = expensive_database_query($author_id); cache_set($author_id, $author_info, 60); } # ... do stuff with $author info

Today, Ars Technica uses multiple memcached servers to store values such as comment counts, author info, and large chunks of HTML on every page. A single page load can pull hundreds or thousands of values from memcached, or even the entire page if it's a popular URL.

Ask any seasoned programmer about memcached and they'll likely have a story or two about how it saved their ass on launch day. So let's take a moment to thank all the memcached contributors for creating and maintaining one of the most important pieces of software used on the Web today!