Memcached

Memcached
Memcached
Developer(s) Danga Interactive
Initial release May 22, 2003 (2003-05-22)
Stable release 1.4.10 / November 9, 2011; 3 days ago (2011-11-09)
Written in C
Operating system Cross-platform
Type distributed memory caching system
License BSD License
Website 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 MacOSX and is distributed under a permissive free software license.[1]

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 requests and additions into RAM before falling back on a slower backing store, such as a database.

The system is used by sites including YouTube,[2] Reddit,[3] Zynga,[4] Facebook,[5][6] Orange,[7] and Twitter.[8] Heroku (now part of Salesforce) offers a Couchbase-managed memcached add-on service[9] as part of their platform as a service. Google App Engine, AppScale and Amazon Web Services also offer a memcached service through an API.[10][11][12] Memcached is also supported by some popular CMSs such as Drupal,[13] Joomla, and WordPress.[14]

Contents

History

Memcached was first developed by Brad Fitzpatrick for his website LiveJournal, on May 22, 2003.[15][16][17]

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 that will be used. 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. A Memcached-protocol compatible product known as MemcacheDB provides persistent storage.

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.[18]

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) {
    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) {
    /* 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 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) {
   /* 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);
       /* last 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

External links

Commercially Supported Distributions


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • memcached — Entwickler Danga Interactive Aktuelle Version 1.4.5 (3. April 2010) Betriebssystem Unix Derivate, Windows Kategorie Cache Server …   Deutsch Wikipedia

  • memcached — Тип memcached Разработчик Danga Interactive Написана на C[1] Операционная система Кроссплатформенное ПО Последняя версия 1.4.14 …   Википедия

  • Memcached — Тип memcached Разработчик Danga Interactive Написана на C[1] ОС Кроссплатформенное ПО …   Википедия

  • Memcached — est un système d usage général servant à gérer la mémoire cache distribuée. Il est souvent utilisé pour augmenter la vitesse de réponse des sites web créés à partir de bases de données. Il gère les données et les objets en RAM de façon à réduire… …   Wikipédia en Français

  • Memcached — Este artículo o sección necesita referencias que aparezcan en una publicación acreditada, como revistas especializadas, monografías, prensa diaria o páginas de Internet fidedignas. Puedes añadirlas así o avisar al autor pri …   Wikipedia Español

  • Membase — Developer(s) Couchbase (merged from NorthScale), Zynga, NHN Stable release 1.7.1 / July 26, 2011; 2 months ago (2011 07 26) Written in C++, Erlang …   Wikipedia

  • Brad Fitzpatrick — Bradley Joseph Brad Fitzpatrick (born February 5, 1980 in Iowa), often seen on the Internet under the nickname bradfitz, is an American programmer. He is best known as the creator of LiveJournal and is the author of a variety of free software… …   Wikipedia

  • Сравнение каркасов веб-приложений — Это сравнительная таблица фреймворков веб приложений. Содержание 1 Фреймворки 1.1 ASP.NET 1.2 C++ 1.3 ColdFusion Markup Language (CFML) …   Википедия

  • Comparison of web application frameworks — This is a comparison of notable web application frameworks. Contents 1 General 1.1 Perl 1.2 PHP 1.3 Java 1.4 Python …   Wikipedia

  • MemcacheDB — Stable release 1.2.1 / December 25, 2008 Operating system Cross platform Type distributed memory caching system License BSD License …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”