|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: Making flows asynchronous by default |
| 4 | +author: nick |
| 5 | +description: Changes are coming in Node-RED 1.0 to how messages are routed in a flow. Find out what its means to go asynchronous. |
| 6 | +--- |
| 7 | + |
| 8 | +In Node-RED 1.0, we are changing the way messages pass between nodes from being |
| 9 | +synchronous to being asynchronous. This will, in some cases, change the relative |
| 10 | +order messages are handled in flows. This post explains what we mean by synchronous |
| 11 | +and asynchronous, why we are making this change and what effect it will have. |
| 12 | + |
| 13 | +### Node.js Event Loop |
| 14 | + |
| 15 | +To understand how messages pass through a Node-RED flow, we first need to take |
| 16 | +a slight detour into how Node.js works. |
| 17 | + |
| 18 | +As JavaScript is a single-threaded language, it can only do one thing at a time. |
| 19 | +But there are times when it needs to do something that will take a while, such |
| 20 | +as make an HTTP request or write something to a file. If it did this work on its |
| 21 | +only thread, then it would block anything else from happening and the performance |
| 22 | +would be terrible. |
| 23 | + |
| 24 | +Instead, Node.js passes those sorts of actions to the underlying operating system, |
| 25 | +which is multi-threaded, and registers a callback that will be called when the |
| 26 | +action completes. This is the idea of the [Event Loop that is at the heart of Node.js](https://nodejs.org/uk/docs/guides/event-loop-timers-and-nexttick/). |
| 27 | + |
| 28 | +To overly simplify it, you can think of it as a queue of work. Each time the Event |
| 29 | +Loop runs, it takes the next piece of work and runs it, which may in turn cause |
| 30 | +new events to be added to the queue. |
| 31 | + |
| 32 | + |
| 33 | +### Synchronous vs Asynchronous |
| 34 | + |
| 35 | +When we talk about Synchronous and Asynchronous code, you can think of it like this: |
| 36 | + |
| 37 | + - Synchronous code all runs in a single pass of the Event Loop. |
| 38 | + - Asynchronous code starts with a piece of work in the Event Loop, that will in |
| 39 | + turn add another piece of work to the Event Loop that will be run in a future |
| 40 | + pass. |
| 41 | + |
| 42 | +The key thing to understand is that with Asynchronous code, once it has added more |
| 43 | +work to the Event Loop and its current work has 'finished', other pieces of work |
| 44 | +can run. |
| 45 | + |
| 46 | +### Synchronous messaging passing |
| 47 | + |
| 48 | +Since the start of Node-RED we have used synchronous message passing between nodes. |
| 49 | + |
| 50 | +This means when a node calls `node.send(msg)`, that call passes to the next node's |
| 51 | +`input` event handler, which does its work and calls the next node's event handler |
| 52 | +and so on. If each node's event handler is purely synchronous code, then a message |
| 53 | +will passing all the way down the flow in a single pass of the Event Loop. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +If one of the nodes contains asynchronous code, such as the HTTP Request node, |
| 58 | +then the current pass ends at that node and the next piece of work can |
| 59 | +start. It is also possible in that case for the second message to overtake the |
| 60 | +first if its asynchronous work completes before the first message's. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +### Branching a flow |
| 65 | + |
| 66 | +When a flow branches, for a synchronous flow, each branch will be completed in turn. |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +This does lead to a slightly counter-intuitive behaviour when you add Debug |
| 71 | +nodes at each point along the flow. In the following diagram, note the order |
| 72 | +in which messages arrive at the Debug nodes. |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +### Changing to Asynchronous message passing |
| 77 | + |
| 78 | +With Node-RED 1.0, we are changing the message passing to be asynchronous. That |
| 79 | +means when a node calls `node.send(msg)`, the work to call the next node's `input` |
| 80 | +event handler is put onto the queue to be called in a later pass of the Event Loop. |
| 81 | + |
| 82 | +For those more familiar with the Event Loop, we use `setImmediate()` so they |
| 83 | +actually get invoked during the 'check' phase of the current Event Loop iteration. |
| 84 | + |
| 85 | +Looking back at the single branch, entirely synchronous, flow we started with, the |
| 86 | +messages will now make equal progress through the flow. |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +When a flow branches, the branches will be evaluated in 'parallel'. |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +Which also means a flow with Debug nodes at each point, will log the message |
| 95 | +is the expected order. |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +### Why is this change needed? |
| 100 | + |
| 101 | +Making the message passing asynchronous is needed for a number of reasons. |
| 102 | + |
| 103 | +#### Pluggable Message Routing |
| 104 | + |
| 105 | +One of the features on the roadmap, coming after 1.0, is the ability to plug |
| 106 | +custom code into the message routing path. That custom code may need to do |
| 107 | +asynchronous work - such as sending messages over the network in a distributed |
| 108 | +Node-RED environment. |
| 109 | + |
| 110 | +#### Node timeouts |
| 111 | + |
| 112 | +We are looking at how the runtime can better monitor messages passing through a |
| 113 | +flow and provide a standard way to timeout any node that takes too long. |
| 114 | + |
| 115 | +With the current synchronous model, the time it takes a node to handle a message |
| 116 | +is the time it takes to run its own code, plus the time it takes each subsequent |
| 117 | +node to handle the message it is passed. |
| 118 | + |
| 119 | +Looking at this branching flow again, lets say we want each node to take no more than |
| 120 | +5 seconds to handle a message. |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +If everything is synchronous, then the second node is not 'finished' until both |
| 125 | +the yellow and red messages have reached their Debug nodes. If each node takes |
| 126 | +2 seconds to process the message, then that second node will take 10 seconds |
| 127 | +to process its message and will get timed out, even though no individual node |
| 128 | +has taken longer than 5 seconds. |
| 129 | + |
| 130 | +With asynchronous message passing, we can stop the clock as soon as the node has |
| 131 | +queued up the work for the next nodes. |
| 132 | + |
| 133 | +There's more work to be done after 1.0 to build on this capability, but the shift |
| 134 | +to asynchronous is a key first step. |
| 135 | + |
| 136 | +We'll have another blog post up soon that covers the changes to the node messaging |
| 137 | +api in 1.0 to further support this timeout behaviour. |
| 138 | + |
| 139 | +#### Better I/O scheduling |
| 140 | + |
| 141 | +If you recall the purpose of the Event Loop is to allow the Node.js runtime to |
| 142 | +perform I/O in the background and call back when there is something to be done. |
| 143 | + |
| 144 | +But those callbacks can only happen if the Event Loop is able to make regular |
| 145 | +progress. If you have a large piece of synchronous code, then you are preventing |
| 146 | +those callbacks from being called. There is a trade off here. Purely synchronous |
| 147 | +code is going to be faster, but it does starve the Event Loop. |
| 148 | + |
| 149 | +By splitting a large synchronous flow into smaller asynchronous chunks, it allows |
| 150 | +the Node.js runtime to better schedule all of the other activity in the runtime. |
| 151 | + |
| 152 | + |
| 153 | +### Will this change break my flows? |
| 154 | + |
| 155 | +For most flows, this change will not alter their behaviour in any way. We have |
| 156 | +always said no assumptions should be made about ordering once a flow branches. |
| 157 | + |
| 158 | +That said, there are bound to be flows out there that have exploited the observed |
| 159 | +ordering and make some of these assumptions. So care should be taken when upgrading |
| 160 | +if you know your flows make such assumptions. |
| 161 | + |
| 162 | +This is why we're making this change as part of the 1.0 release. We've worked |
| 163 | +hard to ensure Node-RED remains backwards compatible between releases, but sometimes |
| 164 | +we simply have to make a change that could have an impact. We don't make those |
| 165 | +changes lightly and we can only make them as part of a major release. |
| 166 | + |
| 167 | + |
| 168 | +#### Keeping things synchronous, for now |
| 169 | + |
| 170 | +Given the potential for this change to alter how *some* flows behave, we're |
| 171 | +introducing a new setting that will restore the synchronous delivery mode: |
| 172 | + |
| 173 | +``` |
| 174 | + runtimeSyncDelivery: true |
| 175 | +``` |
| 176 | + |
| 177 | +Given the features in the roadmap that will require asynchronous delivery, this |
| 178 | +setting is *not* a long term solution. Consider its usage as instantly deprecated. |
| 179 | +It is only intended as a stop-gap measure to allow affected flows to be upgraded |
| 180 | +to 1.0 before they are updated to handle the new asynchronous mode. |
0 commit comments