Subsections

4. How to use load-balancing for your web site

4.1 Introduction

Having a lot of traffic on their web site is what most webmasters dream about ...

Unfortunately, it also comes with its share of problems. Basically, if requests come in faster than your server can handle them, you're screwed :-)

One way to deal with that is to use load-balancing. This basically means that several threads or processes will be serving the pages. It is only efficient if you have several machines or if your machine has several processors. (otherwise, the same processor will just run several threads or processes, but the overall speed will be the same).

Of course, this means that you have to take care of the data sharing between the threads/processes. One way to do that is to use a database where you store all the data that needs to be shared amongst the processes. All processes read and write to the same database, insuring that they all have the same informations.

There are two ways to do load-balancing with CherryPy. One of them is easier to set up, but only applies to multi-processor machines running a Unix-based OS.

4.2 Generic load-balancing method

Since a CherryPy server is a self-contained process that includes everything to run the web site, it is easy to start as many processes as you want (on the same machine or on different machines). If you start several processes on the same machine, you just have to use a different configuration file for each of them to specify a different port each time.

Then all you have to do is use a simple load-balancer (there are many of those out there) to redirect the requests to your CherryPy servers.

Let's take an example:

Here is what we have to do:

And voila !

4.3 Multi-processor, unix-based machine

The method described in the previous section works in all cases, but if you have a unix-based machine with multiple processors, there is another method for load-balancing with CherryPy.

The trick is to create the socket where the CherryPy server will listen, and then do a fork(). This way, we'll have multiple processes listening on the same socket. When one process is busy building a page, the next one will be listening on the socket and thus serving a request that might come in.

This feature is built in. All you have to do to use it is to use the fixedNumberOfProcesses option in the configuration file (in the [server] section):

[server]
socketPort=80
fixedNumberOfProcesses=3
And that's it.

Note: This method is only useful if your machine has several processors. If not, then the processor will run multiple CherryPy processes, but the overall speed won't be improved.

See About this document... for information on suggesting changes.