TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: How do you scale WebSocket?

13 点作者 ravirajx7将近 3 年前
Hi All,<p>I have a screen which displays QR code on browser screen &amp; 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&#x2F;failure) &amp; other order details.<p>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.<p>So, how exactly shall this be architectured so as to scale the same horizontally? I don&#x27;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?

6 条评论

toast0将近 3 年前
From what you&#x27;ve commented elsewhere, it sounds like the immediate question is how do you route an external event (webhook) to the right websocket.<p>You need to include information that comes back in the event that identifies the websocket, either directly (server id + an id the server would understand) or indirectly (something that you can look up in a table&#x2F;hash to get direct information).<p>More likely than not, you&#x27;ll actually want to do the indirect option with something that identifies the browser (session id?) in case the browser reconnects to the websocket before the event comes in. Connectivity is fragile, and clients roam across wifi access points or between wifi and lte, or sometimes between lte boundaries that necessitate different IPs, or sometimes their modem is reset while they wait and they get a new IP, etc. Or something in their network path hates the world and closes idle connections on a very aggressive timeout, or closes connections after a short timeout regardless of idle. <i>Lots</i> of scenarios where a reconnection is likely.<p>Finally, since you asked about horizontal scaling, my best advice is to scale vertically first. It&#x27;s usually simpler to manage one server than can do 1M connections than 1k servers that can each do 1k connections. Depending on details, less servers, but larger can be less expensive than many smaller servers; although that changes when your large servers start getting exotic, more than two cpu sockets is a big inflection point, you most likely want to scale horizontally rather than get a quad socket monster (but they exist, and so do eight sockets)
unraveller将近 3 年前
<a href="https:&#x2F;&#x2F;medium.com&#x2F;@14domino&#x2F;using-nats-to-build-a-very-functional-websocket-server-8f4de44c3f12" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;@14domino&#x2F;using-nats-to-build-a-very-func...</a><p>no database, just have the user&#x27;s websocket reach a simple websocket server which always sends on requests to a fuller API server which can speak back to the NATS server who triggers a push to the user since the websocket server is coupled to NATS. This gives horizontal scale to API servers (if they ignore users&#x2F;work not for them) and websocket servers.
monroewalker将近 3 年前
I could be wrong, but I wouldn&#x27;t think the autoscaling or load balancing would affect the websocket connection. There may be another aspect of the infrastructure that&#x27;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:<p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;12102110&#x2F;nginx-to-reverse-proxy-websockets-and-enable-ssl-wss" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;12102110&#x2F;nginx-to-revers...</a><p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;10550558&#x2F;nginx-tcp-websockets-timeout-keepalive-config" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;10550558&#x2F;nginx-tcp-webso...</a>
评论 #31929037 未加载
timebomb0将近 3 年前
Socket.io has a great article on how you can setup your architecture to scale Web Socket servers: <a href="https:&#x2F;&#x2F;socket.io&#x2F;docs&#x2F;v4&#x2F;using-multiple-nodes&#x2F;" rel="nofollow">https:&#x2F;&#x2F;socket.io&#x2F;docs&#x2F;v4&#x2F;using-multiple-nodes&#x2F;</a>
评论 #31926898 未加载
blablablub将近 3 年前
&quot; the connection is not getting established all the time&quot; 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?
评论 #31929007 未加载
matt321将近 3 年前
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
评论 #31925282 未加载