nginx load balancer and long connections – is target always the same?

Advertisements

I wonder how nginx load balances traffic in the following case:

CLIENT —> NGINX —> A GROUP OF SERVERS

Let’s say that the client is a .NET app. .NET’s HTTPClient maintains TCP connection forever by default (docs) (unless there’s no traffic for around a minute, then it’s dropped).

Then, assuming that the client sends request to the nginx load balancer rather frequently (let’s say every 10 seconds), it’s going to keep using the same connection all the time.

How does nginx load balance that traffic? Does it even do that in case when the client keeps the same TCP connection with nginx? Or does it just assign one target server to that client, and due to the fact that it’s the same TCP connection, it will just direct all traffic from that client to the same server?

>Solution :

Assuming you are using HTTP load balancing – based on the docs NGINX performs load balancing (as expected) on per-request basis and how it actually balances depends on the method selected. So for example if you are using IP Hash – then:

The server to which a request is sent is determined from the client IP address. In this case, either the first three octets of the IPv4 address or the whole IPv6 address are used to calculate the hash value. The method guarantees that requests from the same address get to the same server unless it is not available.

So all requests from the same server will be routed to the same backend one. Even if there are multiple HttpClients used.

Leave a ReplyCancel reply