Hey Losanters,
I’m looking for suggestions on how to handle a race condition in one of my workflows. I have a workflow with a File Trigger.
The workflow reads the uploaded file, performs some calculations, and then sets the device state. One of the device state values is a total cycle counter.
I need this total cycle counter because I want to keep track of the total number of cycles over several years, while the normal time-series data is only stored for one year. So I cannot simply calculate the total by querying all historical cycle data.
The workflow currently does the following:
Gets the previous total cycle count from the device state. Adds the cycle count from the uploaded file. Writes the updated total cycle count back to the device state.
This works fine when only one file is uploaded at a time.
The problem occurs when multiple files are uploaded at nearly the same time. The workflow runs in parallel, so both workflow runs read the same previous total before either one has written the updated value.
For example:
Previous total cycle count = 100
File 1 added cycle count = 20
File 2 added cycle count = 30
Both workflow runs start at the same time and both read the previous total as 100.
So the results become:
File 1 writes: 100 + 20 = 120
File 2 writes: 100 + 30 = 130
The final total cycle count then becomes either 120 or 130, depending on which workflow finishes last. The correct value should be 150.
One approach I am considering is to stop updating the total counter directly from the File Trigger workflow. Instead, each File Trigger workflow would write the calculated cycle increment to a Data Table, for example:
deviceId | fileName | cycleIncrement | processed | timestamp
Then a separate workflow could process these table rows one by one, add each increment to the current total cycle counter, update the device state, and mark the row as processed.
Does this sound like a good approach, or is there a smarter/easier way to handle this kind of cumulative counter update safely?
Sorry for the long post, but I wanted to provide enough detail to clearly explain the problem.
Thanks in advance!

