Home » Memcached Tutorial

Memcached Tutorial

Memcached Tutorial

Memcached tutorial provides basic and advanced concepts of Memcached. Our Memcached tutorial is designed for beginners and professionals.

Memcached is a free, open-source, high-performance, distributed memory object caching system. Memcached is used to speed up dynamic web applications by reducing the database load. Memcached is used by all the major websites having huge data for example, YouTube, Wikipedia, Twitter etc.

Here, you will learn What is Memcached, History of Memcached, Components of Memcached, Features of Memcached, Disadvantages of Memcached, How Does Memcached Work, Installation Process of Memcached, Memcached Storage Commands, How to set, get, replace, delete data in Memcached etc.

What is Memcached?

Memcached is pronounced as mem-cash-dee or mem-cached. It is a free, open-source, high-performance, distributed memory object caching system. Memcached is used to speed up dynamic web applications by reducing the database load. Memcached is used by all the major websites having huge data for example, YouTube, Wikipedia, Twitter etc.

Memcached is used in memory caching software because it is very easy to install on any Windows or Unix system. It offers API integration for all the major languages like PHP, Java, C/C++, Python, Ruby, Perl etc.

It stores the data based on key values for small arbitrary strings or objects including:

  • Results of database calls
  • API calls
  • Page rendering

History of Memcached

Memcached was originally developed by Brad Fitzpatrick from Danga Interactive for LiveJournal in 2003. It was written is Perl, but is rewritten in C by Anatoly Vorobey.

Components of Memcached

Memcached is made up of 4 main components. These components allow the client and the server to work together in order to deliver cached data as efficiently as possible:.

  1. Client Software: It is used to give a list of available Memcached servers.
  2. A Client-based hashing algorithm: It chooses a server based on the key.
  3. Server Software: It is used to store values and their keys into an internal hash table.
  4. LRU: LRU stands for Least Recently Used. This determines when to throw out old data or reuse memory.

Features of Memcached

  • It is open source.
  • It is very scalable; just add boxes with memory to spare.
  • Memcached runs as a standalone service. So, if you take your application down, the cached data will remain in memory as long as the service runs.
  • Memcached server is a big hash table.
  • It reduces the database load.
  • It is very efficient for websites with high database load.
  • The cache nodes are very ignorant: which means they have no knowledge about other nodes participating. This handles the management and configuration of such a system extremely easy.
  • It is distributed under BSD (Berkeley Software Distribution) license.
  • It is a client server application over UDP or TCP.

Disadvantages of Memcached

  • It is not a fault- tolerant tool.
  • Compared to in-memory cache, it is very slow, mostly because of serialization or deserialization and network latency.
  • The cache nodes are very ignorant: For example, there is no way to iterate over all of the cached items.
  • It is not a persistent datastore.

How does Memcached Work?

As mentioned above, Memcached has four main components and these components are what allow it to store and retrieve data. Each item is comprised of a key, expiration time, and raw data. At a high-level Memcached works as follows:

  • The client requests a piece of data then Memcached checks to see if it is stored in cache.
  • There may a possibility of two possible outcomes:
    • If the data is stored in cache: return the data from Memcached (there is no need to check the database).
    • The data is not stored in cache: query the database, retrieve the data, and subsequently store it in Memcached.
  • Whenever information is modified or the expiry value of an item has expired, Memcached update its cache to ensure fresh content is delivered to the client.

This setup has various Memcached servers and many clients. Clients use a hasing algorithm to determine memcahed storage server for use. This helps to distribute the load.

And then server computes a second hash of the key in order to determine where it should store the corresponding value in an internal hash table. Some important things about Memcached architecture are:

  • Data is only sent to one server
  • Servers don’t share data
  • Servers keep the values in RAM – if RAM runs out the oldest value is discarded.

Memcached Tutorial Index



Prerequisite

Before learning Memcached, you should have basic knowledge of data structure and the working of cache.

Audience

Our Memcached tutorial is designed for beginners and professionals.

Problems

We assure you that you will not find any problem in this Memcached Tutorial. But if there is any mistake, please post the problem in the contact form.


You may also like