Slow web servers, and how to speed them up

In a world where a large portion of internet users are on fast broadband connections, we sometimes come across slow-loading web sites. We’re quick to notice when it takes more than a second or two to load, but what do you do when it’s been over a minute since you clicked the link ?

I frequent a particular site called UsedOttawa.com, where people from my area post their junk for sale. You could compare it to Craigslist, but with a somewhat nicer interface. Any active content on this site usually takes a minute or two to process, that’s if it goes through at all; sometimes it just barfs. Now this site is an ad-supported, commercial venture. I have to wonder what the hell the admins are doing, and why such a small site is left to crumble under the pressure. They claim to be serving 1 million hits daily, which works out to an average of 12 hits per second. It’s safe to assume they serve around 50-60 pages per second during peak hours. It’s no small number, but any decent web server should be able to handle that without breaking a sweat.

The Problem.

More often than not, the problem lies in the code itself. Database queries can stall the script while the data is being fetched. Just running a script consumes a fair chunk of processing time.

Another cause for server lag is the users themselves. If someone has a slow connection and it’s taking their modem a long time to fetch the page, they are tying up a server process for the entire duration of their download. To illustrate, let’s suppose the server can accept up to 50 connections at any given time. If half of those are dial-up users and they take up 30 seconds to download a page, it leaves only 25 connections free to serve other people. This can quickly snowball if you have a large ratio of dial-up users. You could realistically find yourself stuck feeding 50 slow users while your server sits idle waiting for a connection to become available. This issue has been greatly alleviated with Apache 2.0, but if you’re still running Apache 1.3 (many people are), you should look into lingerd.

The Solutions.

There are solutions to both of these problems. For lagging scripts, a good script cache can give your site a healthy boost. For PHP there is the official Zend Optimizer, but there is also an open source alternative called eAccelerator, which is what I use. The difference the accelerator makes is dependent on your usage patterns. On my blog, I’ve seen a 600% performance boost in PHP scripts, which is huge.

A more complex caching system involves using Lighttpd (pronounced “lighty”) as a secondary web server. Lighttpd, as the name implies, is a lightweight web server built for speed. It is somewhat less featureful than Apache, but the speed more than makes up for the lack of special features. The magic solution of course, is to use both Lighttpd and Apache together, each one handling its specialty. Apache handles the heavy scripts, and forwards all the static content to Lighttpd, such as images or downloadable files. The big difference between the two is that Apache spawns a separate process for each request, while Lighttpd serves everything from a single process, significantly reducing per-connection overhead and memory usage.

Lastly there is a very unique set of patches to Apache called the “Accelerating Apache Project”, which was a special project backed by Silicon Graphics Inc. that uses specialized techniques to greatly boost Apache’s performance. Unfortunately it is largely unsupported as the Apache Developers refused to incorporate the speed tweaks and SGI abandoned the project. It lives on SourceForge where volunteers are free to pitch in. If you really want to squeeze every last bit of juice out of Apache, you should give this patch a try, although it’s definitely not for the faint of heart.

If those tricks still don’t make your site fast enough, you have to start thinking about clustered solutions, which means you’d better stock up on migraine medication. You’ll be looking at multiple database servers in master/slave configurations, multiple web servers with a front-end load balancer, heavy-duty SAN storage equipment, and the hardest part is usually having to adapt your scripts to the distributed environment. Chances are, if you’ve reached this level, you can probably afford to hire a full-time server guru to make it all run smoothly. It certainly isn’t a task for the casual webmaster.

Both comments and pings are currently closed.

2 Responses to “Slow web servers, and how to speed them up”

  1. Kingneutron says:

    Some folks also use Squid as a frontend to speed up their webserver.

    See:
    http://www.vmware.com/community/thread.jspa?messageID=635391&#635391

  2. […] web pages these web servers might prove hugely useful. Some people even mix the two together- this blog post mentions a way to combine lighttpd and Apache, using the former for static content such as images […]