The Tech Bench: Memcached
What is caching?
Servers have limited resources, and at any given time of high traffic, one or more of these resource limitations may create a bottleneck. A server–no matter the size–can only handle so many requests at a time. But what if there was a way to ease the burden when a server is asked to serve up the same information over and over?
Think about a WordPress site. Each page a user visits is not a single file, but rather an amalgamation of the theme, widgets, posts, footers, headers, etc. Each time a page is accessed, PHP will generate the page requested on the fly from the database. It takes time to query the database to create the page. These database queries put a strain on the resources of your server.
However, what if instead of continually generating a new page–the same page–for every visitor, you turned those pages into static HTML files? No database querying would be needed for new visitors. And a static file can be served up much faster and with significantly less resource consumption. Your visitors see their requested page sooner, and you save on CPU cycles. Everyone is happy. This is what caching does.
Memcached
Memcached is a general-purpose, free and open-source distributed memory caching system. It is one of the most popular caching tools and is used in such popular sites as YouTube, Reddit, Zynga, Facebook, and Twitter. If you also run dynamic, database-driven sites, Memcached may reduce resource consumption and speed up page delivery on your server as well.
Installing Memcached (daemon)
The quickest and easiest method is to install Memcached via Yum on the command line of your server.
1. Grab the RPM that matches your OS:
CentOS 6 (64 bit):
su -c ’rpm -Uvh http://mirrors.kernel.org/fedora-epel/6/x86_64/epel-release-6-7.noarch.rpm’
CentOS 6 (32 bit):
su -c ’rpm -Uvh http://mirrors.kernel.org/fedora-epel/6/i386/epel-release-6-7.noarch.rpm’
CentOS 5 (64 bit):
su -c ’rpm -Uvh http://mirrors.kernel.org/fedora-epel/5/x86_64/epel-release-5-4.noarch.rpm’
CentOS 5 (32 bit):
su -c ’rpm -Uvh http://mirrors.kernel.org/fedora-epel/5/i386/epel-release-5-4.noarch.rpm’
2. Install it with Yum:
yum install memcached
3. Start the memcached service:
/etc/init.d/memcached start
4. Configure the memcached service to start when the server boots:
chkconfig memcached on
5. Finally, disable the RPM so that it is not used for future Yum functions:
perl -pi -e “s/enabled=1/enabled=0/g;” /etc/yum.repos.d/epel.repo
Of course, if you think Memcached may be right for your server, but you’re not ready to install it yourself, just open up a support ticket in your ServInt Customer Portal and the MST will be happy to help.
Configuring Memcached
A main benefit of Memcached is also a drawback. It is language independent so it can work in many environments. But specifically because of this, it needs to be coded into the site to do its job. If you’re not fluent in PHP, a qualified web developer can do this for you.
Alternatively, check the sites of any third-party software running on your server to see if they have a plugin that allows you to use Memcached without further coding.
Check back next time, we we discuss how to install the PHP memcache extension.

The Tech Bench is an ongoing blog series featuring the answers to common questions the ServInt MST fields everyday. You can also find more great tech tips in the ServInt KnowledgeBase.

I understand this is an introductory post about Memcached, although one important issue that should be noted and implemented is security. Memcached does not provide any built-in security, so any requests to port 11211 (the default) will be accepted. A quick solution is to add a rule to the firewall running your Memcached instance to only allow connections to your web application’s host.
thanks for the help on CentOS 4.9. I guess I need to upgrade.
meeven, Unfortunately not. There are too many different types of sites and different use cases to accurately tell you.
The documentation for memcached can be found here: http://code.google.com/p/memcached/wiki/NewStart
[...] written for the ServInt blog as part of the ‘Tech bench’ series. You can view it on the ServInt blog here. Used with permission. Related Posts:How to install Icecast centos 5, centos 6, [...]
@Bill
>>what if you’re running on CentOS 4.9?
We hadn’t included CentOS 4 because it is going to be End of Life (EOL) this coming Feb. 29th.
You can install memcached for CentOS 4 by getting it’s EPEL RPM:
su -c ‘rpm -Uvh http://mirror.symnds.com/distributions/fedora-epel//4/i386/epel-release-4-10.noarch.rpm‘
what if you’re running on CentOS 4.9?
>>Yes, Memcached can work with suPHP, but your site has to be coded correctly.
That’s a bit too cryptic :-)
Could you please explain in a little more detail?
Yes, Memcached can work with suPHP, but your site has to be coded correctly.
But does it work with suPHP as your handler?