Jump to content

memcached

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 131.107.0.114 (talk) at 22:20, 8 February 2010 (→‎Example code: the note was confusing since it applies to the whole section and not that first block.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Memcached
Developer(s)Danga Interactive
Stable release
1.4.4 / November 26, 2009 (2009-11-26)
Repository
Written inC
Operating systemCross-platform
Typedistributed memory caching system
LicenseBSD License
Websitehttp://www.memcached.org/

memcached (pronunciation: mem-cash-dee) is a general-purpose distributed memory caching system that was originally developed by Danga Interactive for LiveJournal, but is now used by many other sites. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. Memcached is distributed under a permissive free software license.[1]

By default, memcached uses port 11211. Among other technologies, it uses libevent.

Memcached's APIs provide a giant hash table distributed across multiple machines. When the table is full, subsequent inserts cause older data to be purged in least recently used (LRU) order. Applications using memcached typically layer memcached requests and additions into core before falling back on a slower backing store, such as a database.

The system is used by several very large, well-known sites including YouTube[2], LiveJournal, Wikipedia/Wikimedia, Amazon.com, Wikia, SourceForge, Metacafe, Facebook[3] [4], Digg, Twitter[5], Fotolog, The Pirate Bay[6] and Netlog.[7]

Security

Most deployments of memcached exist within trusted networks where clients may freely connect to any server. There are cases, however, where memcached is deployed in untrusted networks or where administrators would like to exercise control over the clients that are connecting. For this purpose memcached can be complied with optional SASL authentication support. The SASL support requires the binary protocol.

Example code

Note that all functions described on this page are pseudocode only. Memcached calls and programming languages may vary based on the used API.

Converting a database or object creation queries to use memcached is simple. Typically, when using straight database queries, example code would be as follows:

 function get_foo(int userid) {
    result = db_select("SELECT * FROM users WHERE userid = ?", userid);
    return result;
 }

After conversion to memcached, the same call might look like the following

 function get_foo(int userid) {
     result = memcached_fetch("userrow:" + userid);
     if (!result) {
         result = db_select("SELECT * FROM users WHERE userid = ?", userid);
         memcached_add("userrow:" + userid,  result);
     }
     return result;
 }

The server would first check whether a memcached value with the unique key "userrow:userid" exists, where userid is some number. If the result does not exist, it would select from the database as usual, and set the unique key using the memcached API add function call.

However, if only this API call were modified, the server would end up fetching incorrect data following any database update actions: the memcached "view" of the data would become out of date. Therefore, in addition to creating an "add" call, an update call would be also needed, using the memcached set function.

 function update_foo(int userid, string dbUpdateString) {
     result = db_execute(dbUpdateString);
     if (result) {
         data = createUserDataFromDBString(dbUpdateString);
         memcached_set("userrow:" + userid, data);
     }
 }

This call would update the currently cached data to match the new data in the database, assuming the database query succeeds. An alternative approach would be to invalidate the cache with the memcached delete function, so that subsequent fetches result in a cache miss. Similar action would need to be taken when database records were deleted, to maintain either a correct or incomplete cache.

References

  1. ^ "License of memcached".
  2. ^ Cuong Do Cuong (Engineering manager at YouTube/Google). Seattle Conference on Scalability: YouTube Scalability (Online Video - 26th minute). Seattle: Google Tech Talks. {{cite AV media}}: External link in |title= (help); Text "June 23, 2007" ignored (help)
  3. ^ Facebook Developers Resources
  4. ^ Scaling memcached at Facebook
  5. ^ It's Not Rocket Science, But It's Our Work
  6. ^ Proof TPB is using Memcached
  7. ^ Who's using memcached?

See Also