To developers the term ‘Memcached’ will be a familiar one. For many it is already a standard part of their application. For those who are not familiar with Memcached or have never worked with it, we wrote this blog post to shed some light on what Memcached does, where best to use it and why.
What is Memcached?
Memcached calls itself a ‘distributed object caching system’. Of course a part of the definition is in the name of this tool, Memory Cache or Memcache. Memcached is a service that runs on a server, just like a webserver such as Apache or Nginx or MySQL would run on a server. The idea behind Memcached is to have a very fast key/value way to store ‘disposable’ data. It’s important to know that this is not persistent storage. It isn’t used to replace the data storage of e.g. your database, it only completes this system. All data in Memcache is fleeting. When restarting the Memcached service all data expires and you get a new empty ‘memory cache’. Why could that be handy?
Take a PHP web application for an example. On a busy website, like a news-site, a lot of visitors come by to retrieve the same data over and over again. A homepage with news items, author info and the number of comments on such a news item can become a complex query to calculate. Multiple JOINs, a lot of data … Even though it’s almost the same information for each visitor to the site. The idea behind Memcached is that you retain the results of heavy operations, like retrieving news-item, comments, calculating calculations … in the memory of your server, so you can call on them later and not have to put a load on the database for such information.
Memcached has comparisons to e.g. Varnish (read: What is Varnish and what does it do for me?) because they manage a cache in a similar way. The reasoning behind a Memcached implementation is as follows:
- A visitor requests a page on, e.g. website.be/news-tems
- The PHP code will check whether the information that needs to be displayed is in Memcached.
- If the information isn’t in Memcached, a query is executed to consult the database
- Once the information has been requested, the PHP code stores a copy of that result in Memcached
- The page is displayed to the user
For the first visitor to the website a query will be used to request information from a database. When that information is retrieved, it is also copied to the Memcached server. The next visitor to the site has the advantage of not having to wait for a database query, but having the information retrieved from Memcached. On buys websites this saves a lot of database activity and in doing so thoroughly lightens the load on the database server.
How does Memcached work?
But how does it work? Placing data in Memcached is done through a ‘key’. You can link a ‘value’ to a ‘key’. This means that you can have e.g. a key called ‘website_news_items_homepage’ in Memcached with all news items of the homepage as value. Retrieving data from Memcached means requesting the key ‘website_news_items_homepage’. It’s important to remember that the values in Memcached are fleeting and that it should never be used to store data that must be retained. That’s what the database is for, it has persistent storage.
Putting data in Memcached is as simple as requesting it. In PHP there are multiple ‘extensions’ to talk to Memcached, the most well-known are Memcache and Memcached. Not only their names are similar (only difference is the letter ‘d’), but their function is nearly identical as well.
Memcached is especially usefull in read-heavy environments. A write-action, such as e.g. a signup from a user in your application, is something you can’t cache in Memcached. That data has to go to the database. Multiple requests for user information on the other hand, the read-requests, could go in Memcached.
Only a caching solution?
Memcached can do more than simply caching data, although that is its primary function. It’s also a handy tool to make data available to multiple systems, just like a database server. It can be a central point in a solution where systems can write data and retrieve it. There are even solutions where Memcached is treated as a job-queue, although there are better tools for that (like e.g. Gearman Job Server).
Memcached is often called on to store user session information, such as e.g. the traditional PHP session that are stored on the hard disk by default (or in case of Drupal in the database). By hooking all sessions of a cluster of webservers to one Memcached (or a redundant set of Memcached’s) a user can remain logged in on all servers, even if those would be behind load balancers.
Memory use in Memcached
Memcached is set up to store a maximum of MB’s or GB’s in memory. On a server you can allocate e.g. 512MB of memory to Memcached. Once Memcached received items to store and the memory slowly fills up, it will use a Least Recently Used (LRU) system to throw old items out of the cache. Storing a value in Memcached will always work, that’s not something you need to take into account as developer. Should Memcached be ‘full’ and a new value needs to be stored, then the least recently used value will automatically be removed from the cache.
If you delve deeper into the functioning of Memcached, you’ll find the use of ‘slabs’. Say Memcached gets 512MB of memory then it will divide that internally into multiple pieces, or slabs. A ‘slab’ stores key of (roughly) the same size. If you want to store an item that is a few KB’s large, then it will arrive in the first slab. If there are items that are multiple MB’s in size, then the slab that is responsible for that size will be addressed. Usually you don’t need to take this into account, only on very busy Memcached servers where many items are ‘evicted’ (removed from cache) will it be interesting to investigate why. A good explanation about what slabs are, how they work and why they are used in Memcached you can find in the MySQL manual: Memory Allocation with Memcached.
Memcached in Drupal, WordPress, Joomla …
Fortunately Memcached is already implemented in popular CMS systems like Drupal, WordPress, Joomla … Usually in the form of extra plugins or modules. You can probably find a plugin in your favorite CMS to use Memcached. This plugin can then store the data that usually comes from MySQL in its memory for a faster user-experience for your customers. Depending on your website and the complexity of it, this can result in an increase in speed of 1 to 25 %. Without a lot of work.
What Nucleus products can I use Memcached on?
At Nucleus you can run a Memcached service on a Virtual Private Server, a Dedicated Server or a Cloud Server. Or on your own collocated server of course. Unfortunately Memcached isn’t possible on shared hosting. From your application you can talk to Memcached in almost every programming language. From Windows environments as well, it’s not limited to the Linux world.
Sounds interesting? Then our support department can see if Memcached can be installed on your server and whether your application is suited for it. Have questions? Contact us or tweet us @nucleus_hosting or through our Facebook page.