DEV Community

How do you scale a nodejs real-time API to concurrently serve a million users?

Priyansh Jain on August 05, 2018

So I recently got a project that requires real-time location + chat data streaming, and I wanted to know the right steps to take. Most articles I'...
Collapse
 
dmfay profile image
Dian Fay

Articles about scaling Node are going to recommend pm2 and load balancing because that's how you scale Node. There are plenty of articles here & elsewhere about containerization if you look for those instead; the important thing to remember is that once you have a container that's listening on whatever port and serving content, the fact that it's doing that with Node stops mattering. Same goes for CI/CD and database tuning: the content is out there, and easily found with a little exploration.

Anyway, from 30,000 feet the approach I'd take is to build Docker images as part of CI, deploy them to DockerHub or a private registry, and manage them with Kubernetes, since that's more or less become the default orchestration tool. You'd want to look into high-availability Kubernetes specifically since that involves some extra configuration to avoid single points of failure.

Collapse
 
presto412 profile image
Priyansh Jain

So container management with kubernetes will avoid single points of failure, and this I'll have to integrate with my load balancer and process manager right?
On a side note, are there any good methods to identify the ideal amount of RAM and processing power i should deploy with my servers, to optimise costs? Are there any metrics?

Collapse
 
sandeepkamboj12 profile image
Sandeep kamboj • Edited

XMPP server is best solution for chat. Do not use node.js as chat data streaming.

Collapse
 
abhi12299 profile image
Abhishek Mehandiratta

Hey! A little late to the discussion, but I have a question. What if my application has custom chat requirements, such as only some users being able to message other users. For this we do need some sort of proxy/server in between the client and the XMPP server. How exactly should this case be handled where requirement is based on data stored outside of XMPP? A little overview/nudge in the right direction would be highly appreciated.
Thanks!

Collapse
 
mradigital profile image
mradigital

Hi Sandeep, are you available for some on going development work? If you are please contact me on glen.alexis@mradigital.com to discuss.

Thanks

Collapse
 
david_j_eddy profile image
David J Eddy

Could you provide some details / data on your statement. Why would you choose XMPP over a NodeJS app? Do you have any personal experience / empirical evidence the poster could use for references?

Collapse
 
sandeepkamboj12 profile image
Sandeep kamboj • Edited

Yes. 8 Android app built by me use XMPP (Openfire).

I previously use Node.js but there are many limitations in node.js

1) You need to built all things from scratch but in openfire XMPP server each and every thing that i want to use is already prebuild.

2) Node.js is good for broadcasting messages. But for one-to-one messaging. It is disastrous. On the other hand Openfire provides both of these.

3) Main problem with node.js is offline messaging.

Example. When User_A send message to User_B and User_B is offline at the time of messaging. Then message lost. To solve this problem you need to code all thing for this from scratch in node.js. But in openfire you don't need to worry.

4) You need to track all users sockets. But in openfire there is no such problem.
5) You don't detect which user is online or not in node.js. You need to program it that node.js ping for every 'X' seconds to users to track their availability. But In openfire you don't need to worry.

There are many other points.

In other words You just need to install XMPP server like openfire. And Go for your project. you don't create all thing from scratch like in node.js

I have one question. Why google,facebook,whatsapp use XMPP server for instant messaging and push notification. Why they don't even try to use Node.js

Node.JS real time API is just for fun.

I use Node.js previously But I face many problem . My client complaint that they don't receive messages. Buy when i heard of XMPP server. i used that in all my apps for push notifications and for chat.

Thread Thread
 
david_j_eddy profile image
David J Eddy

Thank you for the details. That is some very helpful information. I sure @presto412 has a better idea of the route to take now.

Thread Thread
 
presto412 profile image
Priyansh Jain

cubettech.com/blog/xmpp-chat-using...

How would you rate this? I'd like to keep my codebase consistent

Thread Thread
 
presto412 profile image
Priyansh Jain

Hey I would like to discuss this a lot further, could you follow me back or send me a mail at priyansh.jain0246@gmail.com ?

Thread Thread
 
david_j_eddy profile image
David J Eddy

I have followed you here on dev.to :).

Thread Thread
 
sandeepkamboj12 profile image
Sandeep kamboj

Mr. Priyansh Jain. That is node.js xmpp client not XMPP server.

Thread Thread
 
sandeepkamboj12 profile image
Sandeep kamboj

I think you don't read comment section in first block of code.

first : we need an xmpp chat server

second : establish connection to the chat server

Thread Thread
 
presto412 profile image
Priyansh Jain

Oh darn, didn't realise it.

Thread Thread
 
kaydacode profile image
Kim Arnett 

Love all the knowledge sharing in this thread. Just a friendly reminder everyone’s here to learn from each other’s experiences. 👍🏻

Collapse
 
david_j_eddy profile image
David J Eddy • Edited

What @dmfay said; if you containerize the application the process running inside the container is of minimal impact; outside of resource requirements per container (IE cost to run each instance). However, scaling becomes much easier.

The current industry direction is options like Amazon ECS or Google GCE. When configured correctly the running process can finally not be the bottle neck anymore.

AS for CI/CD: you will want a task runner (TravisCI/Jenkins/CodeShip) and setup security scanning, performance checking, linting, test runners, and any other step you come across later. The name of the game here is automation with the goal of quality enforcement.

As for Database optimization, that really depends on the needs of the application, SQL, NoSQL, Key/Value store, Document store? How about media/binary storage? Does it have to be ACID compliant? Is latency or accuracy more important? The database question would need more information to really sort it out.

That all being said, I am available, and the dev.to community is here, to help out along the way. No matter what, it sounds like you have an interesting project ahead.

Collapse
 
presto412 profile image
Priyansh Jain

Thanks for this, looks like I'll start with containerization
Do follow me back so we can stay in touch!

Collapse
 
kayis profile image
K

I read that SocketCluster can be used for scalable real-time services, but I never tried it out.

Collapse
 
presto412 profile image
Priyansh Jain

It looks promising, thanks for the suggestion!

Collapse
 
geosoft1 profile image
George Calianu • Edited

To scale an application that serves so many users i would consider Go language.