Web Performance

How To Improve Website Speed: Caching

Question: What is the fastest request a web browser can make?

Answer: One that doesn’t need to be made.

Picture yourself coming home late from work – you’re starving, you’re thirsty, you need food. You arrive home and immediately make your way to the fridge. You open the fridge door and see that every shelf and container is empty. In frustration you grab your car keys and head out to the nearest shopping mall to fetch some groceries.

In this analogy the fridge is like your web browser; it stores content. The containers within it are like the cache within your web browser. A cache is simply an area to place content so that it can be accessed more quickly. In this context it is keeping the content closer to the user. You shouldn’t have to make the journey to the food store (destination web server) every time you want some food (web content). You should be able to store some of the food in your fridge so that you don’t have to go out so often. Caching is what enables you to avoid making costly journeys over the Internet to retrieve web content like images, style sheets, and JavaScript files. By storing some of the content within the browser cache, less data needs to be transferred resulting in improved page load times and lower bandwidth costs for both the user and the website owner.

If you’re a website owner, you should check if caching has been enabled on your web server. If it is enabled, make sure expiration periods have been declared for your page resources. The expiration period will inform the web browser when to ask for a fresh copy of the resource. The expiration date is relative to when the first request was made, so although the expiration period will be the same for all users, the specific date a resource expires may be different for each user.

Sample Apache config to enable caching with 1 month expiration period:

<IfModule mod_expires.c>
   ExpiresActive On
   ExpiresDefault "access plus 1 month" 
</IfModule>

This config can be placed within the .htaccess file within the root of your website directory.

Sample Nginx config to enable caching with 1 month expiration period:

location / {
   expires 30d;
}

This config can be placed within the “server” block of your Nginx configuration file (the default is “nginx.conf”).

To keep things simple, I’ve avoided the topic of public caches on the Internet. If you would like to learn more about Apache or Nginx caching configurations and how to control public caches (using Cache-Control headers), visit these Apache¬†and Nginx web pages.

SHARE THIS POST
Standard

Leave a Reply

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