Memcached is a software that implements a RAM caching service based on a hash table.

file

Memcached is a server that stores some data in RAM with a specified lifetime. The data is accessed by a key (name). You can think of Memcached as a hash table stored on the server. It's mostly used to cache web page code, database query results, etc.

With the help of the client library (for C/C++, Ruby, Perl, PHP, Python, Java, .Net, etc.) it allows to cache data in the main memory of the many available servers. The distribution is implemented by segmenting the data based on the key hash value by analogy with hash table sockets. The client library, using the data key, computes the hash and uses it to select the appropriate server. The server failure situation is treated as a cache miss, which allows to increase the fault tolerance of the complex by increasing the number of memcached servers and the ability to hot-swap them.

The memcached API has only basic functions: server selection, setting and breaking connections, adding, removing, updating and retrieving an object, and Compare-and-swap. Each object has a lifetime, from 1 second to infinity. Older objects are automatically deleted when memory runs out. For PHP, there are also ready-to-use PECL libraries for working with memcached, which give additional functionality.

Memcached features:*

  • All data is stored in memory to speed up read-write;
  • The default maximum key length is 250 bytes and the default value length is 1 MB;
  • Keys can be "extended" with some MD5 or SHA512 (in which case it is reasonable to duplicate the unhashed key in the value);
  • If you want to store very long values, you can compress them and/or split them up;
  • Memcached uses LRU caching algorithm;
  • All I/O is done with libevent;
  • To speed things up, memory is allocated when the daemon starts and is not released until it stops;
  • The slab allocator is used to combat memory fragmentation;
  • All operations are atomic, there is support for compare-and-swap;
  • Memcached can be handled via UDP;
  • In addition to the text-based protocol, there is also a binary protocol;
  • It is written in C and distributed under the BSD license;
Updated Sept. 17, 2018