Today’s post covers the joys and benefits of opcode caching. If you have a very active site, this caching method can provide a great deal of relief to your servers by significantly reducing load averages and CPU utilization.
How does it work?
It’s quite simple, actually. Without caching configured, each time a PHP-based page is requested by a user, the server gathers all of the needed files and “compiles” them into a result which it can understand and deliver. This result is known as “opcode”.
The technique of opcode caching preserves this generated code into a cache so compilation only needs to happen once and then can be used over and over again for numerous requests by numerous users.
Several PHP caching choices exist including APC, Zend, and XCache. I’ve chosen eAccelerator based on numerous reports such as this one which show its distinct performance advantage and also in large part due to the personal recommendation of seasoned server architects such as RackerHacker.
Debian Lenny doesn’t include an apt-get package for eAccelerator so we’ll need to compile from source. Luckily, this isn’t as tricky as it sounds. Here we go!
Download and extract:
wget http://bart.eaccelerator.net/source/0.9.6.1/eaccelerator-0.9.6.1.tar.bz2 tar xjf eaccelerator-0.9.6.1.tar.bz2
Compile and install:
cd eaccelerator-0.9.6.1 apt-get install php5-dev phpize ./configure make make install cp eaccelerator.ini /etc/php5/apache2/conf.d/eaccelerator.ini
php -v PHP 5.2.6-1+lenny8 with Suhosin-Patch 0.9.6.2 (cli) (built: Mar 14 2010 09:07:33) Copyright (c) 1997-2008 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator
That’s it! My load averages have dropped significantly on the load-balanced web servers I manage. We went from 4-5 with spikes to 25 down to roughly 0.5 with spikes to 2. As always, reach out to me via email or in the comments section if you have any questions.