Vasyl Nekohda
Vasyl Nekohda

Reputation: 181

Sticky session load balancer with nginx open source

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?

Upvotes: 15

Views: 30420

Answers (3)

wes
wes

Reputation: 1

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.

Upvotes: 0

Gmugra
Gmugra

Reputation: 510

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.)

Upvotes: 7

Denis
Denis

Reputation: 160

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.

Upvotes: 1

Related Questions