Linux Memcached Explained
Memcached is a memory caching system. To understand memcached, we need to have a preliminary knowledge about some web terms. Let’s see those basic levels and easy to understand features before going to memcached.
Static and Dynamic Websites
The websites are basically classified into two: static websites and dynamic websites. A static website can be defined as a simple website that is usually coded in plain HTML. The whole content of the webpages of these websites will be from the appropriate .html files (Codes). This website take no data from an outer source. The dynamic websites are the websites which are written using a server-side scripting language such as PHP, JSP, ASP etc.
A database-driven website is a dynamic website which has most of its contents in a database. The dynamic webpages can have more size than the static websites so that they can load slowly. To speed up this loading, we use memory caching mechanism. Memory caching can improve the website loading speed.
Memory caching is a reserved portion of high-speed static RAM (SRAM). It stores data that could be accessed repeatedly by different programs. It is effective because data can be easily served to the requests with high speed because it is served by SRAM. This process of storing the data on high demand on the more powerful memory to process it fast and thus improving the overall performance is called the memory caching.
What is Memcached?
The Memcached is a distributed memory object caching system. Even though Memcached is intended to speed up dynamic web applications, it is actually generic in nature. The Memcached improves the loading speed of dynamic database-driven websites efficiently. This is achieved by caching data and objects in RAM. It helps to reduce the number of times external data source (for example a database) must be read. The Memcached runs on all Unix-like operating systems that have at least Linux and OSX and also on Microsoft Windows. The software is free and open source. It is licensed under Revised BSD license. It works dependent on libevent library. The Memcached is useful and either the number of requests for a particular content is high or the cost of generating particular piece of content is high.
The libevent is a software library that provides asynchronous event notification. The libevent API provides a mechanism to execute a call back function when a specific event occurs on a file descriptor or when a timeout period has been reached. This software library also supports call backs triggered by signals and regular timeouts.
The Memcached was developed by Brad Fitzpatrick for his website LiveJournal on 22 May, 2003. The software was originally written in pearl, then later in C.
As we have seen earlier, Memcached works on most of the Linux and BSD like systems. There are also windows builds available but no official support available for these build versions.
The software can be installed from a package same as other software. You can run the following command for Debian/Ubuntu:
# apt-get install memcached
and for Redhat/Fedora:
# yum install memcached
If you are using an OS which is of older version, the software available will be outdated. In such case, it is recommended to install the software from source code. When installing from the source code, you need to meet the dependency first. Memcached depends on libevent and you need to install this before proceeding with the compilation.
# apt-get install libevent-dev
# yum install libevent-devel
Now you can move on installing the software
# wget https://memcached.org/latest
[you might need to rename the file]
# tar -zxf memcached-1.x.x.tar.gz
# cd memcached-1.x.x
# ./configure –prefix=/usr/local/memcached
# make && make test && sudo make install
You can view full configure options by running the command
# ./configure –help
These are the steps to install Memcached. The Memcached is actually a key/value store daemon. After you install it, the caching of data will not be done automatically. You need to program the applications to utilize the service. So, you need to install a client to use implement caching.
Memcached APIs provide a very large hash table. This hash table can be distributed across multiple machines. When the table is full, subsequent inserts cause older data to be removed from the cache. This will be carried out in the least recently used order. Applications using Memcached typically layer requests and additions into RAM before falling back on a slower backing store like a database.
In the Memcached system, each item is made up of certain features. This involves a key, an expiration time, optional flags, and raw data. At the time of an item is requested, Memcached checks the expiration time to see if the item is still valid before returning the item to the client. The cache can be integrated with the application by ensuring that the cache is updated at the same time as the database. If the server runs out of memory, Memcached will look for the expired items to replace. If additional memory is needed after replacing all the expired items, Memcached replaces items that have not been requested for a certain length of time (the expiration timeout period or longer) and will try to keep more recently requested data in the memory.
The size of the hash table used in Memcached is often very large. It is limited to available memory across all the servers in the cluster of servers in a data center. Where high volume, wide audience web publishing requires it, this maybe as large as gigabytes. The Memcached consists of four fundamental components:
1) A client software. It receives a list of available Memcached servers.
2) A client-based hashing algorithm. This algorithm chooses a server based on the key input.
3) Server software, which stores the values with their keys into the internal hash table.
4) Server algorithms, which remove old data or reuse memory.
If you need any further assistance please contact our support department.