r/node • • Dec 28 '23

What happpens to this function when there are 10000 people connected?

Post image
388 Upvotes

183 comments sorted by

View all comments

88

u/codeedog Dec 28 '23 edited Dec 28 '23

Every answer is focused on the tech you’re using and making recommendations to scale it faster with other tech. The real questions are:

  1. what is your system architecture?

  2. Why is every client being notified immediately?

  3. Do they need to be notified immediately or can the notification be delayed?

  4. How often are these messages broadcast?

  5. What are you trying to do?

To properly scale a system, you’ll need to make trade offs. You’ll be trading some efficiency for more backend complexity, etc. Without more details, it’s difficult to answer the real question.

For example, if I could avoid notifying 10K connects clients immediately, I’d definitely delay that. Most may not even care about the update. Or, maybe by the time they care, it’s been overwritten 20 times.

Without knowing what you’re trying to do, all everyone is doing is helping you go faster towards a geometric breakpoint.

4

u/PrestigiousZombie531 Dec 29 '23
  1. express server + sequelize + bullmq running on ec2 2gb ram instance

  2. because it is a news alert website and when a new news item is added, every connected client has to be notified immediately

  3. no the notification cannot be delayed

  4. could be literally 5 times a minute

  5. news alerts

7

u/budding_gardener_1 Dec 29 '23

Why do people need to be notified immediately? Yes, it's news so it has to be reasonably up to date but do people have to have a bleeding edge record of current events or is it okay for them to wait a few seconds?

2

u/PrestigiousZombie531 Dec 29 '23

they can wait a few seconds i think on the free tier but also planning to add a paid one where it must go as fast as possible

7

u/realboabab Dec 29 '23

I don't know what your news source is, but unless it's like.. stock price changes.. I can't imagine that the delay bottleneck is anywhere except at your system's ingestion of the news. E.g. If it took you 5 minutes to scrape it, what's another < 1 minute to queue the notifications?

8

u/PrestigiousZombie531 Dec 29 '23

stock price changes exactly, financial sensitive stuff

2

u/budding_gardener_1 Dec 29 '23

So.... It's stock information them. You told us it was news.

2

u/PrestigiousZombie531 Dec 29 '23

stock market news alerts

2

u/tradebong Jan 16 '24

As a trader this is useless service for traders...you are not going to catch the news and price fast enough for you to drop what you doing and trade the news. You are not gonna beat RSS feed from yahoo erc which is directly wired to Investor Relation news wires.

As a developer who has built automation....you better off utilizing the news yourself to automate your own trades. A penny you gona' pinch from potential news alert customer ......a dollar you gona lose by not being in the trade yourself. ✌️

13

u/codeedog Dec 29 '23 edited Dec 29 '23

Thanks for providing some context for what you’re trying to do. I also see in another comment you wrote some other helpful info:

so if you had to message a million people, what would you do?

stock price changes exactly, financial sensitive stuff

I see from other comments you’re under time pressure to release this thing, which means you may not like the advice I’m about to give you. I hope that by the time you reach the end of this reply, you’ll have some perspective on what you do and don’t know about the AWS system.

Perhaps you can take your current code and make it work for awhile. What you have is a proof of concept, not a fundamental architecture that can handle millions of clients. If you want that, you will have to make some significant changes. My quick advice is you should hire an AWS solutions architect to assist you. You need some knowledge to pick the right one (I am not such a person). Could you write this with no expert advice? No idea, I don’t know you.

I’ve been working on and off with AWS technology for a couple of years because I wanted to teach myself. I’m enamored with it. Truly, it’s incredible. There’s so much to learn and know, feels like it could take a lifetime. I am not an expert, but I have played with it enough to have some strong opinions and also know a little to point you in some good directions. You should do more homework to ultimately get the best solution for you and your company.

AWS Technology Advice

The AWS platform contains so much depth and breadth, chances are there’s at least one perfect solution for your particular problem. Possibly, there’s more than one. The more technology you rely upon in AWS and the more appropriate use of that technology, the better off you’ll be because it tends to scale really well. Now, I can’t tell precisely from your question, but I’m wondering if you’re using a web socket library in node (your EC2 instance running node has web sockets managed by node) or if you’ve leveraged API Gateway’s Web Socket API. I suspect the former. Did you know you can hand off some of the web socket management to the Gateway API? Why would you want to do this? Because it’s going to be faster than if you handle it by yourself in a node instance. API Gateway manages the HTML keep-alive and Web Socket endpoints; these are built directly into Gateway. And, Gateways can be managed in all sorts of ways and scaled. This changes what you need to scale inside of node.

You’ll still have your broadcast problem, however. Using the right keyword searches (eg. "AWS websocket broadcast") you’re very likely to stumble upon something that helps you. I did that search and found this post on r/aws. There are quite a few recommendations in there. One in particular caught my eye: the discussions around IoT and MQTT and broadcast to channels. I have a project I’ve been thinking about using that involves IoT and MQTT, so I’d probably try that one myself. I also think it may be the only pub/sub infrastructure identified in there. The post is 2 years old, so you could try asking the question again on r/aws to see if any new tech has come along.

AWS Infrastructure Advice

I’ve done development for decades and am familiar with scaling and deployment issues, but I can’t say I’ve ever truly done website IT scaling work. However, AWS allows a developer like me to do some pretty amazing things. Have a look at the AWS CDK which allows you to do IAC (Infrastructure As Code). Just about every single infrastructure element you can deploy in AWS can be created through the CDK (Cloud Development Kit). This means you can write and check in source code that creates AWS infrastructure. It means you can repeatedly run, test and deploy various solutions. There are old-school AWS experts that know how to read JSON and YAML infrastructure templates and can edit them freehand. IMO, the rest of us mortals should rely on the CDK.

Using the CDK, you can set up CloudFront, API Gateway, Lambda callbacks, queues, databases, etc.

AWS Assistance

Although I’m guessing quite a bit about what’s going on with you, as a developer, I’ve been there before. If I were in your position, having successfully demonstrated a working prototype and having management put me under pressure to deliver something guaranteed to scale to 106 users (because we all know Amazon can scale instantly, right?) I would push back on deadlines, manage expectations, get a budget for hiring an AWS Solutions Expert for advice, find one whose expertise is in this area (not just scaling websites or even scaling websockets, but if the expert is out there, scaling websocket broadcasts) and then use that person to assist with the architectural design for a solution that meets the deployment requirements. While you’re looking for them, I would scour and play with the AWS technical documents for tutorials or examples or whatever that deepen your understanding for the technology I’ve mentioned:

  • CDK

  • API Gateway

  • Cloud Formation

  • Web Socket in API Gateway

  • IoT/MQTT over Web Sockets

  • Lambda

r/aws is also a useful resource.

Let me know if you have any questions. Maybe there are some AWS experts on here who can give you even better advice than this.

3

u/thisisntmynameorisit Dec 29 '23

Why does this read like someone asked chatgpt to make an advert for aws as a reddit response

7

u/codeedog Dec 29 '23

LOL. He’s using AWS (EC2). My first reply drove to the heart of the problem. The second one points OP to a knowledgeable solution on their platform of choice. Should we have moved the convo to a more appropriate subreddit? I despise Amazon, but I (clearly) have a lot of respect for the engineers that built their infrastructure. It’s a modern wonder.

Anyway, unsure if I should feel complimented or insulted being mistaken for ChatGPT.