Web Hosting

OpCache vs Memcached: Caching Explained

 

When first delving into the world of Caching people often ask OpCache vs Memcached – which is better? The answer is they are completely different and you should be using both. However, these aren’t the only types of caching that exist. In this article I’m going to briefly explain the different types of caching that you need to be using to optimize your website’s performance in really simple terms (I hope) .

Years ago I installed WP Super Cache on this WordPress blog. I had nothing but issues with it and ended up disabling it and forgot about caching. I dismissed it as irrelevant for a company blog. But with the launch of our new site I knew it was time to rethink that position. Sure, it’s irrelevant if you don’t post but if you’re going to have a blog not posting regularly can only harm your brand. Plus it’s such a convenient way to pump out content and drive potential customer traffic to your site.

OpCache vs Memcached?

A few months ago one of our Managed Dedicated Server clients asked me about caching for his busy WordPress site. He said he had narrowed it down to “OpCache vs Memcached” and which did I recommend. This being my preferred caching stack I soon set him straight and advised him how he could easily take advantage of both. But I realized that most webmasters just didn’t understand how caching works so I’m hoping this article will shed some light on the subject. I decided I should probably research and test additional caching tools but to be honest the caching landscape hasn’t changed much recently. A lot of caching tools that used to be popular don’t have any active development.

And what further complicates the matter is the fact we run Plesk and cPanel servers and some types of caching work well on one and not on another. It’s hard to find a one size fits all solution but I believe we have.

Basically if you run a static HTML then all you need to do is Leverage Browser Cache. If your site has a dynamic PHP frontend with generated content you need your leverage browser caching, some server caching which is handled by Nginx and a bytecode cache like OpCache which is compiled into later versions of PHP. And if you have an active database you need a key/value cache store called Memcached. Plus you should be on PHP 7.x with your web server running HTTP-2. The eyes of the average webmaster are glazing over right but it’s not that hard to understand so bear with me!

(1) Browser Caching

Leverage Browser Caching I have covered in another post so I’m not going to get into that here. You just need some simple .htaccess or web.config rules. This controls how a user caches your site – every site needs to have this, no exceptions.

Nginx(2) Server Caching

All our servers run the Nginx proxy web server. It sits in front of Apache and accelerates content and application delivery. Nginx also facilitates high availability and scalability for the busy websites with its abiity to handle a lot of concurrent users. It also handles caching at server level. Nginx caches static files in a temporary directory and serves them from there rather than requesting from the file system. This is a small part of what Nginx so fast compared to Apache. If you’re running a website on one of our Linux servers that magic is always happening.

So that’s all your static content caching dealt with. Done.

 

(3) Application Front-end Caching

PHP code can also be cached at the bytecode level. When a PHP script is compiled by a web server some of that execution time is spent formatting the code into something a machine can read. With a bytecode cache like OpCache the code is compiled once and stored in the server’s memory. This precompiled code is then served on each script request instead of having to compile and parse on each script request ergo faster sites. OpCache is available with PHP 5.5 and higher and works with no extra configuration necessary in your code. If you’re not yet using PHP 5.5 or higher then make the change asap. From a security standpoint you should be using PHP 5.6 at least (or PHP 7 if your apps support it).

If you can’t upgrade your PHP due to some script dependence on on PHP 5.3 or lower then you need to dump that script. PHP 5.5 has been available for since June 2013 – there is really no excuse. :)

(4) Database-level CachingMemcached

Last piece of the caching puzzle is optimizing your database queries by storing query strings and database objects that come database calls and page renders. The best engine for this is Memcached and it is fully supported on all our shared servers. It significantly reduces server load but it does require a lot of memory. Therefore using Memcached on a busy database driven website might not be compatible with a shared hosting environment. But if you have the memory available it will drastically speed up a WordPress site for example. So you can see there is no “OpCache vs Memcached” argument. They do different things and you want to use both if you can.

In Conclusion

I think I saw some stats somewhere that said 25% of new websites are built solely with WordPress. Of the remaining 75% I’m sure a large percentage use WordPress for a company blog. Coming in second is Joomla, Drupal and Magento. So it’s highly probable that you need to use all 4 caching methods. In addition to this you moving to PHP 7.x should be part of your website optimization strategy. All applications run much faster with PHP 7 compared to PHP 5.6. According to Zend Magento’s execution time is twice as fast with PHP 7 with a 30% memory consumption reduction. Drupal runs 72% faster and WordPress is twice as fast! I’m switching HostNexus to PHP 7 as soon as I’ve upgraded our Connect billing and support portal to the latest and greatest version that supports it.

If you use WordPress you can take advantage of all caching types with one powerful WordPress plugin called W3 Total Cache. It’s a complex plugin but armed with the knowledge above you’ll be able to drastically speed up WordPress and also understand what you are doing. My next post will cover your basic configuration for W3 Total Cache.

Happy Caching!

About the author

Laurence Flynn

Hey! I'm Laurence, hosting industry veteran and entrepreneur, obsessed with web performance. My aim is to build the cheapest and fastest Optimized WordPress Hosting platform available today. Our back-end systems include Nginx and Redis combined with PHP 7, FPM and MariaDB to deliver maximum performance. Our front-end UI is powered by the beautiful Plesk control panel to deliver a smooth user experience. All secured with Imunify360, artificial intelligence and machine learning. Connect with me on LinkedIn.

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.