200 points · by tosh · 4y ago · 136 comments
I want the connections to be distributed to separate instances of a WS server based on modulo-based or consistent hashing algorithm... above that in the stack we have a message broker or queue partitioned using the same sharding algorithm for the number of websocket servers running. This queue/message broker sends publishes messages to each web socket service which in-turn pushes the message to the client. This is how I want to handle horizontally scalable web socket connections...
The question I have is the best way to do this and the best way to migrate connections. From one service to another when/if the replication factor of web socket servers changes (we add/remove servers)...
One way I would think would be to send a disconnect message to the websocket service when this event happens and it would disconnect it's clients and then have a random delay for when re-connection happens... so as to avoid trying to reinitialize all of those connections at once. This seems sub-optimal as with consistent-hashing many of the connections will stay on the same server, and terminating those connections would be unnecessary.
What is the best way to migrate an open TCP from one server to another? Or if the connection must be closed... how can we minimize the number of connections that are to be closed.
BTW: I know I can broadcasts to all instances of the websocket server, and then unless that server holds the connection it is no-op... if the server does have a connection then send the message back... This also seems it would have a limit to how far this scaling-strategy would grow before running into limitations.
You're welcome to checkout the docs and get started: https://docs.hathora.dev/#/buildkit/README
If you'd like to get in touch, feel free to shoot me a message: sid [at] hathora.dev
Then you can use whatever logic you want for it. If the client disconnects, it just asks again and reconnects.
Use a token from the directory endpoint to authorize the connection so they have to do it.
That way, you can migrate users one by one (sending a 'redirectUserConnections(userId)' to the original server, and even pick a shard that is geographically close to the user. (If you care about race conditions during migrations, you need to be really careful though.)
race conditions during migrations are the biggest concern.... You do not want a client with a connection to server they will never receive a message on.
haven't committed to using one particular technology. queues that support retries could be useful in the event that a disconnect happens messages wouldn't be necessarily lost. Something custom built on top of Redis could accomplish the same perhaps.
Can't you not involve the client and send it an explicit "reconnect to server XYZ" message, only move clients that really need to move?
[1] https://github.com/uNetworking/uWebSockets/discussions/1466#...
[1]: https://github.com/uNetworking/uWebSockets#battery-batteries...
It's common to remark on a language being "C-like"; not so much that a language is "JavaScript-like".
If someone made a web app in C++, it used to be OOP heavy. There's no class declaration in sight here.
[1] https://en.wikipedia.org/wiki/Clarke%27s_three_laws#Variants...
µWebSockets is a simple to use yet thoroughly optimized, standards compliant and secure implementation of WebSockets (and HTTP). It comes with built-in pub/sub support, URL routing, TLS 1.3, SNI, IPv6, permessage-deflate
I guess that it is a backend library for doing websocket communication for node and perhaps also bun?
https://github.com/uNetworking/uWebSockets/blob/master/misc/...
Interesting. Bun keeps getting more compelling.
Any binary library would also require building the library itself using the same toolchain and then linking to it - which requires a lot more configuration on the consuming side. It might be easy if both projects use the same build system (e.g. cmake), but that's far from guaranteed in C++.
If the library is open source, I'm fine compiling it once and then linking it. Not a big fan of header-only libs to be honest.
Addendum: though it is clearly a u in the URL. Someone may be confused...
μ provides a good contextual clue about the project. What would the ε character tell me?
> In the 1970s, programming was an elite's task. Today programming is done by uneducated "farmers" and as a result, the care for smart algorithms, memory usage, CPU-time usage and the like has dwindled in comparison.
https://github.com/uNetworking/uWebSockets/blob/master/misc/...
Stephen Wolfram is going to be so pleased that he finally has his own unit named after him!
Mathematics, physics, computing, and now psychometrics, is there anything that man can’t make groundbreaking progress on?
“I don't know how many of you have ever met Dijkstra, but you probably know that arrogance in computer science is measured in nano-Dijkstras”
At what point does one assume moral high ground with 100% certainty?
It's as if all this hate comes from the inability to read text carefully, without isolating malicious intent first.
What's wrong with that statement? In 1970's, the number of people who dealt with programming was significantly smaller than today. It's a matter of fact. And people who worked with computers back then were highly educated and pioneers of the technology, there was no room for half-hearted effort or "let's copy the first result from stackoverflow.com".
Today, programmers who are meticulous, detail-oriented and who genuinely recognize the problem first are in minority and that's how I read the above.
The author might have stated it abruptly, but it does not mean he's wrong. You're using the "cool little library", yet you have this kind of resentment for the person who made it, have you even tried to look at the situation from their perspective before you passed the judgement?
I'm asking out of curiosity, because I can relate to having to work with under-skilled individuals who spend more time sorting out their CV and online presence than their work.
> under-skilled individuals who spend more time sorting out their CV and online presence than their work
That sounds more like a organizational/hiring problem.
It's not an insult in gaming context, and "farming" in the context of our craft does not relate to actual farmers. It sounds like taking things out of context to gain moral upper hand which allows for insulting the author.
I have to be devil's advocate, this whole shaming because someone else might have gotten offended is seriously tiresome.
Library is crafted well, guy's annoyed with self-proclaimed crappy devs, yet the freedom to say so is revoked by the very community he helps.
But what do I know ... I'm just a farmer. Now excuse me while I go shove my computer down a shredder and go do something else.
And yet probably the same percentage of what they wrote was utter crap [0]. The illusion comes from having more people writing more code for more reasons now. Sturgeon's law still applies.
On top of that, the industry rewards those who:
* "Produce results" insetad of those who pre-empted a bug 5 years in advance by building something to be robust [1].
* Job hop every year or two, and thereby disproportionately rewarding those who work on their CV.
* Are personal friends and family of higher ups [2].
To claim the major reason why software is in its current state is largely/solely because of all the "farmers" is harmful ignorance, at best.
> programmers who are meticulous, detail-oriented and who genuinely recognize the problem first are in minority
Perhaps the biggest problems in(/with) technology today aren't of a technical nature. If even somewhat true, then those who are "meticulously and genuinely recognizing the problems" aren't necessarily even programmers.
[0]: This is hard/impossible to know one way or the other due to survivorship bias. A lot of software from the 70s is just gone.
[1]: Instances of this are hard to even find, naturally. Squeaky wheel gets the grease and all that.
[2]: Hardly exclusive to IT, but still a significant factor.
He's a bit like Linus. He knows what's up and sees all the people around him building sub par software.
But instead of telling them how to improve, he's very confrontative and I think those times of gatekeeping are over.
Gatekeeping implies that this author actually has something he can keep from you, like universities gatekeeping a certificate for intellectual credibility, or OpenAI saying that they have information that is too dangerous for the world. The personal refusal to acknowledge anyone for anything is only gatekeeping insofar as the public ought have some moral prerogative to that person's acknowledgment.
Why is this one author's acknowledgment so interesting that we should have a debate over it? In the face of all priorities, does this really bubble up to the top?
Much more offensive (to me) is this gem from later on:
> Designing clever, binary and minimally repetitive protocols saves enormous amounts of CPU-time otherwise lost to compression.
Well yes, but also, and more importantly no. This is technically true - not compressing the data is more efficient in terms of computation per bit on the wire. But it is also wrong in a much more subtle way.
The advantage of using JSON (or an equivalent) and then compressing it down is that we inject a large amount of redundancy into the format. The CPU is taking that redundancy back out for us to save bandwidth, but we get to exploit it in much more valuable ways. The encoder/decoder are generic, simple and highly optimized. We can use them everywhere and trust them to work. This has value in itself, and it means that we can spend our limited budget of programmer time in optimizing other code where the payoff is higher. It means less time tracking down bugs in "super 10x programmer's one-off encoder that was only written for this specific application". And lastly, but my favourite - it means that when something goes wrong we have a hope of repairing the damage to the data. Because redundancy gives us options for error checking and recovery, while optimizing the binary format generally does not.
Also, if you want the format to really be optimal it should not be designed as byte streams (as low-level binary formats generally are) and instead should be a stream of variable-sized tokens that are fed through an arithmetic encoder.
The first half of this week I spent repairing data corruption caused by some of my code. Luckily there is a lot of redundancy in how I encoded the data (in a structure that is described in JSON inside the records) so I could write a tool that repaired the damage. I guess I'm lucky that I'm just a "farmer" :)
I disagree strongly. Most people I encounter in the agricultural industry are quite creative lateral thinkers.
But yeah I guess they wouldn't think of higher lvl thought such as using JSON because it "helps" with corruption...good luck with your data
I mean you can be critical of communication forms without punching down.
Now I'm wondering if uWebSockets will fail spectacularly in 2038.
Just ask the programmers back then; https://youtu.be/AxSdWhkMB_A
It sure does not sound like an elite career to me. It was just regular people working with the constraints they had at the time, not because of some super intelligence or leet genius focus on perfection.
If anything, projecting yourself (not you in particular!) into some mythical lost elite, as opposed to the current stupid pleb sounds like pure cope imo.
There is a very valid case to be made for the current state of performance but back then if it wasn't optimized for the resource constraints it didn't run -- nowadays software is optimized for shipping quickly so of course it sucks once the buzzword paint falls off.
We're comparing buy-once bespoke boots vs. assembly line fad-of-the-month trainers. The idea that either was the result of some 'giga brain' creators and not market influences is delusional.
"It's not bragging if you can back it up." - Muhammad Ali
We can instead spend our time doing really cool things with massive amounts of data, beyond the wildest dreams of the early coders.
The uWebSocket's authors take is SO bad in SO many other ways too. They should be prescribed a dose of real world.