granian icon indicating copy to clipboard operation
granian copied to clipboard

Session Affinity / sticky sessions using Cookies

Open aersam opened this issue 1 year ago • 2 comments
trafficstars

Many Python tools use some state per user in simple python dict's or similar. This makes it impossible to use multiple processes and so it scales not this good. For example, see: https://github.com/zauberzeug/nicegui/wiki/FAQs#how-to-avoid-the-reloading-because-handshake-failed-javascript-error-message-which-results-in-a-full-page-reload

It would be so great for granian to support some kind of session affinity, so that a cookie with some value would cause granian to send a request to always the same process. so an app could make the user stick on the same process (as long as it's not restarted).

It's a feature usually only supported on more complex load balancers, gunicorn does not support it. But as it's really a common problem in python, it would be so great for granian to support it.

aersam avatar Aug 27 '24 09:08 aersam

I might understand the frustration, but at the same time if you need that scale level you shouldn't really keep the state into a Python dictionary or whatever struct in the process memory IMHO.

Granian might be written in Rust, but it still submit to Python rules, given that's where the application code actually lands. Plus, granian is structured in a way there's no such thing as a "main controller", all the workers behave the same, and the listen socket is just shared across them. In order to provide what you ask, requests should be firstly parsed into some "main process" and then forwarded to the relevant "slave process", architecture which will for sure add latency and decrease the today performance.

Given that both the use case sounds quite an edge case to me, and that the balancing theme is quite out of scope for the project, I hardly doubt this will be ever done.

gi0baro avatar Aug 27 '24 16:08 gi0baro

You can enable this behaviour on the reverse proxy or load balancer side.

jules-ch avatar Sep 08 '24 19:09 jules-ch