Welcome Sign InSign Up Now! It's Free!
July 5, 2009 Contact MeClient Support

Use a Cache to Improve Website Performance

Tuesday, November 4th, 2008
by Gary Keorkunian
Posted in Performance Tips, Web Admin | 1 Comment »

Over the past week or so this site has been experiencing some issues with server errors and connection timeouts.  I know that my traffic has been increasing pretty steadily. However, I did not receive a jump big enough to account for the sudden loss of service.  Upon inquiring with my host’s tech support, I was informed that I had been exceeding my allocation of MySQL database resources.

GARA.com is in fact a very dynamic site.  Nearly every page contains some element that is updated on a regular basis, such as the Recent Free Software Updates, Current Deals and, of course, the Blog.  But, again, it was working just fine a week ago.  So my troubleshooting began.  It wasn’t long before I realized that I had turned of the cache plugin that I use within the blog.  My intent was to do this temporarily as I was testing changes to my template.  Unfortunately, I forgot to re-enable it.  The result was a sudden spike in hits against the site’s MySQL database and the resulting server errors.  Once I turned it back on, the problems quickly receded.

INTERESTING NOTE - My host provides me with unlimited disk space and bandwidth.  However, if they limit MySQL execution, doesn’t that in effect put an artificial limit on my traffic and bandwidth?

What is Caching and How Does it Improve Performance?

When visiting a site that runs a database driven content management system - like WordPress, for example - every page request will in turn make a request to the database.  Sometimes, several database queries are executed.  Once the data is retrieved it is combined with a template and the resulting HTML page is delivered to your browser.  This is a lot of work for a web server to do.  Fortunately, most of it is not necessary.  Since blog posts rarely change once their posted, regenerating the HTML page for every visitor ends up being work that does not need to be done.

Caching tools work by using a very creative but simple technique.  The first time a page is requested it is generated the normal way described above.  Then, in addition to delivering the page to the visitor, a copy of it is stored in a special cache directory on your server.  The next time a visitor requests the same page, the caching tool simply delivers the already generated HTML page.  This eliminates the need to execute as many database queries as well as the need to repeatedly generate the same pages.  The result is reduced server load and a more pleasant user experience.

WordPress Cache Tools

Because WordPress is a PHP/MySQL based content management and blogging system it is an excellent candidate for caching.  Fortunately there are two very good plugins that serve this purpose.  The first, WP-Cache, has been around for a while.  It is the one that I use on this site.  It is mature, very stable and works wonders on site performance.  It works well in most WordPress installations and comes highly recommended.

The second, WP Super Cache, was branched from wp-cache and includes additional features.  It is currently the top featured plugin in the WordPress Plugin Directory.  WP Super Cache does require that you have access to your .htaccess file and other Apache mods.  It is also pre-release - version .0.8.4 as of this posting.  Therefore, it may not be suitable for every webmaster.

There are some situations where certain parts of a page should not be cached.  For example, you may be using a widget that updates frequently.  Fortunately, both of these plugins support a feature that let’s you identify specific portions of a template that should not be cached.  See their documentation for more details.

If you are using a different CMS or blogging platform check for the availability of caching tools.

Beyond the Blog

Caching is important not just in your CMS or blog but also on any page that retrieves content from a database.  This is particularly true when that content does not change very often.  One example on this site is the Recent Free Software Updates that appears in the sidebar of most pages.  This information is updated daily on average, but certainly not every time the user navigates to a new page.  So again, going out to the database to get this info for each page view would put a huge load on the server.

To remedy this I created a very simple caching method.  Whenever a new update is received, the MySQL database is updated and then a script is executed to create a static XML file on the server.  Every time a user navigates to a new page the data for the Recent Free Software Updates is extracted from the XML file.  The effect is similar to that of WordPress delivering an already generated HTML file in its cache:  a significant reduction to the load on my server.

If your site has custom features that make use of database-driven content you should examine its architecture to see where you can make similar improvements and optimizations.

Conclusion

We create dynamic database driven web sites because they help us create and maintain fresh content.  The result is an audience that grows and comes back frequently.  However, a site that times out or serves up 500 errors can be counter productive.  To improve your site’s performance and keep your audience growing it is important that you implement effective methods to cache your dynamic content.

One Response to “Use a Cache to Improve Website Performance”
  • Altis Lo (Beaulife) Says:

    Good explanation between WP-Cache and WP-Super-Cache which have been confusing me long time. I think it is high time for me to apply either one in my Best Buy And Idea Blog (http://www.bestbuyidea.com/). Thank you. I appreciate it.

Leave a Reply, Question or Comment

Security Code: