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:

This document describes strategies to avoid, detect, and handle locking issues.

Prerequisites

When to Use These Patterns

Core Concepts

Lock Scope

Hot Spots

Hot spots are records that many transactions try to update at once, for example:

Patterns and Examples

Pattern 1: Orderly Processing by Parent Id

Pattern 2: Retry with Backoff

Pattern 3: Reduce Unnecessary Updates

Edge Cases and Limitations

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.