Friday, February 13, 2015

HttpClinet default cookie -- problem

Problem

Problem was we have used load balancing to connect to a third party server, but the sever returns a cookie so clients can have sticky sessions if required.
This results in hitting the same server each time:
curl -s -v -H "Cookie: cookie=R2315085257" 'http://xxxxxxxxx:8080/soap/core/generateOutput?wsdl'
But this round robins:
curl -s -v 'http://xxxxx:8080/soap/core/generateOutput?wsdl'
This means the our application, when accessing third party App through the load balancer, is only using one instance of third party, leaving the other idle.

Diagnosis

Our application  is using HTTPClient and connection pooling. Cookies are not ignored by default.
The cookies sent in the request to Third party Applicaiton via Our application has:
o.a.h.client.protocol.RequestAddCookies - CookieSpec selected: best-match

Hypothesis

Therefore, when a connection is made to Third party Applicaiton via the Loadbalancer  for the first time, the cookie will be set, and then re-used on subsequent connections.
Cookie processing: If an application, such as web spider, does not need to maintain conversational state with the target server, a small performance gain can made by disabling cookie processing. For details on cookie processing please to the HttpClient Cookies Guide.
Ignore Cookies: This cookie specification ignores all cookies. It should be used to prevent HttpClient from accepting and sending cookies.
Specifying the Specification: There are two ways to specify which cookie specification should be used, either for each HttpMethod instance using the HttpMethodParams, or by setting the default value on CookiePolicy.

Proposed Solution

Therefore, in the RESTClient [1], if we explicitly set the cookie policy to:
HttpMethod method = new GetMethod(); method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); we should stop seeing the cooking being sent in subsequent requests from the Our Applicaiton and will load balance to both Third party Applicaiton Servers as required.

No comments:

Post a Comment