General Node Red behavior?


I'm currently using Node Red to Create a complex application which involes many seperate Node-Lines in one Flow and the use of many Context Variables.
For that I have to understand how Node Red processes a Flow to be sure that my Data is consistent.

To get a better understanding what my problem is, I created a exapmle node which you can see in the Picture below:
In this Flow a Queue with different Tasks is stored as a Context-Variable. The Tasks are pushed in the Queue via POST Request. Every Second every Tasks in the Queue should be processed. After that every successful processed Task should be deleted.
To do this work I want to use as many Core Nodes as possible for easy understanding. Every time i want to change the queue I pull it from the Context variable, change it in the msg and then push the Data back to the context variable.

My problem now is that I don't know how my flow processes multiple "Lines". Is every Node-Line handled at one time or not?
So for example an POST Request is received and at the same time the Inject Node triggers. Will Node Red perform the first Line and then the other Line or will it perform both at the same Time and just switching between the Lines (Node 1 of Line 1, then Node 1 of Line 2, then Node 2 of Line 1, ...).
If it behaves like that my Data at the examle flow would not be consistent.

But for that I have a possible solution how to handle it but I'm also not sure if this works. My idea is to Create two Subflows. In each subflow it would pull the Data first and at the end it pushes the result back. But this only works if the subflow is being processed at ones and no other node in my flow does any work. Is this the case?

Can any one explain to me how this works in general?

If you rely on timing to ensure that your data is consistent, you will never have a stable solution. But to answer your question, the NodeJS event loop runs a single task at a time, until there is some piece of asynchronous code that "yields" control back to the main event loop. So yes, all of your "lines" of flow can run at the same time (effectively).

Please read this explanation for more details...

1 Like

Okay thank you for your answer.
I will put a Queue in Front so everything which writes to the same variable will be hold back until another task is finished.Thats the only solution I have to ensure that my data is consistent.

If anyone has a better solution please let me know.

I use node-red-contrib-semaphore when I have a section of flow that must only have one message flowing through it any one time. It automatically queues messages until the flow signals that the current message has completed. It works very well for me.

Or indeed don't use flow/global variables - keep all the data as properties on the msg.

1 Like

Even better.