Prevent duplicate process instances from running at the same time

Context

Please consider the following scenario:

  • External applications are supposed to start new process instances based on messages (Message Start Events) that are being sent to Camunda via the REST API.
  • The external applications do not know if a message will actually start a process or not and they also do not know which process instances/processes are already running - and they’re not supposed to. We want to have a very loose coupling between Camunda and the external applications.
  • In general, we have some long running processes (weeks to months) and it can happen that a message, that has started a new process instance of a specific process before, is triggered again from some external application, while the original process instance is still running.

Goal
We want to prevent a new process instance from being started for the same processDefinitionKey and businessKey, if another process instance is already running across all versions.

Issue
At first, we have considered adding a unique database constraint for the processDefinitionKey and businessKey. This has also worked in the beginning, but we have recently stumbled across a new issue, if the processDefinitionKey is too long: If the proc_def_id_ is longer than 64 characters, Camunda will only use the nextId as the actual value for proc_def_id_.

So if the processDefinitionKey is Foobar, the proc_def_id_ will be set to Foobar:1:81718b03-f03f-11eb-b61f-0242ac110002 for every new process instance that is started.

For the constraint, that I’ve mentioned above, we are only considering the first part of the proc_def_id_ (the Foobar), as this also prevents other versions of the same process definition from being started.

Now if the processDefinitionKey is longer, e.g. FoobarFoobarFoobarFoobarFoobar, the proc_def_id_ will be set to nextId only (or to 81718b03-f03f-11eb-b61f-0242ac110002 considering the example from above), and no longer contain the processDefinitionKey and the version, also see here.

This now makes it impossible to keep new process instances from being started if the same process is already running in a previous version.

Question
Is there any pattern or approach that you can recommend to have these kind of unique process instances running for the same processDefinitionKey, the same businessKey and across different process definition versions?

We are aware, that this could potentially be handled directly on a modeling level, e.g. via an event subprocess as described here or via a script task, as described here.

But we would like to have some general pattern or approach that can be applied on a global scope rather than having to model this into every single process definition.

FYI: We are only using Camunda via the REST API.

Thanks!

1 Like

I have a similar requirement of not running multiple instances of the same process definition and another criteria.

My thought was to use a process start event listener which searches for running instances based on the criteria, if the search result is empty, no instance is running. If it’s applicable for all workflow definitions one could use a global listener, if it only applies to some definitions, the listener would need to be added in each model but that would be pretty straight forward. You could even create your own start event in the modeler palette which has this listener already configured…

But would also be happy hear more alternative approaches.