Latest LQ Deal: Latest LQ Deals
Go Back > Articles > Jeremy's Magazine Articles
User Name


By jeremy at 2006-06-29 14:10
Features - LAMP Post
Written by Jeremy Garcia

As the ( web site continues to grow, good performance remains vitally important. The August 2004 “Tech Support” demonstrated a couple of ways to optimize the Apache HTTP Server. The next few columns examine ways to enhance the performance of PHP applications.
It’s estimated that PHP, a recursive acronym that stands for PHP: Hypertext Preprocessor, is currently in use on tens of millions of domains. Moreover, PHP is becoming increasingly capable with each release of the language, and is now considered a viable option for enterprise applications. Companies like IBM, Oracle, and Yahoo! now support PHP in a major way.

PHP performance is a topic you’ll find widely discussed on the Internet. In addition to Google, is a great place to look for information on language-specific tweaks, such as disabling register_globals and building the interpreter properly. Here, let’s focus on application-specific tweaks and system-wide enhancements that boost performance.

memcached is a high-performance memory object caching system intended to speed up dynamic web applications by alleviating database load. Available under the BSD license, memcached was originally written by Brad Fitzpatrick for LiveJournal and is available for download from memcached is meant to work in concert with something like the MySQL query cache, not replace it. The two implementations excel at vastly different things: memcached is an object cache, while MySQL provides a query cache.
As you may have guessed, memcached is extremely fast. It uses libevent, which provides a mechanism to execute a callback function when a specific event occurs on a file descriptor, to scale to any number of open connections. On a modern Linux system memcached utilizes epoll, is completely non-blocking for network I/O, ensures memory never gets fragmented, and uses its own slab allocator and hash table to achieve 0(1) virtual memory allocation.

Before installing memcached, download and install libevent (the latest version is available from with the usual ./configure&&make&&make install sequence. Then download and unpack the memcached tarball, running ./configure&&make&&sudo make install to install memcached, too.
With memcached installed, start it using the following syntax:

# /usr/local/bin/memcached –d –l x.x.x.x –u nobody –m 32 –p 11000
This starts memcached as a daemon (–d) on the IP address and port specified with –l and –p, respectively, running as the user nobody (–u), allocating 32 MB for object storage (–m). You should adjust the amount of storage to suit your needs; many memcached installs run with 4 GB. Once you’re comfortable with your startup options, add the appropriate command to your startup scripts.

With memcached installed and running, it’s time to get PHP talking to the object cache. While multiple PHP API’s exists, the one in the PECL repository is recommended. If you’re running a newer version of PHP, installation is as simple as:

# pecl install memcache
If you are still running PHP4, you may need to use pear to perform the install instead of pecl. Or, if you’re more comfortable with a manual install, head over to and download the extension from there.

If you’ve made it this far, you now have the full suite of PHP memcache functions at your fingertips. You can review those functions at The latest version of the memcache PHP extension supports some advanced features, including distribution over multiple servers with loadbalancing and failover, automatic compression, and an extended statistics function.

Unfortunately, this is as far as general instructions can take you. From here, you need to profile your application, see what data it makes sense to cache, and then use the API to do so. Depending on your application, this can range from a trivial task to a major undertaking. The good news is that much of the complicated logic is abstracted away for you, so you can focus on the needs of your specific application. When utilized properly, memcached can make an astounding difference, and it’s well worth the initial work to get setup if you are experiencing any amount of strain on your database servers.

Fully optimizing your entire LAMP stack is no easy task, and proper optimization requires you to look at each component in detail. There is no panacea, and memcached is just one piece of the puzzle, albeit a piece that has worked extremely well for sites such as Slashdot,, and Wikipedia.
Next month’s column looks again at PHP optimization. Coming up, we’ll explore the installation of a PHP opcode cache.


All times are GMT -5. The time now is 05:34 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration