Node-red is a nodejs application which uses a single thread. What makes nodejs fast is the use of asynchronous functions.
Most programs will sit and wait for something to complete before continuing with the next task.
However nodejs applications start a task and then continue on with the next task.
They return to the previous task when they are notified that it is complete.
This way there is effectively no waiting.
This is what makes nodejs and node-red ideal for networked applications.
This is because with networked applications most of the time would be spent waiting for the network.
This also means that node-red is unsuitable for applications that require intensive processing as this will slow down the entire Workspace (All flows).
This doesn’t mean that you can use node-red for these applications but that you don’t put flows in that workspace that require quick responses.
Common Questions and Answers
How does the event loop handle Node-RED flows?
Node-RED flows are executed as a series of asynchronous tasks. Each node in a flow processes a message and then either passes it to the next node or completes execution. The event loop schedules these tasks efficiently, ensuring non-blocking execution.
What happens when a node takes too long to process a message?
If a node takes too long to process a message (e.g., performing a CPU-intensive task), it can block the event loop. This delay affects the performance of other tasks in the workspace.
How can blocking operations be avoided in Node-RED?
Blocking operations can be avoided by:
- Offloading heavy computations to external services.
- Using asynchronous APIs instead of synchronous ones when using external nodejs modules.
- Designing flows to be efficient and modular.
Can Node-RED handle parallel processing?
Node-RED is single-threaded by default, but parallel processing can be achieved by offloading heavy tasks to external APIs or use custom nodes designed for multi threading.
Related Tutorials and Resources
- You can find more about the event loop here.