Speed up your PHP applications with memcached
How to reduce load on your database by minimising the number of database requests you need to make
The ever increasing complexity of web applications has meant the need for larger and more complicated database schemas. While there are efforts to improve high input/output apps by using non-relational databases such as MongoDB and then pairing them with a non- blocking language such as Node.js, these are still in their infancy, and in many cases tried and tested languages, frameworks and databases are best for the particular job. The number of database calls you make largely depends on your app, and it’s obviously possible to reduce these as much as possible within your code and by increasing server RAM and CPU power.
There are other more efficient (free!) ways of improving app performance, and one of these is by using memcached. Memcached works by storing objects or strings within memory, ready to be used again quickly, without the need to hit the database again. Uses include storing user session data. Memcache is available to be used in many languages, but here we are looking at deploying it using the most common dynamic web scripting language: PHP.
LAMP stack running PHP 5.2.0+
Step by Step
The first step is to see whether or not we have the ‘libevent’ library installed on your development machine. To do this, open up a terminal window and type in:
If the file is found, then you are good to go.
To download libevent, in the terminal enter ‘wget https://github.com/downloads/ libevent/libevent/libevent-2.0.20-stable.tar.gz’ and then wait for it to be grabbed from GitHub. There is also an alpha version from here:
Once that’s finished downloading, we can unwrap the tarball.
In the terminal, enter ‘tar xfz [your downloaded file name]’ (for example, ‘tar xfz libevent-2.0.20-stable.tar.gz’). This will leave you with a folder.
Change into the directory using ‘cd libevent-2.0.20-stable.tar.gz’ and then we can start the build process with a configure command. Enter:
…which creates our makefile ready for the build command.
Now we can run our ‘make’ command.
This might take a little while depending on the speed of your machine. Finally, you can enter:
sudo make install
…to finish off the install process. Assuming there were no errors, libevent is now installed.
Then we can create a symlink to the library, so that is can be accessed from the usr/lib directory. Back in the terminal, enter:
sudo ln -s /usr/local/lib/libevent- 2.0.20-stable /usr/lib
Next we can download memcache.
To download memcached, enter:
wget http://memcached.googlecode. com/files/memcached-1.4.15.tar.gz
…which is the latest version. It might be possible to grab it using your favourite package manager, but this way we will make sure we are using the latest version.
As before, we need to untar it using ‘tar xfz memcached-1.4.15.tar.gz.’
Follow that up with ‘cd memcached-1.4.15’, where we are then ready to run the ‘./configure’ command again for memcached.
Then enter ‘make’ and once that’s finished, we can run our ‘sudo make install’ to install memcached. After this has finished and you have not received any errors, we are ready to start the memcached daemon.
To start memcached we need to give it a few parameters. The -m switch determines how much RAM you want to be available; the minimum is 48MB. The -d switch sets whether it will run daeomonized, and the -v sets the verbosity of output; more -v switches means increased output.
The -p switch sets the port, allowing multiple instances to run on one machine. -l sets the IP; -u the user. Here we’re running as root. An example start-up would be ‘memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211 -v -v’.
To make our life easy we will install the extension using PEAR. If you hae got it installed already you can skip next few steps. Enter:
wget http://pear.php.net/go-pear -O go-pear.php