ApexAdvancedbatchperformancedml
Resolve row lock contention when parallel batches update same parent Accounts
Real World Scenario
Two parallel batch jobs updating Account.Last_Activity_Date__c from Task and Event triggers cause UNABLE_TO_LOCK_ROW errors during business hours.
Expected Answer
• Partition batches by Account Id range or region preventing same-parent collision
• Serialize updates via single batch job consolidating activity timestamps
• FOR UPDATE in query only when necessary—avoid locking parent rows in execute
• Retry with exponential backoff on lock exception in execute catch block
• Schedule heavy Account updates off-peak; throttle parallel batch count
• Consider Platform Event subscriber batching parent updates debounced per Account
• Monitor row lock errors in custom log correlated to batch job Id
Follow-Up Questions & Answers
Click to expand — each follow-up includes a direct, interview-ready answer
Direct answer: Partition batches by Account Id range or region preventing same-parent collision Also consider: Serialize updates via single batch job consolidating activity timestamps In practice: FOR UPDATE in query only when necessary—avoid locking parent rows in execute Optimize for scale and operational observability.
Architect Perspective
Parallel batch on shared parents is race condition—partition or consolidate updates.