Sticky Session for Rest API Calls

4.7k views Asked by At

For browser based request with sticky session true load balancer can restrict request to same JVM out of multiple JVMs in a cluster.

But in case request is coming from REST client rather any browser, how the load balancer can restrict requests to same JVM even sticky session is set as true? Any Idea please.

3

There are 3 answers

2
Himanshu Goel On

REST client is made to call REST API and REST APIs should be stateless i.e. complete information about processing of request should be present in request itself, thus request should not dependent on any session data.

If your API is dependent on session data then in actual it is not following some principles of REST.

0
Himanshu Goel On

If your requirement is such that you need to maintain the state then it should be maintained on client side not on server. So one of the way that i will suggest is that you can use cookies to store your state and temp data. While making any REST api call just attach that cookie with request. You can make cookie configurable so that it will be controlled by server and no one else can make change in it.

0
Julio Milani On

The load balancer uses Cookies to keep track of sessions. Retaining the cookies and sending them back in the client should be enough to get the expected result.

For instance, in python, that would mean replacing requests.get(url) with:

s = requests.session()
// ...
s.get(url)