Leastconn for Websockets


I’m trying to figure out how to balance websocket connections across several servers using haproxy.

It looks like the leastconn balancing algorithm seems a good idea for this.

I understand that this means that when a new websocket connection is being established (i.e. connected), haproxy will assign this to the server with the least connections.

However, what happens while the connection is open (and after haproxy initially assigned it to the server with the least connections)?

Would haproxy reassign the server anytime a packet flows over the open websocket connection, or only when a new websocket connection is being established?

Thank you very much!

It’s for new connections only, you can’t reassign a websocket connection, because by websocket definition we need to be layer 4 payload agnostic, so we cannot know when we could possibly interrupt a session and if another server would know what to do with it.

The only way to move existing connection around is to fully close and destroy them, so that the client reconnects.

1 Like

Perfect, thank you, this is exactly what I need :slightly_smiling_face:

So, if I understand correctly, I don’t need a stick-table for this scenario, as once-established websocket connections stick to the originally assigned server automatically?

You’d only need stick-tables if all the connections from one single source IP would need to hit a specific server, e.g. when the customer closes the browser and reopens it 10 minutes later, reestablishes the connection to your application, and you still need to make sure that the customer this the same server than it did in the previous session, then you need stick tables.

But I imagine that is not your requirement.

1 Like

Great, thanks again!

I don’t really need this, but it’s good to know it’s there. Do I understand correctly that I can combine leastconn with stick-tables?

Yes, you should be able to combine it: leastconn would only apply for IP’s not yet in the stick table.

1 Like

Thank you, @lukastribus :slight_smile:

Might I ask two more questions?

  1. If I take one of my servers down, will the loadbalancer reset the connection count for that server automatically to zero?

  2. Does using wss (instead of ws) introduce any problems in terms of leastconn balancing? Would it matter -from a balancing and connection counting perspective- where I terminate SSL in this case (on the load balancer vs on the server)?

Thank you again for your very kind help!

If the server goes down, connection will die which means the connection count will decrease.

I don’t see any problems with wss, no.

1 Like

Great, thank you very much again :slightly_smiling_face:

1 Like