Answer the question
In order to leave comments, you need to log in
How to avoid node js overload?
I have a telegram bot, when entering a command, it can think for 10 seconds due to the fact that several thousand objects can be processed on the server at that moment. In the file, the bot is connected via require.
file.js <- thousands of objects are processed here and a bot is immediately connected to send messages.
Answer the question
In order to leave comments, you need to log in
Transfer processing of objects (if it is background) to a separate thread. If not background - seriously think about the architecture.
To run Node, use PM2 "instances": "max"
- the maximum number of threads; "exec_mode": "cluster"
- work in a cluster; "node_args": "--max-old-space-size=512"
- memory limit so that Node does not go beyond its limits.
We learn the mat part. NodeJS is executed in an event-loop, which implies some kind of single-threading with "breaks" and queuing of processing. If you do not interrupt, then you are running some operation for a long time - it means you are using nodejs incorrectly - it is not designed to perform resource-intensive tasks in a single thread. If your run queue exceeds the critical level for business logic, then you need to parallelize the processing of incoming messages. And to control all this - use message queues organized on any software (at least the same node / radish / rabits / nuts). You also need to find out where you have a bottleneck from the above
PM2 is a tool that allows you to run multiple instances of an application, some kind of basic load balancing system.
To work with third-party services with which delays may occur and files are separated into separate processes (workers). And sometimes not only those that work with third-party services. In general, there is a worker that simply accepts some requests and puts them in a queue. The queue is monitored by other workers, each with their own specialization, functionality, and get tasks from the queue and process them. If the worker has fallen, then the task is returned to the queue, the worker is restarted. But the main service does not fall from this and in general it does not affect it. Processing queues are organized, for example, using the RabbitMQ tool
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question