How does Memcached work? – A quick guide
Do you want to know how does the Memcached work? Here is a quick guide.
Memcached is a caching system that speeds up your web applications by storing frequently accessed data in super-fast RAM, instead of the super slow hard disk.
Today, we’ll cover how Memcached makes this happen, and how our System Engineers set it up.
The mechanics of Memcached
You see, all the data that we need are stored in server hard disks.
But hard disks, or HDDs for short, are notoriously slow because it is a mechanical device.
Memcached sits between your application and your HDD and keeps track of what the commonly asked bits of data are.
It’ll then store this data in the super-fast server memory so that the next time your application asks for it, it can be served right up! No-fuss.
Memcached – Under the hood
So how does Memcached serve the frequently requested data within fractions of seconds? The logic behind this is really simple.
Memcached stores every single piece of data with an ID. The Memcached calls it data as value and its ID as the key.
So when a user requests the ID again it can serve it in a glimpse.
But how does it store and fetch the data? The image says it all.
Let’s get a little deeper into the details.
When a client requests a data, Memcached checks for it in the cache. It returns the data if it is available in the cache. Otherwise, it queries the hard disk and then retrieves it.
If there is any change in the data stored, the Memcached updates its cache to serve the latest data to the clients.
How does Memcached work? – The technical stuff
Really keen to know how it actually works, then first have a look at the components involved. It includes Client software, a Hashing algorithm, Server software, and an LRU algorithm.
The client software is the available Memcached servers. It stores the data based on a hashing algorithm. That is it chooses a server to stores the data.
The server software stores the key and corresponding value in a hash table. While the fetching of the data relies on the LRU algorithm. In simple words, the least recently used data get loaded fast.
Now let’s see how our Support Engineers install Memcached.
How to install Memcached?
There are two ways to install Memcached. That is, either using the package manager provided by the OS or directly from the source.
Using a package manager
We can simply install Memcached from the package provided by the OS.
For instance, in a Ubuntu/Debian system, we use the command,
apt install memcached
Whereas in a RedHat/CentOS system we use the command,
yum install memcached
Here the OS will take care of the dependencies installation and security updates.
But if users run older OS version, it installs an older version of Memcached. In such cases, we recommend the customer to upgrade the OS.
Installation from the source
We can also install Memcached from the source. So first, we install its dependency named libevent-dev.
In an Ubuntu system, we use the command,
apt install libevent-dev
Whereas, in a CentOS system we use the command,
yum install libevent-dev
Then we install Memcached using the commands,
wget https://memcached.org/latest tar -zxf <package name> cd package name ./configure --prefix=/usr/local/memcached make && make test && sudo make install
Both the methods successfully install the Memcached. One way to connect to a Memcached server is through a telnet session,
telnet <HOST> <PORT>
Where and where not to use Memcached?
Memcached is best suited for larger websites with heavy traffic. That is servers that get frequent queries. If the most frequent queries are cached, then the website load is considerably reduced. And this speed up the website.
Even though, caching speed up websites, there are certain situations where it is not preferred to use Memcached.
For instance, if a website has more frequent updates, then caching will not speed it up. Because the cache needs to refresh itself always. Memcached has some limitations too. It cannot store objects larger than 1MB and keys longer than 250 characters.
[Need assistance in setting up Memcached? – We can help you.]
So far, we saw how does Memcached work. Memcached helps in improving server speed that gets frequent queries. We also saw how our Support Engineers install Memcached on various platforms.