# Leastconn for Websockets

**URL:** <https://discourse.haproxy.org/t/leastconn-for-websockets/5860>\
**Category:** Help!\
**Created:** [October 27, 2020, 11:18am UTC](https://discourse.haproxy.org/t/leastconn-for-websockets/5860 "2020-10-27T11:18:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![why](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@why](https://discourse.haproxy.org/u/why)\
**Post date:** [October 27, 2020, 11:18am UTC](https://discourse.haproxy.org/t/leastconn-for-websockets/5860/1 "2020-10-27T11:18:56Z")

</div>

Hello,

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!

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [October 27, 2020, 12:14pm UTC](https://discourse.haproxy.org/t/leastconn-for-websockets/5860/2 "2020-10-27T12:14:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![why](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@why](https://discourse.haproxy.org/u/why)\
**Post date:** [October 27, 2020, 12:27pm UTC](https://discourse.haproxy.org/t/leastconn-for-websockets/5860/3 "2020-10-27T12:27:27Z")

</div>

Perfect, thank you, this is exactly what I need 🙂

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?

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [October 27, 2020, 1:09pm UTC](https://discourse.haproxy.org/t/leastconn-for-websockets/5860/4 "2020-10-27T13:09:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![why](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@why](https://discourse.haproxy.org/u/why)\
**Post date:** [October 27, 2020, 1:55pm UTC](https://discourse.haproxy.org/t/leastconn-for-websockets/5860/5 "2020-10-27T13:55:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [October 27, 2020, 2:42pm UTC](https://discourse.haproxy.org/t/leastconn-for-websockets/5860/6 "2020-10-27T14:42:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![why](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@why](https://discourse.haproxy.org/u/why)\
**Post date:** [October 28, 2020, 8:41am UTC](https://discourse.haproxy.org/t/leastconn-for-websockets/5860/7 "2020-10-28T08:41:13Z")

</div>

Thank you, @lukastribus 🙂

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!

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [October 28, 2020, 10:28am UTC](https://discourse.haproxy.org/t/leastconn-for-websockets/5860/8 "2020-10-28T10:28:02Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![why](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@why](https://discourse.haproxy.org/u/why)\
**Post date:** [October 28, 2020, 10:40am UTC](https://discourse.haproxy.org/t/leastconn-for-websockets/5860/9 "2020-10-28T10:40:20Z")

</div>

Great, thank you very much again 🙂
