Having the proper caching mechanisms in place is one of the most important things you can do to speed up the delivery of your website’s assets. Caching ensures that assets are stored somewhere closer to the user so that latency incurred by distance is minimized. Learn more about the ins and outs of caching by checking out our cache definition and explanation guide.
If you’re investigating what options are available when it comes to choosing a caching system, you may have come across Memcached. This post will focus on explaining what exactly Memcached is, how it works, and how you can install it to speed up your dynamic web applications.
What is Memcached?
Memcached is an open source distributed memory caching system. It is used for speeding up dynamic web applications by reducing database load. In other words, every time a database request is made it adds additional load to the server. Memcached reduces that load by storing data objects in dynamic memory (think of it as short-term memory for applications). Memcached stores data based on key-values for small arbitrary strings or objects including:
- Results of database calls
- API calls
- Page rendering
Memcached is made up of four main components. These components allow the client and the server to work together in order to deliver cached data as efficiently as possible:
- Client software – Which is given a list of available Memcached servers
- A client-based hashing algorithm – Chooses a server based on the “key”
- Server software – Stores values and their keys into an internal hash table
- LRU – Determines when to throw out old data or reuse memory
How Does Memcached Work?
As mentioned above, Memcached is comprised of four main components and these components are what allows 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 which Memcached checks to see if it is stored in cache
- There are two possible outcomes here:
- If the data is stored in cache: return the data from Memcached (no need to check the database)
- If the data isn’t stored in cache: query the database, retrieve the data, and subsequently store it in Memcached
- Whenever information is changed or the expiry value of an item has expired, Memcached updates its cache to ensure fresh content is delivered to the client
A typical setup has various Memcached servers and many clients. Clients use a hashing algorithm to determine which Memcached storage server to use – this helps distribute the load. The server then computes a second hash of the key in order to determine where it should store the corresponding value in an internal hash table. A few important points about Memcached architecture include:
- Data is only sent to one server
- Servers don’t share data
- Servers keep values in RAM – if RAM runs out the oldest value is discarded
There are a few ways you can install Memcached. Depending on which system you’re using, the method will vary. As outlined on the official Memcached Installation Wiki, installation from a package is simple. If you’re using Debian or Ubuntu, use the following snippet:
apt-get install memcached
For Redhat/Fedora users, use the following:
yum install memcached
There are a few other ways to install Memcached which you may want to consider depending on your requirements as outlined in the Wiki. If you want to learn more configuration, usage, and maintenance of Memcached, it’s highly recommended that you check out their complete Wiki hosted on Github.
Memcached has been around for a while (since 2003) and is still used today to accelerate database queries for dynamic applications. If load times are of priority for you (which they should be) and you’d like to implement a tried-and-true caching solution for your application, consider giving Memcached a try.