r/haproxy • u/LimpAuthor4997 • Oct 06 '26
How does leastconn wors ?
I have an HAProxy server that use leastconn to load balance between two server. Here's the config file:
frontend FrontEnd
bind :80
maxconn 2000
mode http
option httplog
option dontlognull
default_backend BackEnd
backend BackEnd
balance leastconn
server App1 IP1:8080 maxconn 3000 check inter 2000 rise 3 fall 3
server App2 IP2:8080 maxconn 3000 check inter 2000 rise 3 fall 3
My problem: HAProxy send fewer connection to IP1 than IP2.
I want to know why so I read the leastconn documentation which said that:
The server with the lowest number of connections receives the connection.
What does number of connections mean in that context ? I want to understand how HAProxy send traffic to the backend server.
Since HAProxy cannot directly know the capacity of each server to handle connection, I thought that "number of connections" may mean connection in TCP Established state. So I verified that hypothesis with:
|
|
It shows that established connections are fewer on IP1 but HAProxy thought that IP1 has more active connection so it won't send any new connection to it.
I run this command again a few moment later just to check if HAProxy will correct this but it still yield the same proportion.
When I log into the backend server IP1 and IP2, I get the same proportion.
- On IP1:
| |
- On IP2:
| |
Question: How does HAproxy count the number of active connection when using leastconn ?
Note:
- The HAProxy server only runs HAProxy on it so
ssdoesn't show any other connection to IP1/IP2
1
u/BarracudaDefiant4702 Oct 06 '26
Something is definitely inconsistent in that your haproxy.cfg is using ports 8080 for the server but youu are checking stats is looking at 8081. It's difficult to identify with fake info that does inconsistent alterations. Also make sure you don't have multiple copies of haproxy running as only the latest one will count toward the balancing.
3
u/Old_Guidance9715 Oct 06 '26
Please enable the haproxy stats page and view the number of active connection there. There can be several reason for seeing a different amount of active connections on each backend server, for example connection lifetime.