Live data from Hacker News

Ask HN: How do you scale WebSocket?

news.ycombinator.com

1–10 of 17 posts

Ask HN: How do you scale WebSocket?

#1
Hi All,

I have a screen which displays QR code on browser screen & at the same time it opens a Websocket connection with my backend(Spring). Once the payment is done a Webhook response comes to one of my backend endpoint with the payment state(success/failure) & other order details.

The application is working fine if it is running locally or with only single instance. But since we have our own Auto scaling group and load balancer configured over DNS, the connection is not getting established all the time.

So, how exactly shall this be architectured so as to scale the same horizontally? I don't have any DB configured as of now. I have thought of using SNS with SQS but it seems they are way too many overheads. How do big companies like WhatsApp scale?

Re: Ask HN: How do you scale WebSocket?

#3
post #2

Id use a database and have the websocket just periodically check the database for payment confirmation. This way when once instance get the webhook activity, it puts it in the database and then.... ??? .... profit

Hi, Thank you for responding. Actually we have one external API which provides this detail as soon as the payment is done. But we wanted something wherein once the payment is done we can directly send back the details directly to the connected client using that incoming webhook response instead of making the client(browser) to call one another API/fetch state present in DB periodically.

Re: Ask HN: How do you scale WebSocket?

#5
post #2

Id use a database and have the websocket just periodically check the database for payment confirmation. This way when once instance get the webhook activity, it puts it in the database and then.... ??? .... profit

Hi, Thank you for responding. Actually we have one external API which provides this detail as soon as the payment is done. But we wanted something wherein once the payment is done we can directly send back the details directly to the connected client using that incoming webhook response instead of making the client(browser) to call one another API/fetch state present in DB periodically.

Without knowing more than the details you've just provided, I would suggest giving the various options a second look and weighing the pros/cons.

Websockets to me don't seem like the ideal approach here, since the communication is just from the server to the client (an update of a data payload once an event occurs in the backend system).

Websockets have quite a bit of technical complexity requiring significant architectural effort to ensure reliability of a service. Ably is a company that offers websockets as a service and has some good blog articles to start you out if you're sure about this path.

What I would recommend with the details provided so far is to either use SSE or long-polling. The "downsides" are often over-exaggerated, and there are lots of businesses that one would assume use websockets that really are just using SSEs because operationally and architecturally it is vastly simpler to reason about.

I can almost guarantee that the complexity of adding another API endpoint will be drastically less than standing up a reliable websocket infrastructure.

Re: Ask HN: How do you scale WebSocket?

#6
" the connection is not getting established all the time" Which connection does not get established? The webhook connection to your backend or the websocket connection to your backend? Or do you get a webhook response, but failing to send a response via websocket?

Re: Ask HN: How do you scale WebSocket?

#7

Socket.io has a great article on how you can setup your architecture to scale Web Socket servers: https://socket.io/docs/v4/using-multiple-nodes/

The whole article seems to be about sticky sessions which are needed for the long polling fallback transport option Socket.io uses when websockets can't be used.

Eg. from the article: " the WebSocket transport does not have this limitation, since it relies on a single TCP connection for the whole session. Which means that if you disable the HTTP long-polling transport (which is a perfectly valid choice in 2021), you won't need sticky sessions "

Re: Ask HN: How do you scale WebSocket?

#8
I could be wrong, but I wouldn't think the autoscaling or load balancing would affect the websocket connection. There may be another aspect of the infrastructure that's preventing the connection though. Can you share more about the setup? Strange that the connection would succeed sometimes but not others.. This could be related to the configuration of some intermediate network layer. Eg. if Nginx is used, you may have to look into the settings that are needed to ensure websockets work well. Take a look at these pages:

https://stackoverflow.com/questions/12102110/nginx-to-revers...

https://stackoverflow.com/questions/10550558/nginx-tcp-webso...

Re: Ask HN: How do you scale WebSocket?

#9

Socket.io has a great article on how you can setup your architecture to scale Web Socket servers: https://socket.io/docs/v4/using-multiple-nodes/

The whole article seems to be about sticky sessions which are needed for the long polling fallback transport option Socket.io uses when websockets can't be used. Eg. from the article: " the WebSocket transport does not have this limitation, since it relies on a single TCP connection for the whole session. Which means that if you disable the HTTP long-polling transport (which is a perfectly valid choice in 2021), you…

>since it relies on a single TCP connection for the whole session.

still has to exist and stay active. (ie, interact with the correct node)

Re: Ask HN: How do you scale WebSocket?

#10

" the connection is not getting established all the time" Which connection does not get established? The webhook connection to your backend or the websocket connection to your backend? Or do you get a webhook response, but failing to send a response via websocket?

It's the websocket connection which is not getting established. Webhook response is a simple Post request and it's coming fine. The problem here is sometimes this webhook response comes to that particular instance where the websocket connection was not established with the client.Ideally I would be happy if this response comes to both of the instance through some configuration on loadbalancer.
Post reply on HN