I have a node.js app with 10k-100k concurrent setTimeouts running at any given time. (They are all 5 minute duration.) The callback is pretty trivial, just an HDECRBY in redis. I have not hit any performance issues yet with this, even on a t2.micro instance.
I know that I will run into issues if the callback functions can't get executed as fast as I'm setting the setTimeouts (obviously), but are there issues with having a high number of setTimeouts, per se? e.g., am I going to run in to a RAM bottleneck if I scale this up to, say, 1 million concurrent? 10 million?
For these types of questions, it is often useful to just go look at how node.js handles timers in the source code.
What you will find is that node.js keeps one or more linked lists of its own internal timer objects and all timers set to occur at the same time share one libuv timer. This means that zillions of timers all set to occur in a fairly specific time window will inevitably share many firing times and thus will share timer lists and thus will share many system timer objects.
This makes it less of an issue for having zillions of timer objects. Now, every timer object still requires some memory and not every operation in the timer implementation is of constant time though you can see in the comments below, they tried to make as many of them as possible to be constant time to allow large numbers of timers with still decent performance.
If you don't need absolute precision in exactly when the timer fires, you can probably cause your timers to coalesce and share timer objects more often by scheduling your timers only for specific time boundaries such as an even number of 100ms. This would be scheduling more of your zillions of timers for the same firing time and would allow node.js to put more timers into the same list that all share a single system timer. I don't know whether this is feasible with your timers or if it is even needed, but in studying how node.js works, it would increase efficiency. There would be both fewer timer lists internally to node.js and fewer system timers in libuv.
Here are some explanatory comments from the node.js code on timers that explains some more aspects of the design:
©2020 All rights reserved.