Jump to content

Memcached

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by ToikeOike (talk | contribs) at 00:11, 10 September 2013 (Minor change to a sentence that sounded a bit awkward.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Memcached
Developer(s)Danga Interactive
Initial releaseMay 22, 2003 (2003-05-22)
Stable release
1.4.15 / September 3, 2012 (2012-09-03)[1]
Repository
Written inC
Operating systemCross-platform
Typedistributed memory caching system
LicenseNew BSD License[2]
Websitewww.memcached.org

In computing, Memcached 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 runs on Unix, Linux, Windows and Mac OS X and is distributed under a permissive free software license.[3]

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.[4][5] Applications using Memcached typically layer requests and additions into RAM before falling back on a slower backing store, such as a database.

The system is used by many sites, including MocoSpace,[6] YouTube,[7] Reddit,[8] Zynga,[9] Facebook,[10][11][12] Orange,[13] Twitter,[14] Tumblr[15] and Wikipedia.[16] Engine Yard and Jelastic are using Memcached as the part of their platform as a service technology stack[17][18] and Heroku offers a managed Memcached service built on Couchbase Server[19] as part of their platform as a service. Google App Engine, AppScale, Windows Azure and Amazon Web Services also offer a Memcached service through an API.[20][21][22][23]

History

Memcached was first developed by Brad Fitzpatrick for his website LiveJournal, on May 22, 2003.[24][25][26] It was then rewritten in C by Anatoly Vorobey.[27]

Architecture

The system uses a client–server architecture. The servers maintain a key–value associative array; the clients populate this array and query it. Keys are up to 250 bytes long and values can be at most 1 megabyte in size.

Clients use client-side libraries to contact the servers which, by default, expose their service at port 11211. Each client knows all servers; the servers do not communicate with each other. If a client wishes to set or read the value corresponding to a certain key, the client's library first computes a hash of the key to determine the server to use. Then it contacts that server. The server will compute a second hash of the key to determine where to store or read the corresponding value.

The servers keep the values in RAM; if a server runs out of RAM, it discards the oldest values. Therefore, clients must treat Memcached as a transitory cache; they cannot assume that data stored in Memcached is still there when they need it. MemcacheDB, Couchbase Server, Tarantool and other database servers provide persistent storage while maintaining Memcached protocol compatibility.

If all client libraries use the same hashing algorithm to determine servers, then clients can read each other's cached data; this is obviously desirable.

A typical deployment will have several servers and many clients. However, it is possible to use Memcached on a single computer, acting simultaneously as client and server.

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 compiled with optional SASL authentication support. The SASL support requires the binary protocol.

A presentation at BlackHat USA 2010 revealed that a number of large public websites had left Memcached open to inspection, analysis, retrieval, and modification of data.[28]

Example code

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

Converting 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) {
    data = db_select("SELECT * FROM users WHERE userid = ?", userid);
    return data;
 }

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

 function get_foo(int userid) {
    /* first try the cache */
    data = memcached_fetch("userrow:" + userid);
    if (!data) {
       /* not found : request database */
       data = db_select("SELECT * FROM users WHERE userid = ?", userid);
       /* then store in cache until next get */
       memcached_add("userrow:" + userid, data);
    }
    return data;
 }

The client 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 also be needed using the Memcached set function.

 function update_foo(int userid, string dbUpdateString) {
   /* first update database */
    result = db_execute(dbUpdateString);
    if (result) {
       /* database update successful : fetch data to be stored in cache */
       data = db_select("SELECT * FROM users WHERE userid = ?", userid);
       /* the previous line could also look like data = createDataFromDBString(dbUpdateString); */
       /* then store in cache until next get */
       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.

See also

References

  1. ^ "Release notes for Release 1.4.15". Retrieved 2012-10-03.
  2. ^ "memcached". Google Code/Memcached Project. Retrieved 2013-05-22.
  3. ^ "License of Memcached".
  4. ^ "Memcached NewOverview".
  5. ^ "Memcached NewUserInternals".
  6. ^ http://highscalability.com/blog/2010/5/3/mocospace-architecture-3-billion-mobile-page-views-a-month.html
  7. ^ Cuong Do Cuong (Engineering manager at YouTube/Google) (June 23, 2007). Seattle Conference on Scalability: YouTube Scalability (Online Video - 26th minute). Seattle: Google Tech Talks.
  8. ^ Steve Huffman on Lessons Learned at Reddit
  9. ^ How Zynga Survived FarmVille
  10. ^ Facebook Developers Resources
  11. ^ Scaling Memcached at Facebook
  12. ^ NSDI '13: Scaling Memcache at Facebook
  13. ^ Orange Developers
  14. ^ It's Not Rocket Science, But It's Our Work
  15. ^ http://www.jobscore.com/jobs2/tumblr/engineer-core-applications-group/cvAFBCbcyr47JbiGakhP3Q?ref=rss&sid=68
  16. ^ MediaWiki Memcached
  17. ^ Engine Yard Technology Stack
  18. ^ Jelastic Memcached System
  19. ^ Heroku Memcached add-on
  20. ^ Using Memcache - Google App Engine - Google Code
  21. ^ http://appscale.cs.ucsb.edu Memcached in AppScale
  22. ^ http://msdn.microsoft.com/en-us/library/windowsazure/hh914161.aspx
  23. ^ http://aws.amazon.com/elasticache/
  24. ^ [1]
  25. ^ [2]
  26. ^ [3]
  27. ^ [4]
  28. ^ BlackHat Write-up: go-derper and mining memcaches
  29. ^ "Speedy MySQL 5.6 takes aim at NoSQL, MariaDB."
  30. ^ Tarantool user guide

Commercially Supported Distributions