Using Memcached To Speed Up Your Website

Modern websites use a lot of data. Web applications and dynamic database-driven websites use a lot more data for a single page generation.

And if you multiply this amount by the number of your visitors, you can often experience an overloaded database, which would make your website a lot slower to load.

This is where Memcached can come to the rescue.

Memcached is a general-purpose distributed memory caching system which caches data and objects inside the RAM to reduce the number of times external data sources (database or API) should be read. As a high-performance in-memory data caching system, Memcached runs on your server and uses its available memory to temporarily store an associated array of data.

Speed

First developed by Brad Fitzpatrick for his website LiveJournal, on May 22, 2003, Memcached is an open source that uses distributed memory object caching system. Its API provides a large hash table distributed across multiple machines.

Its fundamental components include:

  1. Client software, which receives a list of available Memcached servers.
  2. A client-based hashing algorithm, that chooses a server based on the key input.
  3. Server software, needed to store the values with their keys into an internal hash table.
  4. Server algorithms, which determine when to remove old data or reuse memory.

In the Memcached system, each item has a key, an expiration time, optional flags, and raw data. When an item is requested by a website visitor, Memcached checks the expiration time to see if the item is still valid before returning it to the visitor.

The cache can also be integrated with the website by ensuring that the cache is updated at the same time as the database.

By default, Memcached acts as a "Least Recently Used" cache plus expiration timeouts. What this means, if your server runs out of memory, Memcached will look for expired items to replace. If additional memory is needed after replacing all the expired items, Memcached will replace items that have not been requested for a certain length of time, keeping more recently requested information in memory.

While Memcached can be useful for storing temporary values. It has a list of drawbacks. They include:

The number of servers which is not elastic, has no built in security, no support for locks, read-through, CAS, etc., a cached data value at 1 MB, unable to store key larger than 250 bytes, cannot contain a null byte, no data redundancy, requires changes to be pushed to all clients, and no events on cache changes.

Memcached is also useless for large files or media.

Memcache

So if a value is solely in the Memcached and not backed by other persistent storage, you need to make sure that your application behaves acceptably when the value is suddenly not available. Items can expire from the Memcached at any time, and can be expired prior to the expiration deadline previously set.

We can see that Memcached do have some drawbacks.

To overcome them and use Memcached in its most effective form, customized solutions should be available. For example, because Memcached can use a lot of memory to work, it's recommendation that you add APC to your stack, as it caches PHP files and lessens the overall memory usage per page. And for security, you can compile Memcached with optional SASL authentication support.

Memcached was originally written in Peal programming language, before later being rewritten in C.

Its capabilities has made it popular for many websites, including Google, Facebook, YouTube, Wikipedia, Pinterest, WordPress, Twitter, Amazon Web Services and more.

Memcached is small and nothing much. But its presence between your code and your database, with proper implementation, can mean a whole lot of different things for your website, as it can speed up the loading time of your site significantly.