Tweaking for speed
I’ve made a couple of small speed improvements to this site.
The server can definitely handle the traffic—it’s not very high1—but I thought that it would be nice to speed up the front page. It’s by far the most common entry page for visitors, and it’s the easiest to optimise.
The front page is now using a cached static copy which is
regenerated once every five minutes. Instead of messing around with
file locking, I just use mv
—it’s not exactly atomic
under Linux, but it’s close enough.
1. Retrieve page via the “ugly” interface, i.e. without
mod_rewrite
’s intervention.
2. In case something went wrong, check that the page has been fully
created by checking that the html
or rss
element has been closed. Abort if there’s a problem: the old static
version will continue to be served.
3. Write generated page to temporary file.
4. Move temporary file over the actual static file on the same
filesystem.
The slightly non-atomic part comes in step 4: under Linux’s
implementation of rename
, there is a time when both
the temporary filename and the destination filename point to the
same file. But that’s OK. The important fact is that the
destination filename is pointing to some complete copy of
the page at all times.
I’ve done the same for the RSS feed, too. The result is that the front page and the RSS feed are both being served from static files, and ought to be especially fast.
The next potential improvement is to check the content of the generated and cached pages: if they haven’t changed, the cached page should be left as is in order not to disturb the timestamp.
1 A large portion of my bandwidth at present seem to be taken by some Spanish-language bulletin board, one of whose members has posted my 1024×768 pixel photographs of the Shanghai maglev (along with those of several other people) inline into a discussion. It’s not busy enough to worry about, however, and I’m not really mean enough to make some referrer-checking rules that would replace it with something offensive or perhaps those Internet Explorer-killing JPEGs. At worst, I’m just likely to serve a small generic anti-leech image if it becomes a problem.