Writing Load-Balanced CGI Scripts
Balancer-aware CGI scripts
In normal operation, the Zeus Load Balancer will function completely transparently to the webservers that it controls. However, there are occasions when programs or scripts that run on the webservers should have control over which backend machine is used for each request.For example, consider having a cluster of webservers hosting one medium-size website. Each backend server has a local copy of the website on its hard disc. If the complete website is static data, then everything will work fine.
However, suppose some kind of shopping-cart system is to be added to the site - the webserver must track a user as they browse the site, keeping track of the products that they have put into their shopping basket. If the shopping-cart code stores this information locally, then there will be problems. Picture the following two steps:
This backend has no idea that the user has already got product 'A', leading to confusion and mishandling of data.
- A user sends a request to get product 'A'. The Balancer sends this request to backend server 1.
- Next, the user tries to get product 'B'. The Balancer happens to send this request to backend server 2.
Solutions
Of the three solutions, the first is the most efficient method. If there are any URL mappings in place, each HTTP request must be checked against each mapping. If the mappings are complex, this may slow down the performance of the Balancer. Also, supporting the cookie feature means that the server-side applications can still be distributed over all the backend machines - each machine will get some transactions and they always get a complete series of transactions.
The CGI or server application that provides the shopping cart can be made cluster-aware. In the HTTP response, a web server can specify that all future transactions by an individual client must be redirected to a specific backend webserver. This is achieved by the application creating a cookie named 'X-Zeus-Backend' and setting its value to the name of the backend server to be used.
The next time that the Balancer receives an HTTP request by this client, it will detect this cookie. Reading the value, it will know which backend server should be used to handle the request, and forward it on appropriately. In this way, all linked transactions can be pushed onto the same backend server.
The Load Balancer supports mapping specific URLS onto one particular backend server. URL mappings are explained fully in the 'Mappings' section. In brief, if all the shopping cart transactions are performed in a subdirectory of /shopping, and must be handled by the backend machine 'mercury', you must create the file $ZEUSHOME/balancer/mappings with the following line:
venus ^/shopping/Be sure to put a copy of this file in each Load Balancer installed. The Balancer will not need to be restarted in order to handle the mappings - they will be detected automatically.Share the shopping cart data between all the cluster machines, perhaps over NFS.