Ensure a single instance running based on business key

I have a workflow where I receive files and based on some ids in the files I want to trigger a workflow-per-id. I can create a business key like ‘{id}-{date}’ and I want to make sure I only create a single workflow per business key - is there a way to do that in a way that avoids race conditions? We can receive files in close succession and files may contain overlapping IDs, so if I have to check if an instance already exists and then start the workflow you can see how there’s a race condition there…

What kind of restult would you like to have happen if a request comes in without a unique business key, Would you like to throw an error, or just ignore the request?

I’d want to know if a non-unique request comes (I guess that means an error is preferable). One thing I didn’t clarify is that I need to make sure I only start one process with that business key, including already finished processes. So if I start a process with bk ‘1-2021/8/17’, that process finishes and then I receive a request with that same bk, I still want to fail when starting the process.

Alrighty.
The easiest way to do this is by creating an execution listener on the start even of the process.
When a request comes in the listener will trigger it can run a query against the process instances history looking for any records that match the business key and process definition key and can throw an error if anything is found.

So, Because this would be run in the same thread as the request an error would be sent back to the originator of the request.

Thanks for the reply @Niall!

I’m trying to imagine how that works but I’m not sure it solves the race condition problem in my case. I’ve drafted a simplified workflow to try and illustrate my case:

Clarification: ‘Trigger next workflow’ is a multi-instance based on the IDs.

Let’s say file1 arrives containing IDs A and B and file2 arrives (in close succession) containing the same IDs - if the workflows run queries against the process instances history at the start there’s still a chance that they then go to the ‘Trigger next workflow’ task and trigger overlapping workflows no?

My hope is that when I get to the ‘Trigger next workflow’ task, there is a way to try and trigger the next workflow which succeeds if the business key is unique and fails (throws an error perhaps) if a workflow with that business key has already been created in the past, so I can then throw a BPMN error.