Load balancing

Summary

This article explains how to use OpenAMQ to distribute work between multiple applications, a technique called "load balancing".

Explanation

Sometimes processing requests on one box is not enough. Single box has limited processing power and when transaction volume is high, you would like to distribute the requests between two or more boxes in a fair manner.

lb1.png

So, the first message (request) that arrives at the broker goes to box 1, second one goes to box 2, third one goes to box 3, fourth goes to box 1, etc.

To implement load balancing create a single queue for the requests and let all the boxes consume from the queue. If there are several consumers on a single queue, messages are automatically distributed between the consumers in round-robin fashion:

lb2.png

Imagine you are running a helpdesk. You have thousands of clients all over the world posting requests and questions all the time. You have several operators who are receiving those requests. You want to have requests load-balanced so that all the requests don't go to a single operator, rather thay are distributed in a fair manner among all the operators. You would also want the system to adapt dynamically, i.e. if an operator closes his application (say going for lunch) the requests are distributed among remaining operators rather than waiting for the absent operator.

You can find implementation of such a helpdesk here:

To compile and link the files set your environment in the same way as you do to build OpenAMQ server and do the following:

$ c -l helpdesk_client.c
Compiling helpdesk_client...
Linking helpdesk_client...
$ c -l helpdesk_operator.c
Compiling helpdesk_operator...
Linking helpdesk_operator...

To run the helpdesk, you have to start OpenAMQ server first:

$ amq_server

Once the server is running you can run the operator terminal this way:

$ ./helpdesk_operator <broker-address>

For example:

$ ./helpdesk_operator server001

Clients can connect to the helpdesk in the following way:

$ ./helpdesk_client <broker-address>

For example:

$ ./helpdesk_client server001

Comments

rating: 0+x

Author

  • Martin Sustrik <moc.xitami|kirtsus#moc.xitami|kirtsus>