Sticky session load balancer with nginx open source
Asked Answered
R

3

14

What the main difference between the sticky session available in nginx plus and hashing cookie in open source version?

According to the docs nginx open source allows session persistence based on hashing different global variables available within nginx, including $cookie_

With the following configuration:

    upstream myserver {
        hash $cookie_sessionID;
        server localhost:8092;
        server localhost:8093;
        server localhost:8094 weight=3;
    }

    location / {
       proxy_pass http://myserver;
    }

Assuming, there will be centralized mechanism across backends for generating unique sessionID cookie for all new requests, so what the main disadvantages of such method compared to the nginx plus sticky session approach?

Retribution answered 18/12, 2017 at 21:51 Comment(2)
If you want sticky load balancing, you probably want to add consistent to the hash configuration, so that it reads hash $cookie_sessionID consistent;; see the following nginx docs: choosing a load-balancing method; option 4. Generic Hash.Orv
does it work with hash $cookie_sessionID consistent; on nginx opensource?Munos
B
7

IP Hash load‑balancing can work as "sticky sessions", but you have to keep in mind that this load balancing method is working relative bad itself, because a lot of user/devices shared same external IP-Address in modern world.

We experimented with a rather heavily loaded (thousands of parallel users) application and observed tens of percent imbalance between servers when using IP Hash.

Theoretically, the situation should improve with increasing load and number of servers, but for example we did not see any significant difference when using 3 and 5 servers.

So, I would strongly advise against using IP Hash in productive environment.

As open-source based sticky sessions solution, not bad idea to use HAProxy, because HAProxy support it out-of-the-box. Or HAProxy + Nginx bundle, where HAProxy is responsible for "sticky sessions". (I know about one extremely loaded system that successfully uses such a bundle for this very purpose, so, this is working idea.)

Birkenhead answered 7/2, 2020 at 8:25 Comment(1)
The question was more about the ability to build a custom session persistence by hashing a $cookie_cookievalue same as for $request_uri decribed in Generic Hash option available in free nginx version. To be more specific - to what extent such option would be a reliable in comparison with the sort of similar suggested 'sticky cookie' option from the paid nginx version, provided all of the custom code needed to distribute, maintain and generate unique session ids works ok.Retribution
S
1

Your approach will work. According to the official NGINX documentation (Configuring Basic Session Persistence):

"If your application requires basic session persistence (also known as sticky sessions), you can implement it in NGINX Open Source with the IP Hash load‑balancing algorithm."

While NGINX Plus "offers a more sophisticated form of session persistence". For example "Least Time" method – when for each request the server with the lowest average latency and the lowest number of active connections is chosen.

Salaam answered 21/2, 2019 at 14:30 Comment(1)
I believe the question isn't about the IP hash, but ability to build a custom "sticky cookie" persistence system by leverage Generic Hash option from the free nginx version.Retribution
F
0

The sessionID cookie would probably not be set in the first request, only on subsequent requests, therefore the first and subsequent requests could end up on different backends.

Foundry answered 27/3, 2024 at 9:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.