Serving Cache using Lighttpd
Published: 2008-11-15
Lighttpd is widely know as a powerful lightweight server that is particularly useful at serving static content. With this in mind I migrated the responsibility of serving images on alltradeart.co.uk over to lighttpd. Sounds simple enough but alltradeart generates images on the fly. The first time an image is accessed it is processed and saved into cache, further requests have to processed by PHP to check the cache an then the image output. The method is very flexible in that I can change the image size of gallery without having to batch process all the images uploaded by the artists - the problem is that it's very intensive on the server to load each image through a script.
The new process will have the image request processed through the existing PHP scripts and Apache for the first time then any subsequent requests served directly from the cache using lighttpd.
Setup...
alltradeart is designed to cache a system generated image in /home/alltradeart/tmp/cache/TYPE/ID/SIZE. I setup lighttpd to serve images from /home/alltradeart/tmp/cache on port :81 and created a 'server_resource' method to serve generate the correct resource pointer.
public function serve_resource($file, $dir = CACHE_DIR){
if(file_exists("{$dir}{$file}")) return STATIC_SERVER . $file;
else return HOST_SERVER. $file;
}simple! As a result of this simple change alltradeart's page load time has been massively reduced and has freed up massive amounts of server memory. Benchmarking the serving of an image using ApacheBench saw an increase in rps from 9.2 via the php scripts to 3200 accessing the file directly via lighttpd.
RECENT ARTICLES
Lightweight blog created on Ruby/GAA/Datastore stack
Put this together a month or two back but never got around to replaci... read more
Using Node JS and Couch DB Stack for Web Dev
With the recent hype surrounding Node.js I thought I'd better get my ... read more
Campaign Monitor API Using PHP and SOAP
Campaign Monitor has a fairly comprehensive API and support docs.&nbs... read more
Google Maps Snippet
Absolute bare basics when it comes the Google Map api but a snippet o... read more