Optimizing Node-RED
I’ve used Node-RED for several years for various odds and ends. Node-RED’s history started with the IBM Emerging Technology Services team and now is a part of the OpenJS Foundation.
The dashboard and flow editor are simple to setup and work with. The limit of node complexity is directly proportional to the developer, which makes it an ideal frontier to push. Pushing those limits can expose issues and that is where this post comes in.
Hosting Node-RED by itself on a dedicated system may not be the rule. It will likely tenant with other services and processes. This is important to take into account because if no orchestration is in place, services may not play in concert, especially when resources are limited. This turned out to be the case in two scenarios I experienced. The first was a system with limited physical memory, the second was multiple services that use resources heavily.
The issue with the first case was that Node-RED used all of the memory. Although the system only had Node-RED running on it, this was enough to hang the system. It was too taxed from memory space taken by Node-RED that core functions hung.
The second case was a system that had plenty of resources, but ran other processes. After a period of time since reboot, the system would gradually loose free memory space. Eventually another service would hang and systemctl status for that service would show that an OOM had occurred.
Forensics showed the gradual memory consumption of Node-RED. It eventually consumed 25% of available memory on the system. Unfortunately, other services suffered the fault when they crossed the threshold. This lead to a setting defined in the Node-RED services config.
$ cat /lib/systemd/system/nodered.service # systemd service file to start Node-RED ... Environment="NODE_OPTIONS=--max_old_space_size=1024" ...
Changing the “–max_old_space_size” value to “512” will consume less than 15% of available memory and hopefully prevent further hangs.
Sometimes the issue might not be a service that can be referenced with systemctl status. A process could end up in a zombie state. Using this command should provide details about that process.
$ ps aux | grep ' Z '
In any event, optimizing the services to use set allotments of memory might be the orchestration that was needed. A stable and reliable system can be music to ones ears.