Smart caching for websites and blogs

If you are creating a new website or modifying an existing one, you should always keep in mind that site speed depends on many factors. From the very beginning, you should carefully plan your site architecture, code and overall structure to make sure your site is easy scalable in the future without huge expenses and a great deal of infrastructure changes.

One of the most important performance boosts is the use of caching. In this guide, we will review multiple caching strategies that will boost your website performance and, if done correctly, decrease your site loading times and offer better online experience to your web visitors.

Javascript code.
It’s better to place Javascript code into external JS files and then call from the HTML or PHP code. By using this method, we take advantage of caching. This works great if you have a Javascript code used on multiple pages.

Example:

<script language=”JavaScript” src=”http://www.yourwebsitegoeshere.com/mycode.js” type=”text/JavaScript”></script>

Style sheets.
Move all CSS definitions to an external file, and call the file from your HTML or PHP code.

Example:

<link rel=”stylesheet” type=”text/css” href=”http://www.yourwebsitegoeshere.com/my-own.css”/>

Database queries.
The more database queries you use to generate the website or blog, the higher load it will place on your database server. This will add extra latency, and if you are getting a high number of concurrent web visitors (for example “Slashdot” effect), it may cripple the server, causing a halt. You may want to check out memcached and implement it in your website or blog code. It’s also very important to have the right indexes on the database tables. Limit SQL SELECT queries with LIMIT, you don’t need to select all table rows if you need to display only 10 records from 1,520.

If you have millions of records, a database sharding must be implemented. You can split records in multiple tables or even databases this will provide much lower query latency and greatly speed up your MySQL performance. With database sharding, you can also easily lower index creation time, as well as increase and scale INSERT and UPDATE operations.

WordPress blog caching
By default, WordPress works perfectly fine even for popular blogs when there are no high bursts of concurrent connections . If you get Dugg, the standard WordPress install will not scale, and most likely the server hosting your blog will be inaccessible. WordPress uses PHP to generate output on the fly thus, all pages are generated in real time, including data from the database.

If your content doesn’t change that often, why generate files on the fly all the time? Correct. There are wp-cache and wp super cache plug-ins that offer WordPress caching. In other words, the plug-in will generate a static file on the server hard drive and serve it to visitors without querying the database at all. When the content changes, the static cache file is removed and a new file is generated once again.

You can also offload static content to Content Delivery Network (CDN) – see below. Offload static content section, or serve static images from a different server that is optimized to serve static content only.

Memcached
Memcached is perfect for file, data and SQL query caching that is heavily used and accessed. Caching is very good and recommended for content that is not so important to be generated from database back-end in real-time. Examples include recent comments, statistics, logged-in users in the system and news links.

Please keep in mind that there’s little of benefit to caching MySQL queries or files in memcached if the output is dedicated for one user only for example, a customer control panel displaying client-only settings or dynamic content that depends on cookies or very specific GET values.

Offloading static content
From the very beginning implement static content separate URL from your main site. Separate static content from dynamic content serving URL – for example, with subdomain http://images.yourwebsiteishere.com/ so that all images are served from this URL.

At the very beginning, you can have http://images.yourwebsiteishere.com/ on the same server to save on hosting expenses. When your site or blog grows, this will allow more flexibility in the future, and you can then point this URL to a Content Delivery Network like Akamai or ValueCDN, serve images from another server and so on.

Apache mod_expires
You should implement the HTTP protocol expire header feature to lower the hits for content that doesn’t change very often, including images, CSS, Javascript and even static HTML content. You need to compile Apache with mod_expires module for this to work.  For Apache2, you need to add the --enable-expires switch to ./configure line.

Next, you activate it with the LoadModule expires_module modules/mod_expires.so line.

Then, add the following lines to the Apache configuration file (usually httpd.conf). You can also add this to .htaccess but from the performance point of view, it’s better to have in httpd.conf.

<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/jpg “access plus 1 weeks”
ExpiresDefault “access plus 1 days”
</IfModule>

ExpiresByType image/jpg “access plus 1 weeks” tells the browser to reload the file with the document type image/jpg from the server one week after it was first accessed.

ExpiresDefault “access plus 1 day” tells the browser to reload default files from the server one day after it was accessed.


Comments

  1. Kai says:

    More detailed info on mod_expires you can find in official Apache documentation

    http://httpd.apache.org/docs/2.0/mod/mod_expires.html

Leave a Reply