Lock workflow activity
Summarize
Summary of Lock workflow activity
The Lock activity in ServiceNow workflows ensures that only one instance of a workflow can proceed past a specific point at a time by preventing other concurrent instances from continuing until the lock is released. This is critical when multiple workflow instances trigger simultaneously, such as when multiple records are created in quick succession.
Show less
The lock is acquired using a unique key, and it can be explicitly released by an Unlock activity or automatically released after a specified maximum duration. If a lock is already held by another workflow instance, the current instance will repeatedly attempt to obtain the lock up to a configurable maximum number of attempts.
Key Features
- Concurrency Control: Prevents multiple instances of the same workflow from proceeding beyond the lock point simultaneously.
- Lock Acquisition Attempts: Configurable maximum attempts and delay between attempts to obtain the lock.
- Automatic Lock Release: Locks are automatically released after a maximum duration to avoid indefinite blocking.
- Explicit Lock Release: Unlock activity can be used to release the lock programmatically.
- Recommended Timer: A one-second timer activity before the Lock is recommended to avoid rare thread-related conflicts.
Practical Considerations
- Avoid placing wait-related activities (e.g., Approval, Task, Timer, Wait for Condition/Event, MID server activities) between Lock and Unlock activities, as these can delay lock release and cause workflow stalls.
- The lock is tied to the execution thread, so the timer activity helps ensure that each workflow instance runs on a distinct thread for reliable locking behavior.
Key Outcomes
- Success: The workflow instance obtains the lock and can proceed while others wait.
- Failure: If the workflow instance cannot obtain the lock after the configured maximum attempts, it times out and the activity fails.
Configurable Parameters
The Lock activity behavior is controlled through these input variables:
- Key: Unique identifier for the lock, used by both Lock and Unlock activities.
- Max. duration: Maximum time the lock remains held before automatic release.
- Max. attempts: Number of times to try acquiring the lock before failing.
- Delay between attempts: Wait time after a failed attempt before retrying.
Activity States
- Waiting: Workflow is attempting to obtain the lock.
- Finished: Lock successfully obtained.
- Timeout: Lock attempts exceeded without success; activity failed.
The Lock activity prevents other instances of this workflow from continuing past this activity until the lock is released.
Several instances of the same workflow may run concurrently. For example, if a workflow triggers when a record is added to a particular table. That workflow triggers multiple times if multiple records are added one after the other, once by each record insertion. You can use the lock activity to ensure that this instance of the workflow has finished one or more activities before any other instance of the workflow can proceed.
A workflow can explicitly release a lock with the Unlock activity. The lock may also be released when the Max duration is reached.
When an instance of the workflow reaches the Lock activity, it attempts to obtain a lock using the key specified in the lock activity. If another instance has already obtained the lock and has not yet released it, this lock attempt fails. The instance continues trying to obtain the lock until Max attempts has been reached.
We recommend placing a one-second timer activity before the lock activity. This helps prevent a rare condition in which the lock activity may not be able to distinguish one workflow instance from another. This condition can occur because the entity owning the lock is not the specific workflow instance, but rather the code-execution thread in which that instance is running. In most cases, each workflow instance runs on a different thread. Adding a timer activity ensures that this is the case.
- Approval activities
- Task activities
- Timer activities
- Wait for condition activity
- Wait for WF Event activity
- MID server activities such as a PowerShell script
Results
| Result | Description |
|---|---|
| Success | The activity successfully obtained a lock. This instance of the workflow can proceed past this point, but other instances cannot proceed until the lock is released. |
| Failure | After attempting to obtain the lock Max attempts times, the activity could not obtain the lock. |
Input variables
Input variables determine the initial behavior of the activity.
| Field | Description |
|---|---|
| Key | A unique mutex key. The Unlock activity activity uses this key to release the lock. |
| Duration | |
| Max. duration | The maximum time the lock persists. The lock is released after it reaches this duration. Releasing a lock this way is equivalent to running the Unlock activity. |
| Lock attempts Specify how the activity behaves if the lock attempt is denied. If the final lock attempt fails, the activity state is set to 'timeout' and the activity result is set to 'failed'. |
|
| Max. attempts | Specify the maximum number of times the activity may attempt to obtain the lock. |
| Delay between attempts | The amount of time required after a failed lock attempt before another lock attempt is allowed. |
States
The activity state tells the workflow engine what to do with the activity.
| State | Description |
|---|---|
| Waiting | The workflow engine is waiting to obtain a lock. |
| Finished | The activity successfully obtained the lock. |
| Timeout | The activity could not obtain a lock within the number of attempts specified by the Max. attempts input variable. |