memcached
From Wikipedia, the free encyclopedia
Memcached | |
---|---|
Developed by | Danga Interactive |
Latest release | 1.2.5 / March 3, 2008 |
OS | Cross-platform |
Genre | distributed memory caching system |
License | BSD License |
Website | http://www.danga.com/memcached/ |
memcached (pronunciation: mem-cache-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 memory to reduce the number of times the database must be read. Memcached is distributed under a permissive free software license.[1]
Memcached lacks authentication and security features, meaning it should only be used on servers with a firewall set up appropriately. By default, memcached uses the 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 code 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, Slashdot, Wikipedia, SourceForge, ShowClix, GameFAQs, Facebook, Digg, Twitter[3], Fotolog, BoardGameGeek, NYTimes.com, deviantART, Jamendo, Kayak and Netlog.[4]
Contents |
[edit] Example code
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, and no others, the server would eventually end up fetching incorrect data (assuming the entire database doesn't fit into the memory caching available). 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.
Note that all functions described on this page are pseudocode only. Memcached calls and programming languages may vary based on the used API.
[edit] References
- ^ Licence of memcached.
- ^ Cuong Do Cuong (Engineering manager at YouTube/Google). Seattle Conference on Scalability: YouTube Scalability [Online Video - 26th minute]. Seattle: Google Tech Talks.
- ^ It's Not Rocket Science, But It's Our Work
- ^ Who's using memcached?
[edit] External links
The external links in this article may not follow Wikipedia's content policies or guidelines. Please improve this article by removing excessive or inappropriate external links. |
[edit] General
- Official memcached site
- memcached wiki and faq
- Win32 memcached port 1.2.1
- Win32 memcached port 1.2.4
[edit] Client Libraries
- PHP
- Java
- Python
- Perl
- pure Ruby
- compiled Ruby
- Enyim.com .NET client
- BeIT .NET client
- ASP.NET cache and session provider
- Scheme
- new C/C++ reference implementation
- old C implementation
[edit] Client Applications
- memcached in MediaWiki
- memcached storage engine for MySQL
- pgmemcached - API for PostgreSQL
- Memcached Functions for MySQL - API for MySQL
- memcachefs: a memcache filesystem using FUSE
- IPC::Lock::Memcached - fast locking via memcached