Workflow concurrency control
This feature is available in version
2.7.2
or later
A race condition in Casewhere occurs when two or more workflows share the same context (e.g., access shared data objects) and try to change the context state simultaneously. Having race conditions for critical business scenarios such as payment, booking, etc., is unacceptable. For instance, it must not be possible for two users to purchase the same product, or an invoice should not be paid more than once. Understand the challenge of dealing with race conditions, Casewhere provides a feature to control concurrency that works out of the box for developers.
Configuration
Casewhere allows you to control concurrency at activity level using exclusive lock. When editing an activity, you can navigate to Advanced Settings to turn on the setting Enable Exclusive Lock.
Other settings:
- Lock Context (optional): When the exclusive lock is enabled, Casewhere will only allow one activity to execute at a time, in the entire system. This is obviously not optimized for many scenarios. By providing a lock context, you can control the concurrency logic on a more granular level. For example, you can enable the lock on specific data objects to execute more activities as long as they're on different objects. The setting accept an expression using the same syntax with navigation rule. You can learn more about the rule syntax here.
- Waiting Timeout: Casewhere will release the lock if it lasts longer than the configured timeout, even when the activity is not finished.
Example
Imagine we are implementing a simple feature to count application message readers. The data model looks like below:
A message is accessible to many users, and every time someone reads a message, Casewhere will increase the counter as below:
The feature will not count correctly if many people are reading the same message at the same time. The value Read
will always be lower than the number of readers in reality due to the race condition, i.e., the counter is modified after being loaded by another workflow.
By enabling the exclusive lock on the Read activity definition, the feature will work as expected because there is only one activity is executed at a time. However, that also means the system only allow one person to read message at a time, even when they are reading different messages. To improve the user experience here, you can configure the Lock Context as below:
By configuring the Lock Context as described above, multiple users can read messages simultaneously, provided they are accessing different messages. For this setup to function correctly, ensure the Auto Commit Changes setting is enabled. This allows the executing activity to read the latest value of the Read
counter, which was saved by previously executed activities.