Explanation of horizontal scale. Is it possible to autoscale?
lowzyyy
HOBBYOP

2 years ago

Hi, i am writing a node js backend that will have worker thread for every user which will do endless loop work.

The nature of problem is that i dont know how many users i will get. Sometime it can be 3, or it can be 100, so i need possibility to have option to have dynamic number of threads.

Is it possible to get more threads or i have to limit lets say 10 threads per app and do the replicas? If i create 10 replicas with 10 threads, can i scale down or up if i get more users?

79 Replies

brody
EMPLOYEE

2 years ago

nodejs can run multiple threads on a single core, so i dont see any issue with threads vs cores, it would only then come down to cpu usage and for that, your app will either use as little or as much of the 32 vcpu as it needs


brody
EMPLOYEE

2 years ago

n/a


lowzyyy
HOBBYOP

2 years ago

there is an issue how can i have 100 threads if i have 32 cpu cores?


lowzyyy
HOBBYOP

2 years ago

all of the threads are blocking threads, so they can run for 10 days straight untill user stop the work


brody
EMPLOYEE

2 years ago

thats simply not how it works, please look into threads vs cores as that topic is outside of the scope of these forms


lowzyyy
HOBBYOP

2 years ago

100 blocking threads cannot run at 32 core


lowzyyy
HOBBYOP

2 years ago

it will be 32 threads at maximum running, the other will wait


brody
EMPLOYEE

2 years ago

yes they can, they would be interweaved


brody
EMPLOYEE

2 years ago

please look into threads vs cores as that topic is outside of the scope of these forms


adam
MODERATOR

2 years ago

With remote computing, threads vs cores aren't something you have direct control over. On Railway, you aren't assigned physical cores for your processes, you're assigned virtual cpus. I think of each vCPU as a single, powerful thread when planning multithreading


adam
MODERATOR

2 years ago

The nature of your app depends on how many users you can serve. Your app needs a major redesign if you're using an entire thread for a long period of time for one user


adam
MODERATOR

2 years ago

For us to help further, some specifics as to the app you're planning on hosting would be very helpful


lowzyyy
HOBBYOP

2 years ago

Every thread is a bot doing endless searching on let's say 1 minute interval, and when he get items, he does something with every item sequentially and repeat that.
I guess i will try to replicate some example on my CPU and test how much it will intertwine or not if try to spawn to many workers


lowzyyy
HOBBYOP

2 years ago

agree, app is in concept stage so far and i am thinking about possible problems


adam
MODERATOR

2 years ago

When you say endless searching, searching what? Why does it take that long? Can you implement multithreading into that process to speed it up?


adam
MODERATOR

2 years ago

Architecture wise, you're probably better off with two services here. One frontend to display a "please wait" message to the user, and a Backend API that does the actual searching and communicates with the front end


adam
MODERATOR

2 years ago

that way you can implement a queue system so users aren't shown a timeout page while waiting for a slot in the api


adam
MODERATOR

2 years ago

You can also host multiple replicas of that API that all communicate with one frontend to boost your capacity


lowzyyy
HOBBYOP

2 years ago

Users just start and end bot. They have separate endpoints to get info about the bot's work


adam
MODERATOR

2 years ago

Ah so this is an api?


adam
MODERATOR

2 years ago

you're saying bot, discord bot?


lowzyyy
HOBBYOP

2 years ago

Search is actually not time intensive, the work with browser is slow, running scripts for every item


adam
MODERATOR

2 years ago

The same point I made about apis and replicas applies here then, you can have multiple replicas running the same api to alleviate bottleneck


lowzyyy
HOBBYOP

2 years ago

I am trying to understand what horizontal scale actually mean. What does it solve in my case?


lowzyyy
HOBBYOP

2 years ago

If i can spawn 100 threads from the single app, what replica help with this


adam
MODERATOR

2 years ago

Multiple apis running at once that all communicate with a user through the same link


adam
MODERATOR

2 years ago

You cannot spawn 100 threads


brody
EMPLOYEE

2 years ago

js uses a single thread with an event-loop. In this way, Node can handle 1000s of concurrent connections without any of the traditional detriments associated with threads.


adam
MODERATOR

2 years ago

You can overload a single process with workers, but you will face worker timeout. Horizontal scaling solves this by allowing you to have multiple instances of the API that all communicate with the user through one link that can have many workers each


adam
MODERATOR

2 years ago

This isn't about connections, there is a large amount of processing needed for each request


adam
MODERATOR

2 years ago

^


brody
EMPLOYEE

2 years ago

same principal applies


adam
MODERATOR

2 years ago

Sure, but there's a limit to how many requests can be served at once if each is eating a large portion of the available vCPU


brody
EMPLOYEE

2 years ago

yes there always a limit, but it isnt necessarily tied to the vcpu count


adam
MODERATOR

2 years ago

In this case, it likely is


adam
MODERATOR

2 years ago

@lowzyyy Horizontal scaling will help you by adding 8 more vCPU available for each replica you create. A new API process will be spawned in the new container, allowing for more users to be served


adam
MODERATOR

2 years ago

Does that answer your question?


brody
EMPLOYEE

2 years ago

not directly its not


lowzyyy
HOBBYOP

2 years ago

so limit is not 32 cpu if i use replica?


lowzyyy
HOBBYOP

2 years ago

10 replica = 80cpus?


adam
MODERATOR

2 years ago

On the hobby plan the limit is 8vCPU per container


adam
MODERATOR

2 years ago

It's important for your understanding that they're virtual CPUs, not physical ones


adam
MODERATOR

2 years ago

but effectively yes


lowzyyy
HOBBYOP

2 years ago

I am on hobby, but i am planning this for a company that i work for


adam
MODERATOR

2 years ago

Ah I see. In that case, each replica will give you an additional 32vCPU


lowzyyy
HOBBYOP

2 years ago

as far as i understand main node js thread can spawn workers, and then main thread can take requests for some endpoints for any user, so 1 thread for MAIN and 31 thread for workers?


lowzyyy
HOBBYOP

2 years ago

workers should not eat cpu time for main thread


lowzyyy
HOBBYOP

2 years ago

my initial question was: if i have 10 users now, and suddenly i get 50 more all at once, how can i or can i dynamically get one replica and then shut it down when i dont need it


lowzyyy
HOBBYOP

2 years ago

Or should i predefine how much in advance can i have


lowzyyy
HOBBYOP

2 years ago

Because today i can server 50, and in three days i can serve 150. I feel like i should need to manually restart the service and increase replica or i if Brody is right, i might wont need another replica at all if workers can intertwine. It will be slower for sure but maybe is acceptable?


brody
EMPLOYEE

2 years ago

nodejs is a non blocking event loop, as mentioned, please do some research on the topic


lowzyyy
HOBBYOP

2 years ago

try to compute prime numbers in main thread and tell me if its non blocking


brody
EMPLOYEE

2 years ago

google it if you dont belive me, as ive asked you to do many times


brody
EMPLOYEE

2 years ago

besides, that completely different


lowzyyy
HOBBYOP

2 years ago

if i have operation that can be offloaded to c++ it will be, but if its not it will block


brody
EMPLOYEE

2 years ago

please do some research on the topic


lowzyyy
HOBBYOP

2 years ago

i think we have misunderstanding


lowzyyy
HOBBYOP

2 years ago

adam understood me i think


brody
EMPLOYEE

2 years ago

respectfully, the misunderstanding is on your side of things


lowzyyy
HOBBYOP

2 years ago

please try to explain so i can understand. Google it up wont help me understand


lowzyyy
HOBBYOP

2 years ago

what part i dont understand


brody
EMPLOYEE

2 years ago

as mentioned, explaining how node works is outside of the scope of these threads


brody
EMPLOYEE

2 years ago

simply put, how many "things" you can do at once is not directly limited by the vcpu count, and if it is, you have done something incredibly wrong.


adam
MODERATOR

2 years ago

hey sorry had to get to a meeting


adam
MODERATOR

2 years ago

it does sound like you don’t have a full understanding of the tools you are using, so it will be difficult for you to predict performance issues


adam
MODERATOR

2 years ago

I suggest you get the app hosted, test it, and address any issues you find from there


adam
MODERATOR

2 years ago

load testing is what you’ll want to do. if you’re unable to hit a high peak without performance degradation, we can discuss potential solutions


lowzyyy
HOBBYOP

2 years ago

fetch new items -> open new page for every item in chrome and do some work
By the time i finish with browser it's time to fetch again new items and do the thing again, and so on


lowzyyy
HOBBYOP

2 years ago

Isn't that the only way to do the thing. One worker per one user?


adam
MODERATOR

2 years ago

Only you have a full understanding of the problem you’re trying to solve


adam
MODERATOR

2 years ago

you could set up a task system such that a user sends in a request, then multiple workers work on multiple pages at once


adam
MODERATOR

2 years ago

the user’s request response time would be quicker


adam
MODERATOR

2 years ago

And you would also be able to service multiple users at once as workers can swap between users according to capacity as they complete jobs


adam
MODERATOR

2 years ago

Ultimately, it's up to your implementation


lowzyyy
HOBBYOP

2 years ago

that is the problem, as far as i know, you cannot execute scripts on multiple pages. In chrome, if you switch focus to one page, script pause on the other, so i can only do sequential job, or open new chrome instane for every item which would we be very memory inneficent


lowzyyy
HOBBYOP

2 years ago

User just read info from db, they just control when to start and stop the bot


adam
MODERATOR

2 years ago

Sounds like you have it all figured out then


adam
MODERATOR

2 years ago

Only thing left to do is host, load test, and see what happens


adam
MODERATOR

2 years ago

I’ve suggested that a few times as well, we’re on the same page


Loading...