Minimizing Memory Usage via Backpressure
push() returns a new Promise each time it is called, thus consuming memory. Depending on your application, it may be necessary to limit calls to push() when the queue is full if you are unable to control the number of calls to this method.
For example, consider the following constraints:
1. Up to 5 tasks can call push() and immediately continue their work. Subsequent callers will wait until a worker has finished.
2. Up to 2 workers can execute at the same time
Although it appears that resources are properly constrained in this scenario, if push() is called, say, 1,000 times a second, and the workers take longer than 1 second each, the process will likely run out of memory. One solution to this scenario is backpressure.
No form of backpressure is a silver bullet. External systems must handle errors and retry.