To be able to mimic our production workload in testing, we had to come with a low cost solution to load balance HTTP traffic between few application servers. In addition to that, for the first (initial request) we required even distribution amongst the backend nodes but, subsequent requests needed to be handled by the same backend server.
Load Balancing and Sticky Sessions by URL Parameter
apache maintains one of the best online documentation for most of its versions so finding a solution to my problem was not hard at all. However, most solutions are based on cookies what I could not use. The issue with cookies is that I have limited control over them, there is no guarantee that the user has cookies enabled, or it will be saved not to mention that cookies can become corrupted too.
The other issue with cookie based load balancing is that once you have one set, you always ended up on the same backend server even when you make your first request. Our requirement was that for initial calls, we want randomly allocated backend then for subsequent calls we want the user to be handled by the same server what handled the first call.
Sticky sessions with URL parameter is supported by apache’s mod_proxy:
The second way of implementing stickyness is URL encoding. The web server searches for a query parameter in the URL of the request. The name of the parameter is specified again using stickysession. The value of the parameter is used to lookup a member worker with route equal to that value.
For some reason, I had mixed results, over all it seemed unreliable hence I had to come up with my own solution based on mod_rewrite:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Important to point out, that mod_rewrite rules are evaluated first before any other settings or rules and this behaviour is critical for this functionality.
We basically check the incoming URL and if we a have match on our URL parameter, we pass the request handling to mod_proxy without evaluating any other rules. Our URL parameters are always unique and set by the backend servers so we need as much rewrite rules as backed servers we have.
<Proxy> directive sets up a standard load balancer, this ensures that initial request can be load balanced evenly between all member servers. The
directives ensure that we correctly pass redirects back to the clients…
The only issue with this setup is that, if one of the application servers become unreachable, the clients will receive 502 (bad gateway) if they have URL parameter set.
NGINX is much easier to set up, and since it monitors the backend servers, it can proxy traffic to other available backend servers regardless of the URL parameter being set or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34