26
OCT
Speeding up our clients sites with caching
Having recently been working on our new PHP framework, I decided to integrate caching in a bid to improve the performance of our clients websites. I'm seeing some amazing speed improvements!
For those that don't know, let's first look at what happens when you request a page from a database driven website...
First, the server recognises the page you want, and jumps to that page, if the page is in a CMS, it's usually a little more complex than this. Within the page there's likely to be logic that talks to a database to retrieve the content of the page. It's sometimes normal for a page to have little blocks of content in columns, for example, latest blog posts, latest news, so each of this need to be loaded from the database too. As the site does more work and makes more connections and queries to the database, the amount of memory (RAM) the page uses increases. The final HTML of the page is then returned to your browser.
As a test case I looked at a fairly large site we developed for a client a couple of year ago. It's fully database driven and multi-lingual. I added small snippets of code to the top and bottom of the index file(which serves all pages) which would report back how long the page took to generate. I was shocked to find the page took 1.98 seconds to render! I monitored memory usage too, and that peaked at an average of 1.2MB, with some pages using over 3MB!.
The results above probably don't seem too bad at first glance, but imagine you've just launched a competition or promotion, and you've gone and announced it on Twitter. Your site sees a sudden increase in traffic, and for every visitor that visits the site, it going to take 2 seconds of processing time to render the page and 1.2MB of memory. Multiply that by a thousand or so visitors and you could easily see the site generating errors because there isn't enough memory.
The solution... caching. Caching involves creating a script that loads your page as normal, but saves the HTML output to a cache file on the server, the next time the page is loaded, the script checks if a cache file exists, and if it does, it serves the HTML from the cache file. Why is this so good, simple, because it's serving plain HTML, the page doesn't have to query the database or parse any complex code, it doesn't have to perform an complex calculations, and the result is a drop in script processing time, and a reduction in memory usage.
I went back to the site I used in the example earlier, and modified the caching class from our new framework. I added the caching script to the site and tried to render the same page again. The first time I rendered the page, it still took 1.98 seconds, and memory usage was the same, so adding the caching script hasn't made a noticable difference there. Load the page a second time (after it generated a cache file the first time) and the results are astounding! it takes 0.00 seconds of time to process the same page that previously took almost 2 seconds, and memory usage drops to sub 400KB, down from 1.2MB.
So, the conclusion of all of this is that my tests on a real website revealed a speed improvement of almost 100%, and it used around 67% less memory. Why is this so good I hear you ask? Now your website pages render much more quickly, so improved loading times for your visitors, but because memory usage is much less, the site can now handle almost 3 times more users than it could before.
Our new framework is going to benefit our clients hugely, not only do their projects get the benefit of quicker developer times, but now much improved loading times and less server load. All adding to the experience for the end user.
Just to note, caching your site is a little more complex that I probably make it sound, depending on the type of site you've got. A simple content managed site that serves page content from a database could probably be configured to create a new cache file every couple of hours, but with more complex sites that require users to login, and contain forms or live search results would need to be configured so that certain pages don't cache so that the live results are always displayed instead of caches data.
Comments
Be the first to post a comment...
