+ Start a Discussion
Will CraigWill Craig 

Unable_to_lock_row - Optimal Serial Batch Size

Our org is going through a re-architecture - and large updates are causing row locks because many records are being related to a single parent record. While I can find descriptions of cause of the issue - which I understand - I don't see any guidelines on the optimal batch size to use when performing updates where the likelihood of row lock is very high. (all updates referencing a single parent). Is there a standard practice to reduce the number of records that end up in the asynchronous queue? Running batches at 500 records, serial is working fairly well - but I don't know if there is a specific limit of batch size that could be used to prevent row locks entirely.
Hi Will,

I've come across this issue before. There are a couple of things we did to reduce and in some cases completely prevent row locking with batch jobs. There are a number of suggestions mentioned here (https://help.salesforce.com/articleView?id=000229525&type=1), so I won't repeat those below. 
  1. Remove master-detail relationships between the problem parent/child objects by converting them to lookup relationships where possible.
    • If a child is being edited, then the parent gets "locked" resulting in the row locking issue.
    • Converting your master-detail relationships to a standard lookup field means you lose out on some of the functionality of a master-detail relationship (e.g. standard rollup fields), but these limitations can generally be overcome with customization. 
  2. Retry the records that failed to update through chained batch jobs
    • Use database.update with partial update enabled and record down the IDs of the records that failed to update because of unable to lock row, then in the finish() method of the batch job, re-run the batch job for those failed records. Use a counter to ensure the batch job doesn't keep retrying forever. I try to re-run the batch job 5 times before I send an email out to the system administrator letting them know of the issue. 
Hope that puts you on the right track!!