Site Speed Improvements with OPcache and PageSpeed

If you’re running a fair-to-mid sized site with PHP code running the back-end, you’ve probably noticed the intense amount of activity that’s going on to serve a page to a visitor. PHP files get loaded from the hard drive by the process manager, compiled, run and then immediately thrown away for the next process in line. All of that takes time, which slows down our applications, which frustrates our visitors and they leave the site before finding the resource they needed.

If you’ve got RAM to spare you should think about mounting some of it as TMPFS and park the cache files there.

PageSpeed compresses and combines various resources like Javascript, CSS, images and the like and stores the optimized versions in a cache. Store that cache on a RAM-based storage and now you’ve gained the double benefit of faster I/O and smaller payload of data. We wrote about PageSpeed and getting the most out of it in a previous post.

Zend OPcache takes PHP file requests and runs them through the compiler and then stores the bytecode in a cache. The next request for that code is served straight from the very fast and efficient cache and processed almost instantly. If you’re running PHP 5.5 or higher, you’ve most likely already got the OPcode extension installed, but it’s turned off by default.

Locate your php.ini file and update (or add) the following commands:

opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60

Restart the web server (or the fast CGI/FPM process) and you should now start seeing the cache being loaded with files as they are requested by your visitors.

These turn on the opcache, allocates 128MB for it, sets the max files to cache at 4000 and finally sets the frequency to check for updates at 60 seconds. Those are just the most basic settings to specify for now, but the full list can be found here.

RAM storage of cache files improves the i/o time to load the cached items and and cache maintenance is made faster as well. Here is a small before/after sample of the CPU usage stats for our primary server:
CPU Stats

Starting right after the update to using OPcache and moving the PageSpeed cache to ram, CPU loads dropped from the 35-99% usage to 15-50% max usage.

This entry was posted in Technical Talk and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *