Locking and Concurrency Strategies
Based on Real Implementation Experience: This guide reflects patterns from high-volume portals, integrations, and batch jobs where locking issues are common.
Overview
Salesforce uses record-level locks to ensure data integrity:
- Multiple transactions updating the same records at the same time can cause
UNABLE_TO_LOCK_ROWerrors. - Poorly designed automations can create hot spots where many transactions compete for the same records.
This document describes strategies to avoid, detect, and handle locking issues.
Prerequisites
- Required Knowledge:
- Basic understanding of DML operations and transactions in Apex and Flows.
- Recommended Reading:
When to Use These Patterns
- High-concurrency environments (Experience Cloud portals, heavy API integrations).
- Batch and async jobs that process records also touched by users.
- Objects with roll-up summaries, parent-child dependencies, or sharing recalculation.
Core Concepts
Lock Scope
- Locks are taken on records being modified and sometimes on related records (e.g., parent records for roll-up summaries).
- Operations that update many related child records can increase lock scope.
Hot Spots
Hot spots are records that many transactions try to update at once, for example:
- A single
Accountwith thousands of relatedContactorCaseupdates. CampaignorOpportunityrecords updated by multiple jobs.
Patterns and Examples
Pattern 1: Orderly Processing by Parent Id
- Process child records grouped by parent Id.
- Use ordered queries and Batch Apex to reduce contention.
Pattern 2: Retry with Backoff
- Catch
DmlExceptionwithUNABLE_TO_LOCK_ROW. - Retry a small number of times with increasing delays (in async Apex).
- Log failures after max retries.
Pattern 3: Reduce Unnecessary Updates
- Avoid DML when no actual data change is required.
- Compare fields before performing updates to reduce lock frequency.
Edge Cases and Limitations
- Some locking behavior is not fully documented, especially around managed packages.
- High-volume sharing recalculation can exacerbate locking problems.
Related Patterns
Q&A
Q: How can I reproduce locking issues in a sandbox?
A: Use anonymous Apex and multiple concurrent test runs (e.g., via separate users or scripts) to simulate simultaneous updates to the same records. Also, run Batch Apex and interactive updates at the same time to mimic production concurrency.