No matter how fast your server is, it is always a good idea to use different caching methods. It is important to understand how caching plugins work before you decide what is best solution for you based on how your website works and how much you want to gain with caching.

There are many cache plugins for WordPress. Some are only for a specific cache related use, and some implement all caching methods. If you want to use only a specific caching method, best solution is to get specialized plugin, if you need more – go with all in one solutions. Here on Dev4Press, cache is controlled by W3 Total Cache. Before we go into specific caching methods, there are some server features that can be used and that you may consider installing.

Why do you need to use cache?

Well, most important thing is speed. With one or more cache methods used, your website will be faster without expanding and upgrading the server. Faster website means that your visitors will get to information quicker, and slow websites are known to lose visitors that don’t want to wait long time for page to load. Also, search engines are taking website loading speed into account.

So, using cache will have some impact on the search engine rankings, will save you money on upgrading server and will improve visitors interaction.

Memcached

This is general purpose solution that is made to work across multiple servers for distributed storage of cached data. Data is stored in memory, and oldest values will be removed if allocated memory runs out. This is a good solution if you need to use multiple servers with a lot of memory and the servers are within the same network.

PHP Accelerators

There are several different accelerators available but most popular solutions are: APC, XCache and eAccelerator. They are storing compiled byte-code of PHP scripts into the memory. Normally, every time a script is executed PHP loads it and and compiles it. If you use accelerator, PHP runs pre-compiled byte-code from memory eliminating overhead of recompiling on each request. Here are some test results for loading of WordPress 3.3 with 3 different accelerators in use.

Accelerated WordPress 3.3 Loading

Accelerated WordPress 3.3 Loading

As you can see, use of accelerator for PHP will improve speed about 3 times. Accelerators can run on their own regardless of the cache plugin you decide to use, and you can’t use more than one accelerator in the same time. One more thing that accelerators can do is to work as object storage, and you can cache data also using functions.

Cache Methods

Page Cache

This is the most popular caching method available, and the method that brings biggest speed improvements. Normally, when page is requested, server loads WordPress and everything else needed, code is compiled and executed. So, for each request server needs to load PHP, to load all required files and compile and execute all. Depending on the server power this can take a different amount of resources and time to complete. If you use accelerators on the server this can be faster due to storing of compiled code. But, you can save even more.

Cache plugin is changing how the page is loaded. When a page is rendered with PHP and you get HTML to output to the browser, cache plugin saves that output as a plain HTML file. Files are stored in the folders that mimic the permalinks structure. Each file is stored as HTML and GZIPPED HTML (well, this depends on the plugin you use). Cache plugins need to modify HTACCESS, and add new rewrite rules. These rules check if the user is logged in and if it is not (checked through cookie), HTML file is loaded. So, when cached page is loaded PHP is not used at all and WordPress is not loaded! User gets simple HTML content from a single file and that is it.

This has some drawbacks: cached data remains the same for every visitor. Cache plugins allow you to rebuild cache from time to time to get new content in (once or twice a day typically, on busy websites once every couple of hours). Also, cache rebuild is run when new content or comment is published. Biggest problem is how to get dynamic data into the page even for visitors. Well, most common method is JavaScript and AJAX (my GD Star Rating plugin does that to get real rating values), and some cache plugins allow special tags in the code to markup the dynamic content. There are different implementation, but basic principle is the same for all cache plugin that use page caching method.

Typical gain in speed with page cache can be up to 10 times over normal non cache loading. Combined with some PHP Accelerator (for logged in users), you will get much faster loading. Accelerators have no impact on speed of page cache, since cached pages are HTML only.

Browser Cache

You can speed up loading on the client side only, by setting up proper request response headers, instructing browsers to store data for later and to load data from local storage on next visit. With this, loading of next pages on one website can be much faster if browser stores JavaScript, images or CSS files to reuse.

Through HTACCESS file you can set up handling of different file types by using expire time, cache control, entity tag and compression. Some WP cache plugin allow you to set this up for HTML, JavaScript, CSS and image files.

Minify Files

Typical page is not only HTML to be loaded by the browser. In most cases, to display the page, additional files are linked from the main HTML file, and that includes JavaScript, CSS and images. While you can’t change images that easily (it is always good practice to optimize them though some optimization software before using on website), you can make changes to JS and CSS files.

This process includes compression of JS and CSS files (special optimization of the code so it takes less space) and combining multiple files into one file. So, if you have 5 CSS files loaded on each page, with minify process you will get one file instead. With JavaScript typically you can get one or two files (one in header, other in footer). Same principle of compressing the code can be used for embedded JavaScript and CSS.

CDN: Content Delivery Network

Very important thing to speed up your website is to have some of the files loaded from some other server. If you have 20 images, JS and CSS files loaded for each page, you can set up CDN network that will allow you to have only main HTML file loaded from your server, while all other files loaded from CDN server. There are different methods to this, but must common one is called Mirror. You set up your CDN server (there are many companies offering this service), and you set up CNAME record on your server to point to the CDN server.

Change URL’s to all external files to use new CDN based URL and that’s it. CDN server will automatically pick files requested through CDN URL and store then on many storage locations (usually in different parts of the world), and when next time someone requests a file that is on CDN storage, it will be loaded from there, and not from your website. CDN can help offloading a large amount of traffic to your website generated by images, videos, JavaScript or CSS files.

But, process of changing URL’s can be very complicated, so cache plugins can do that for you automatically. With all plugins this is very easy to set up, and it depends on the CDN service you are using. CDN is not a free service, but the money you invest in this will pay off since you will save on your own server expanding, and you will have much faster website for your visitors.

Cache used on Dev4Press website

Server where Dev4Press is located uses eAccelerator for PHP. And it uses W3 Total Cache plugin with these cache methods active: page cache for visitors, minify JavaScript and CSS, browsers cache and CDN. Minify process helped by merging 7 JavaScript files into 2 files, and by merging 9 CSS files into 1 file. Typically on each page CDN is used to load 20-30 files.

With all the cache optimizations active a home page on Dev4Press.com (according to Pingdom Full Page Test) loads for 0.857 seconds for 71 requests and 660KB of page size. Without any of the optimizations, home page takes 17.5 seconds to load. That is 2042% gain, or 20 times faster with cache!

Conclusion

There are more cache methods available, but in my experience objects or database cache are not worth using due to problems it can cause and marginal speed gains.

How much you will gain with different cache methods depends on many things including server speed and configuration, use of CDN, cache plugin(s) you use… In some cases you can’t use all cache methods, and it is best to experiment until you find what works best for you. There are many plugins available, different CDN services to use. I hope that this article will help you understand how some of the cache methods work to decide what to use.

7 Responses to “How different caching methods work?”

  1. Pothi Kalimuthu | February 16, 2012 at 5:35 pm

    I wish you also had a chance to test and mention about server side caching methods such as Varnish that looks promising, especially on WP multisite enviroments.

    • MillaN | February 16, 2012 at 5:43 pm

      Thanks for the comment. Yes, there are many cache methods available (both server side and plugins), but this article wasn’t about testing them, it is to provide the overview of most popular methods. Testing any cache method in details would require a lot of time to make it objective.

  2. Wordpresslover | February 16, 2012 at 6:31 pm

    Very useful article. Thanks Milla.

    What about a very active buddypress installation where we can’t use usual page cache? Is caching DB queries in RAM useful (e.g. for 5 Mins)? can you suggest a method?

    • MillaN | February 16, 2012 at 6:45 pm

      Thanks! Well, I found all forms of DB queries (or objects) caching to cause more problems than they help with the speed. So far all plugins I used that can accomplish this are not doing a good job with that causing many problems on admin side. I have developed simple DB caching for data for my widgets that is effective in some cases, but using similar approach on a whole page should be tailored to specific use.

  3. Henry | March 23, 2012 at 3:41 pm

    Just tested my website and it loads the homepage in 4 seconds. What caching plugins would you recommend to help speed up page load? I am using the latest version of WordPress.

    Thanks
    Henry

  4. Henry | March 23, 2012 at 3:42 pm

    Argh, make that 6 seconds! Please help :}

Leave a Reply

Dev4Press Plugins Pack

Dev4Press Plugins Pack

Personal$99.00
Business$249.00
Developer$499.00
GD bbPress Toolbox

GD bbPress Toolbox

Personal$30.00
Business$90.00
Developer$180.00
GD Custom Posts And Taxonomies Tools

GD Custom Posts And Taxonomies Tools

Personal$35.00
Business$105.00
Developer$210.00
GD Press Tools

GD Press Tools

Personal$40.00
Business$120.00
Developer$240.00
GD Products Center

GD Products Center

Personal$40.00
Business$120.00
Developer$240.00
xScape Theme Club

xScape Theme Club

Standard$149.00