What is Memcached?

Memcached is an open sourced, free, distributed caching system. It is an in-memory key value pair store and is often used to speed up retrieval of information by storing it in memory, which otherwise would have taken longer time to retrieve from its original slower source like database, file system, web service etc.

Mechanism of Working

The servers maintain a key–value associative array; the clients populate this array and query it by key. Keys are up to 250 bytes long and values can be at most 1 megabyte in the size. When the table is full, subsequent inserts cause older data to be purged in the least recently used (LRU) order.

Important Points to Remember



  • Memcached has no internal mechanism to track misses which may happen.
  • When the size of cache is full, older data is removed in LFU order. So clients must treat Memcached just as a transitory cache; they cannot assume that data stored in Memcached is always there.
  • Memcached does not support any clustering features.
  • It is important to implement Serialization of Data Structures that an application intends to cache in memcached.
  • When an object stored in cache is no longer valid from business perspective, application must make sure to manually remove it from Cache. For example if application is storing product information in cache then in case of a product becomes no longer available it needs to be manually removed from cache.

Clustering using Memcached

Out of the box, Memcaches does not support any failover or clustering features. So, if an application (client) needs to scale up to using multiple memcached servers, then it has to be implemented by client itself using appropriate algorithms. Memcached servers do not communicate with each other and do not have information about existence of other such servers in the cluster.

Normally, client knows about all available memcached servers. Clients achieve clustering by generating hash for the information it intends to store in memcached and using that hash decides on which memcached server in the cluster to use for that specific (set/get) operation.

 

Memcached Installation Instructions

Note: The versions earlier then 1.5.6 were shipped with UDP port 11211 open by default which is susceptible to DDoS attacks. If you install older versions, please make sure to disable UDP port. You can download latest version from memcached.org

By default, memcached works at port 11211 on TCP.

Memcached is typically light on CPU usage, so does not have massive infrastructure requirements. Memory requirement depends on the amount of data you intend to cache.

There is no official support for windows, but as it is open sourced, it has been compiled for windows and can be downloaded from here – Memcached for Windows

For Linux based machines, it is best to install memcached using native package installers. For example for Ubuntu:

apt-get install memcached

for Redhat/Fedora:

yum install memcached

How to use Memcached


Memcached API is available for most popular languages. It offers basic simple commands to manage the data stored in cache. Following are the three mostly used commands and sufficient to meet most common requirements –

  • set – Most common command. Store this data, possibly overwriting any existing data. New items are at the top of the LRU.
  • get – Command for retrieving data. Takes one or more keys and returns all found items.
  • delete – Removes an item from the cache, if it exists.

 

Following is a sample algorithm indicating how applications normally use memcached

Function GET_PRODUCT_INFO(String PROD_CODE) {
   Search if PROD_CODE is available in Memcached
   If Found
      Return PROD_DATA which is stored against PROD_CODE key
   Else
      Get PROD_DATA from original source like database
      Add PROD_DATA to Memcached with PROD_CODE as key for future fast retrieval
      Return PROD_DATA
}

Leave a Comment

Your email address will not be published. Required fields are marked *