Memcache: Turbo Charge your website
May 19th, 2009 | Published in Daemons
The general problem faced while making content-rich websites is that of content loading speed and the response time of the web pages. Very often we see websites that are well designed and have a good navigational structure. However, the slow loading of content ruins the user experience. A number of issues present in a the website’s technology stack can effect the load time. Developers often try to improve loading speeds by a method known as caching. One software available for caching content in various forms is Memcache.
Lets assume we have user data that is generally accessed at all levels of a web application, calling the data from the database every time it is needed is not viable as it will have cost each time the call is made. Also, if the query is complex and is difficult to cache at the db level, it can hurt the performance of your web application considerably. Enter Memcache: its a daemon that can be installed on your server, and gives you the support to cache your data for a predefined or unlimited period of time or until the server is restarted (which is a very rare case). A number of programming languages including PHP support it and it can be used for giving calls to the memcache daemon to save or retrieve data.
How does it work?
- First identify areas which need to moved into memcache. It is recommended that you move your user data and information that can help you reduce your database calls or joins into memcache.
- Memcache saves the data in the form of objects where each object has a key to identify it uniquely. This key can be used to fetch the object data. Any information can be stored in this object such as arrays, object of a class, integer values, etc.
- Different operations can be performed on the data, and memcache can be used to store the resulting data set. PHP also has memcache functions available such as $memcache_obj->add, $memcache_obj->delete etc. These functions can be used for setting, deleting and flushing memcache objects. More functions can be viewed at http://www.php.net/manual/en/book.memcache.php
General Problems and Solutions
• One of the major problems faced while working with Memcache is the updates, i.e. we typically update the database but forget to update the memcache object. The best way to go about this is to repopulate your objects.
• Memcache should not be relied on for complete data manipulation as it’s just like data which is put into memory, so it’s not a reliable source. Moreover, the rollback scenario needs to be implemented in case the Memcache server crashes.
Benefits:
• The Memcache storage space is easily extendible and whenever we reach the maximum storage limit of Memcache. We can go for ‘add more servers’ known as the ‘server node’. All the server nodes collectively make one Memcache area, and they act like one server.
• A highly distributed application can be made where we have many servers, and the temporary session information of the user can be stored in memcache objects.
• Boosts the speed of the application by reducing the database calls.
More information regarding the MC can be found at
http://www.danga.com/memcached/
