Fetch and lock external tasks - concurrency

Hi all,

I’m trying to validate the expected behaviour while locking external tasks using a unit test as follow:

  • Using REST api to connect to an Engine with Camunda 7.6.
  • Start 100 instances of the process. This process creates an external task.
  • Create 5 threads that will fetch and lock 20 external tasks. Each thread uses an unique worker ID.
  • Validate that each thread receives 20 unique external tasks.

What’s happening is that only one thread is receiving tasks, the other ones are receiving zero.
Wondering if you can help me to spot what I’m doing wrong.

Thx in advance,


externalTask.bpmn (4.1 KB)
externalTask.bpmn (4.1 KB)
code.java.txt (5.7 KB)


The Javadoc for ExternalTaskService#fetchAndLock says:

May return less than maxTasks tasks, if there exist not enough unlocked tasks matching the provided topics or if parallel fetching by other workers results in locking failures.

Maybe that is what you are seeing.


Hi Thorben,

Thx for the reply. Looks that’s the issue, if i add a random sleep in a thread before doing the REST call, it gets the expected tasks. Do you know if locking failures are triggered as well if external workers fetch different topics?
At the moment, all threads are trying to fetch the same topic.


For different topics, no locking failures should occur. To provide some background: When executing fetch and lock, the process engine selects external tasks from the database that match the criteria (i.e. topic name matches, currently not locked). It then updates the lock state of these tasks. If this happens in parallel for the same task, only one fetch and lock invocation can “win” this task. This principle is called Optimistic Locking.


1 Like

Thx so much for the information!!