Load balancing HTTP requests based on unique ID

I am trying to load balance traffic amongst 2 servers which have their own separate databases. Once a file is submitted for processing, the server responds back with a unique ID. Subsequently, another request is sent to the server to check the outcome of the request referencing the unique ID that was generated earlier. With round robin and least Conn load balancing algorithms, and with no control over the client making the requests (cookie set and received in the first request is not used in the subsequent request), is there a way wherein all unique IDs and the corresponding cookies generated in the first request are saved, and based on the unique ID in the subsequent request, the appropriate cookie is inserted by the load balancer. The problem i am facing now is the first request is going to one of the servers, wherein the file is submitted for processing and unique ID returned, and the subsequent request to query the result using unique ID, is sent to the other server, which does not have any information about the unique ID as it was generated by the other server. Any help would be much appreciated.

Client file submit → haproxy → load balanced to server A → response contains unique ID and cookie

Client result query using Unique ID (without cookies) → haproxy → load balanced to server B → response contains error unique ID not found

Any ideas to fix this please?

You will have to explain context.

Why would the client forget the Unique ID cookie and where should the proxy detect the Unique ID from in the subsequent request if it lacks the cookie? Is the Unique ID booth in the cookie and the URI perhaps? Is the client unable to handle cookies?

Unless you have other requirements, I’d simply load-balance based on the source IP hash … balance source.

Thank you lukastribus for your response.

It is a custom application which is installed on the client, which does not support cookies - it consumes REST API provided by the server. It just sends the file for processing in the first request (POST) and receives a unique ID in the body of the response . After a delay of 5 seconds, it sends the second request (GET) containing the unique ID in the URI to query the result. It does not honor the cookie (set by haproxy) received during the first response containing the id of the server that received the first request.

Is there any mechanism through which the proxy keeps a copy of the cookie and the unique id while responding to the POST request, which can be used to lookup and rewrite the GET request based on the cookie that was saved earlier (probably by cookie insertion)

The Unique ID is in the body of the response of the POST request and in the URI of the subsequent GET request

Yes - the custom application seems to be poorly built and does not support cookies

I have used the balance source option, and it works, but the load is not balanced - all requests are sent to one server only and the other server remains idle. Utilizing both servers at the same time would reduce the total processing time. If 500 files are being processed, 2 servers would process them within half the time required by a single server

I thank you once again for your response and would appreciate if you would provide recommendations to fix this issue

Got it.

A problem here is parsing the body of the response to the POST request. We need the unique ID from that response, because we need to save the ID → server association at that point (it’s too late after). But parsing the body is pretty much impossible.

Are you able to modify backend server behavior so that the unique ID is copied in a HTTP header (like a cookie) also, so that we don’t need to parse the body?

Unfortunately, it is not possible to modify the server behavior - JSON-based REST API is used and the unique id is in the body of the response (there is nothing else in the response except the unique id).

Sample Response to Initial Request -

{

"Unique_ID" : "a1b2c3d4e3f6g7h8i9j0a1b2c3d4e5f6"

}

The cookie inserted by haproxy is in the header, of course.

Sample Subsequent Request -

GET /result/a1b2c3d4e3f6g7h8i9j0a1b2c3d4e5f6

Sample Response to the Subsequent Request when request routed to the wrong server

{

"a1b2c3d4e3f6g7h8i9j0a1b2c3d4e5f6" : "Not Found"

}

If it is difficult to parse and store responses to the initial request, can we redirect/rewrite the response containing “Not Found” error to the other server. Meaning if a “unique id” is not found on server a, the entire response is stripped by the proxy and a redirect to the other server (server b) is sent to the client - the client can then send the same request to the other server and would be able to successfully retrieve the result. What do you think?