Question

I have been using Flask login module, which creates and maintains session on the server.

Since server maintains the session, I think it is not completely stateless. How does it work when application has more than one server. Should requests be sticky (i.e. given session should make subsequent requests to a particular server)?

Was it helpful?

Solution

This statement you've made is not completely correct:

... which creates and maintains session on the server.

Flask-Login uses the session facilities provided by Flask, so the data it stores in the session will be written by Flask using the configured session storage mechanism.

By default Flask writes user sessions as secure cookies in the client, but session on the server are also possible. For example, this snippet shows how to configure Flask to write sessions on a server-side Redis store.

When the user session is stored in a client side cookie it is pretty obvious that having multiple servers is not a problem. The cookie will be sent to the server handling each request, so everything will work just fine.

For server-side sessions this works as well. A server-side session is written under a unique identifier, and this unique identifier is then stored in a client side cookie. Each request then comes with the session ID, and Flask uses this ID to load the session data. If you configure all your web servers to use the same user session storage then multiple servers can handle requests from the same client without issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top