From time to time you may enouter the situation where you have a very well written web service which works great for 95% of time but you have some spikes where there is more traffic on those services as usual and much more than one instance can handle.

One approach is to look for some ways to optimize things so it could also handle those spikes. If an optimization results in being able to handle the traffic spikes it would be the ideal situation as this would keep things simple and “simple” is something I really like ;-).

But sometimes there is not much we can do about the performance of a single web service instance any more. But if one instance cannot handle things then maybe some more instances can.

Welcome to the land of load balancing!

Load Balancing

Here some definition for load balancing from Wikipedia (yeah … shame on me for using Wikipedia as a reference ;-) ):

In computing, load balancing refers to the process of distributing a set of tasks over a set of resources (computing units), with the aim of making their overall processing more efficient.

So we have a set of tasks (multiple HTTP requests) which we would like to distribute over a set of resources (multiple instances (jobs) of a web service).

Starting the web service program in multiple jobs (on a different port for each job) is really no problem on our beloved IBM i. There are so many ways to skin this cat like SBMJOB or using autostart jobs in a subsystem or … . Choose the variant which best fits your environment.

So now we have the set of resources set up and we can easily generate our set of tasks (HTTP requests) with tools like k6 or Gatling. The missing piece of the puzzle is the middleware for the load balancing itself.

But you don’t have to look any further. One possible solution is sitting right in front of your nose. It is included in every IBM i installation and is installed on millions of servers.

Apache HTTP Server!

Apache HTTP Server

The Apache HTTP server project support its features by including and enabling modules. All modules of the Apache HTTP server has been compiled as ILE modules and some of them are bound to the service program QHTTPSVR/QZSRCORE. At least this service program contains the HTTP server modules which are needed for setting up load balancing for the HTTP server.

The necessary modules are

  • mod_proxy
  • mod_proxy_http
  • mod_proxy_balancer
  • load balancer scheduler algorithm module like mod_lbmethod_byrequests

The modules will be loaded with the LoadModule directive. The syntax is very simple:

1
LoadModule <module identifier> <file>

The module identifier of the module can be found in the Apache HTTP documentation for modules.

Most tutorials and documentation for the Apache HTTP server is for the Linux operating system. The corresponding object of a service program in linux is a shared object (suffix .so). So if you see a linux tutorial with this line for loading a module

1
LoadModule proxy_module modules/mod_proxy.so

it corresponds to this line on IBM i:

1
LoadModule proxy_module /QSYS.LIB/QHTTPSVR.LIB/QZSRCORE.SRVPGM

So loading the modules necessary for load balancing looks like this:

1
2
3
4
LoadModule proxy_module /QSYS.LIB/QHTTPSVR.LIB/QZSRCORE.SRVPGM
LoadModule proxy_http_module /QSYS.LIB/QHTTPSVR.LIB/QZSRCORE.SRVPGM
LoadModule proxy_balancer_module /QSYS.LIB/QHTTPSVR.LIB/QZSRCORE.SRVPGM
LoadModule lbmethod_byrequests_module /QSYS.LIB/QHTTPSVR.LIB/QZSRCORE.SRVPGM

The rest of the configuration is compatible with the standard documentation and may look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ProxyRequests Off
<Proxy \*>
Order deny,allow
Deny from all
</Proxy>

<Proxy balancer://clusterIleastic>
BalancerMember http://devil01:47001
BalancerMember http://devil01:47002
ProxySet lbmethod=byrequests
</Proxy>

<Location /balancer-manager>
SetHandler balancer-manager
</Location>

ProxyPass /balancer-manager !
ProxyPass / balancer://clusterIleastic/

Note: I am no expert of Apache HTTP server. This is a simple configuration for my private adventures and should definitely be reviewed before using it anywhere.

ILEastic Web Services

As we can see “Load Balancing” is not something specific about ILEastic. It is rather the opposite: it has nothing directly to do with ILEastic. But ILEastic web services can be “load balanced” in a very easy way because an ILEastic web service brings everything to the table it needs … even its own web server. So all you really need to do is keep the port number configurable and you are ready to go.

Starting a new instance of an ILEastic web service is a simple SBMJOB and stopping an instance is done with ENDJOB. No magic there.

It really can be that simple!

Happy load balancing!

Mihael