HTTP load balancing and Web serving on a budget with Perlbal.
The last few â€œTech Supportâ€ columns have demonstrated how to optimize your PHP installation for maximum performance. From memcached, to decreased database load, from APC for opcode caching, to custom compilation tips, you have many options to boost the throughput of your Web server. A
t some point, though, you may find that your site outgrows even the best optimizations. When that happens, youâ€™ll need to scale your application in other ways. This article focuses on scaling your Web servers by spreading the load to multiple machines.
If youâ€™re a “Web 2.0″ startup flush with venture capital cash, you probably can afford a proper load balancer from the likes of Cisco or F5 to help multiplex Web traffic among many servers. However, if youâ€™re a Linux community site, purchasing expensive hardware is likely not a viable option.
An easy multiplexing solution is round-robin DNS, but as with most easy solutions, itâ€™s one that offers little flexibility and has many limitations. Depending on your application behavior, you may be able to utilize Squid as a caching, reverse proxy. But what if your setup isnâ€™t cache-friendly?
Enter Perlbal, a Perl-based, reverse proxy load balancer written by Danga (who also wrote memcached). Available for download from http://www.danga.com/dist/Perlbal/, Perlbal is released under the same license as Perl.
Perlbal is a single-threaded, event-based server that enables HTTP load balancing, Web serving, and a mix of the two. Perlbal maintains a pool of backend connections and does intelligent load balancing based on what backend connections are free for a new request.
You can install Perlbal and any missing dependencies via the Perl CPAN.
$ sudo perl â€“MCPAN â€“e shell
cpan> install Perlbal
Or, if youâ€™d prefer not to use CPAN, you can download the Perlbal source and build and install the software via the standard procedure for Perl modules:
$ perl Makefile.PL && make && make test
$ sudo make install
(If you choose not to use CPAN, youâ€™ll also need to manually install the following Perl modules: BSD::Resource, Danga::Socket, and Sys::Syscall. Optionally, you can also install Perlbal::XS::HTTPHeaders, IO::Socket::SSL, and IO::AIO.
A Balancing Act
With Perlbal now installed as /usr/bin/perlbal, itâ€™s time to move on to configuration. Letâ€™s look at how to enable HTTP load balancing.
By default, Perlbal looks for the configuration file /etc/perlbal/perlbal.conf. Hereâ€™s a sample:
CREATE POOL my_apaches
POOL my_apaches ADD 10.0.0.10:8080
POOL my_apaches ADD 10.0.0.11:8080
CREATE SERVICE balancer
SET listen = 0.0.0.0:80
SET role = reverse_proxy
SET pool = my_apaches
SET persist_client = on
SET persist_backend = on
SET verify_backend = on
This simple configuration listens on port 80 of the host machine and forwards all requests to port 8080 on 10.0.0.10 and 10.0.0.11. (Remember, since Perlbal does intelligent load balancing, you wonâ€™t need any â€œweightingâ€ numbers that you may have seen in other solutions.)
Perlbal also has built-in statistics and monitoring to monitor CPU usage, uptime, total requests, and a variety of other useful metrics. To enable statistic collection and monitoring, add the following to your configuration file:
CREATE SERVICE mgmt
SET role = management
SET listen = 127.0.0.1:60000
You can now start Perlbal as a background service:
$ sudo /usr/bin/perlbal â€“â€“daemon
Perlbal is extensible: you can create per-service and global plug-ins that can override many parts of request handling and behavior. By writing a custom plug-in, you can adapt Perlbal to your exact environment.
Perlbal can handle much more complex setups. Perlbal also supports SSL and virtual hosts, allows internal redirection to a file or URL, has configurable header management, and a variety of other advanced features and options. Start by browsing the docs and config directories contained in the download.
The docs directory gives a comprehensive list of supported configuration directives, shows you how to reconfigure Perlbal on-the-fly, introduces the concept of â€œreproxying,â€ and even provides information on hacking the Perlbal code and writing custom plug-ins. The config directory includes sample configuration files for everything from SSL support, to a fully-dynamic virtual host setup.
If youâ€™re on a tight budget and donâ€™t have a cache-friendly application, Perlbal may be the ideal solution for you to conquer your Web traffic.
is the founder and administrator of LinuxQuestions.org, a free, friendly, and active Linux community that filters its email with SpamAssassin. Please send questions and feedback to firstname.lastname@example.org.