Sub topic 15
Performance and lock contention practices
This chapter assumes you can create an ingest job, upload CSV data, close the job, and read the results. It covers the habits that keep large loads fast and reduce failures that come from record locking.
Why loads slow down or fail
Salesforce splits your uploaded data into batches and processes them in parallel. Two batches that touch the same record, or records that share a parent, compete for a lock. When a batch cannot get a lock in time, its records fail with an error such as UNABLE_TO_LOCK_ROW. The failed rows appear in the failed results, and you can resubmit them.
Lock contention is the most common cause of avoidable failures in large loads. Most of the practices below reduce it.
Order data by parent
Rows that share a parent should sit next to each other in the file. Salesforce cuts the file into batches in order, so related rows then land in the same batch. Rows in one batch are processed together, so they do not block each other.
Suppose you load Contacts. Sort the CSV by AccountId before you upload it:
import csv
with open("contacts.csv", newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
fieldnames = reader.fieldnames
rows = sorted(reader, key=lambda r: r["AccountId"])
with open("contacts_sorted.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames, lineterminator="\n")
writer.writeheader()
writer.writerows(rows)The script writes \n line endings, which match "lineEnding":"LF" in the job definition.
Reduce other sources of locking
- Skip skewed parents. A parent with a very large number of children, such as one Account with hundreds of thousands of Contacts, is locked on almost every batch. Spread those children across several parents if your data model allows it.
- Turn off automation you do not need. Triggers, flows, and workflow rules run for every record and can update other records. Each extra update is another lock. Where your org policy allows, disable nonessential automation for the load window.
- Avoid concurrent jobs on the same object. Two jobs that update the same records collide. Run them one after the other.
- Watch roll-up summary fields and sharing rules. Changes to child records recalculate the parent. Changes to ownership or role hierarchy can trigger sharing recalculation, which takes locks on groups.
- Load only the columns you need. Smaller files upload faster and process fewer fields.
Keep jobs sized sensibly
Salesforce batches the data for you, so you do not choose a batch size. You still control how many jobs you run and how large each one is.
- Prefer a few large jobs to many tiny ones. Each job has fixed overhead and counts toward daily limits.
- Stay within the per-job file size limit and the daily record limits described in the previous chapter.
- Poll at a sensible interval, for example every 10 to 30 seconds. Polling every second wastes API calls.
Retry the right way
After a job ends, check numberRecordsFailed. If failures are mostly lock errors, fetch the failed results, drop the sf__Id and sf__Error columns, and submit the remaining rows as a new job. Retry runs alone are usually faster, since the original contention is gone.
Summary of practices
| Problem | Practice |
|---|---|
| Many children of the same parent in different batches | Sort the CSV by parent ID |
| One parent with a huge number of children | Spread children across parents |
| Triggers and flows add updates | Disable nonessential automation during the load |
| Overlapping jobs | Run jobs one after another |
| Lock errors in results | Resubmit failed rows as a separate job |
Exercise
A load of Contacts produces many UNABLETOLOCK_ROW errors. The CSV is in random order and Contacts from the same Account are spread across the file. What two changes would you make before retrying?
Show answer
Sort the CSV by AccountId so Contacts of the same Account fall in the same batch. Then resubmit only the failed rows as a new job, after removing the sf_Id and sf_Error columns. You can also check for triggers or flows that update the parent Account, and for other jobs running on the same data.