We have a master-detail relationship with Account and a custom object. We have created a roll-up summary to count the number of custom object records for the account to help us with scaling some processes because in some cases we will have more than 50k records meeting the criteria for the roll-up. Moreover, we have several future jobs as part of the trigger for the custom object that would update a few fields on the custom object. Now, when we have larger batches of custom objects inserted or updated, we sporadically see some of the future jobs fail to update the record. So we are wondering if calculating the roll-up summary has had any complications (e.g., locking records or the master record) and want to know how it works behind the scenes.
1 Answer
There are following issues in your design.
Account data skew:
If an Account record has more than 10,000 child records then it is called Account data skew. It can also be broadly called as Parent-Child data skew.
So, any updates to a single child record, Salesforce locks the parent Account which is called Record level locking. Now if you try to update child records through some batch jobs (or through data migration) then system tries to lock the same parent. So, if batch jobs are processed in multiple threads and in parallel mode then Salesforce throws the error message "unable to lock a row".
Secondly, you are having a roll up summary field on Account, so during calculations, Salesforce locks the parent account even if the amount is not getting updated at Account level.
Solutions:
Single Account record should never have more than 10,000 child records. Better to distribute the child records in different accounts.
When you perform batch jobs that should be done in serial mode and reduce the batch size, so that it can acquire a lock on Parent account.
You need to incorporate retry mechanism if you are getting failed to acquire a lock and update.
Refer following documentation
Designing Record Access for Enterprise Scale where you will find about data skew related scenarios and solutions.
Record Locking Cheat Sheet where you will refer
Object specific locking behavior
where Parent is getting locked when child is updated.Custom Configuration Locking Behavior
where you will findRecord with rollup summary field
- Master record(s) locked regardless of whether the roll-up summary’s target field has a value or the value has changed during an update. This behavior is the same on a roll-up summary on a standard object, such as Account
-
Thanks Santanu. Looks like the link to "Designing Record Access for Enterprise Scale" is broken.– JorjaniCommented Jul 5, 2017 at 13:31
-