While researching a possible memory leak issue on a NodeJS 8 (KOA) microservice I read a post where it was suggested that periodically recycling Node Cluster workers (destroy old worker, then start a new one) is a good idea as it will clean up memory leak issues that a service may build up after many days or weeks of running.
Currently our microservice is running on a dual core (4 logical cores) server and spins up one worker per logical core (so 4 in total) using Node's Cluster service.
Questions:
1) Is it really a good idea (or even necessary) to recycle workers periodically? Is this considered to be a best-practice among the Node community?
2) What are the possible side affects of destroying a worker? Specifically I am concerned that a user may have their work interrupted should their currently used worker suddenly be destroyed? Or is Node robust enough that a destroyed worker processes will finish its task before being shut down and a new process is started.
FWIW: we are considering doing worker recycling off hours when (normally) no users would be active.
It is better to just find and fix the memory leaks. Here is a good post for how to do it.
Maintaining a Node.js project also consists of keeping dependencies updated. Always take into account that there can be issues not only in your own code but also in dependencies. So check if any dependencies have updates, if yes check what has been changed and update accordingly.
It is not a good practice to recycle workers periodically. It is better to fix the issues not create workarounds for them. Also it is not considered to be a best-practice among the Node community.
Node isn't that robust that it would wait for worker process to finish its task, if you don't make your code work that way. There are ways how you could make process manager wait till process has finished its work. Here is how it could be done with pm2.
As you brought out when destroying a worker your user may have their work interrupted. There is of course ways how to prevent this by disabling requests to worker beforehand and waiting till last request is handled.