Setting up memcached on Cloud Servers

Filed in by Adrian Otto | July 30, 2009 1:28 pm

By: Adrian Otto

This tutorial explains how to set up memcached for RHEL5 or CentOS5 on Cloud Servers. Although the example is provided for PHP, you can access a memcached server from practically any language using one of the memcached Client API’s[1].

Step 1: Start up a Cloud Server

Use your Control Panel to start a Cloud Server. For an example, see this video demo[2].

Step 2: Log Into your Cloud Server using SSH

You may connect as root if your server configuration allows it, or connect as a user and use ‘su’ to get a root shell. I use my Terminal from OSX or PuTTY[3] on Windows.

$ ssh root@12.34.56.7
root@12.34.56.7's password:
[root@mcd-demo ~]#

Step 3: Set up rpmforge

The rpmforge package allows you to download and install third party packages from the DAG RPM archive using the ‘yum’ utility on any RHEL5 or CentOS5 system. First visit this URL and determine the current rpmforge package for your system. Note that all Cloud Servers are 64 bit.

http://dag.wieers.com/rpm/packages/rpmforge-release[4]

Once you have determined the current release for your system, use the ‘rpm’ tool to install it from the correct URL. Example:

# wget http://dag.wieers.com/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm
# rpm -ivh rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm

Step 4: Install memcached

Install memcached from rpmforge using yum, set it to run at system boot, and start it up.

# yum -y install memcached
# chkconfig memcached on
# service memcached start

Step 5: Configure memcached

You can skip this skip if you want to use a default installation. Otherwise, you can edit /etc/sysconfig/memcached on your server. You will find these settings by default:

PORT="11211"
USER="nobody"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

If you plan on using memcached locally on this same server, you can restrict it to only listen on a local UNIX socket and disable networking.

OPTIONS="-s /var/run/memcached/memcached.sock"

Make sure that if you do use a socket file that the directory is created and owned by the user specified by the USER setting.

# mkdir -p /var/run/memcached
# chown nobody /var/run/memcached

If you plan on using memcached over the network, you may want to install it on a non standard port number of your choice.

PORT="22222"

You can also tune the slab size so that it’s suitable for the size of the data you plan on storing in memcached. The default is 48 bytes. This example sets it to 512 bytes:

OPTIONS="-n 512"

To enable UDP, add this option for the UDP port number you want it to listen on:

OPTIONS="-U 22222"

If you want it to use more memory, you can change this setting to the number of megabytes it should use:

CACHESIZE="64"

On a dedicated 1GB Cloud Server, I set mine to 512MB. This allows a lot of memory for connection handling, and the OS, and guarantees that I won’t exhaust the memory and start running from swap:

CACHESIZE="512"

If you have a busy site and expect more than 1024 concurrent connections, you can safely set that maximum much higher:

MAXCONN="20480"

So here is an example settings file that has a bunch of options changed from the default:

PORT="22222"
USER="nobody"
MAXCONN="20480"
CACHESIZE="512"
OPTIONS="-U 22222 -n 512 -l 10.1.2.3"

Note: The above example says it should only listen on the private IP address 10.1.2.3. This will help avoid unauthorized access to your memcached for setups where you access it from your other Cloud Servers. If you want to limit it only to local clients on the same server, then use “-l 127.0.0.1″ instead.

For more information, consult the man page:

# man memcached

Now save the file and restart the memcached daemon:

# service memcached restart

Step 6: Set up a test application

Now let’s set up a simple PHP application that uses memcached to show that it works. First let’s set up an Apache+PHP environment:

# yum -y install httpd httpd-devel php php-pecl-memcache
# service httpd start
# chkconfig iptables off
# service iptables stop

Now, here you have the option of using the easier installation and using the Memcache[5] class in your PHP script, or use a more complex installation to use the superior Memcached[6] class in your PHP script. This procedure requires that you upgrade to version 5.2.0 or newer of PHP. For this tutorial we will show you the easier way to set up the older Memcache class. In this example, I just download the test script from this blog.

# wget -O /var/www/html/test.php

Now browse to your server (use the IP address if you have not set up and DNS for it yet):

Example:

You will notice that if you visit it multiple times within 5 seconds that you only see the cached result and if you wait 5+ seconds between visits to the page that you will see it insert and fetch new data from the cache.

Look at the source code for the example.

Completion

Congratulations! You’ve set up memcached on a Cloud Server! Now it’s time to begin using it in your application to add speed and scalability to your application and start saving money!

Endnotes:
  1. Client API’s: http://code.google.com/p/memcached/wiki/Clients
  2. video demo: http://www.rackspacecloud.com/cloud_hosting_demos/cloud_computing_cloud_servers
  3. PuTTY: http://www.chiark.greenend.org.uk/~sgtatham/putty/
  4. http://dag.wieers.com/rpm/packages/rpmforge-release: http://dag.wieers.com/rpm/packages/rpmforge-release
  5. Memcache: http://www.php.net/manual/en/book.memcache.php
  6. Memcached: http://www.php.net/manual/en/book.memcached.php

Source URL: http://www.rackspace.com/blog/setting-up-memcached-on-cloud-servers/