Hi all, a little topic for discussion preparing us for a new year of Node-RED fun.
How do we optimise the running of Node-RED?
This is mainly aimed at people using a Pi but should be generally useful for anyone who needs to squeeze out some extra performance. I certainly don't have all the answers so please chip in and I will try to collate a decent list that maybe can then go into the documentation.
Here is my starter list:
- Don't run an OS desktop on the same device as Node-RED. Especially on a Pi, the desktop consumes a LOT of resources.
Check for SWAP usage (using
topor similar), because a Pi uses an SD-Card for storage, this is extremely slow. But even on other systems use of SWAP significantly slows down the system and limits Node-RED performance.
nicevalue. Dave's installer for the Pi sets niceness at
10which is lower than the average. Try setting to zero or even a negative number to boost the priority of the Node-RED process compared to others on the system. This may, of course, have a negative impact on other processes running on the same machine. This is changed in the systemd service file that runs Node-RED on bootup.
Change the node.js parameter
- Change from using an SD-Card to an SSD. On most Pi's you have to boot off an SD-Card (except for the latest Pi4's) but you can put everything else on an attached SSD (via a USB adapter). Even a spinning hard drive will be faster than the SD-Card. This will really only make a difference if you are doing lots of filing system read/write activity. If you do this, move the log file locations as well as this is often the main load on the SD-Card.
- Move some services to another device. If your Node-RED system device is being pushed hard, it may help to use a second device and move some services to it. Note, however, that this now means accessing services over your network which itself will be slower. However, overall performance may improve by relieving the pressure on your Node-RED device. Certainly if you need to run a desktop, use a second device not the one running Node-RED. Databases and MQTT brokers would also be reasonable candidates for moving.
- Turn off unnecessary services. Easy to say, not always easy to identify suitable candidates though. Do some searches online for optimising your device. Take it slow and remove one thing at a time. Keep good notes.
- Create (or find) an optimised lightweight OS. Really, this is one for people more experienced with managing operating systems. Lightweight versions might be missing critical applications that you will need to know how to identify and install.
- Check the size of context/flow/global variables. If you let them get too large, you could run out of memory and force Node.js to garbage collect. In general, you should always prefer passing data using the msg passing that is the core of Node-RED.
- Avoid having 2 wires out of a node. When you direct the msg output of a node to 2 or more downstream nodes, Node-RED has to "clone" the msg which is a relatively slow operation.
- Prefer Function Node's over JSONata. Currently, JSONata is typically several times slower than a function node. However, if you don't need to optimise, don't worry about this. In most cases you will not see an impact.
- Avoid Function Node's. Function Node's use Node.js's virtual machine facility which wraps the code in a protective layer to help protect the underlying service. This has an overhead of course. However, in the majority of cases you will not see any difference in performance, only in extreme cases.
rooms.bathroom). In addition, with an Object you can still use it as an array with
Object.values(myobject)and can even get an array of the keys using
Object.keys(myobject). So in general, Objects are more flexible and easier to use.
Above all - do not optimise to early! Also, don't optimise unless you need to. Keeping things standard makes them easier to manage.
I am sure that there are a lot more performance tips out there so please contribute to the discussion.
- JK 2020-12-31: Added bullets 6 & 7 to the OS section, added warning not to over-optimise.