Recently we had a requirement to read each account record and create 4 related child records for each of them. First we wrote a console app that will loop through each of the account records and will create corresponding records. The tool was working fine however it’s execution was very slow. It was talking around 5 hours to process 10000 Account records and create corresponding 40000 child records.
To fine tune the performance, we implemented the Parallel.ForEach.
We got the performance improvement, however, we were getting the following error
The communication object, System.ServiceModel.Security.TransportSecurityProtocol, cannot be used for communication because it has been Aborted.
And because of this, we had few Account records which had one, two or three records instead of total 4 related records as required. (as the child records might be getting created by separate thread).
For e.g. we can see that for Account Number 98208 we have four records created one at 8:33, then another one at 8:36, 8:38 and 8:41. (gap of minimum 2 minutes). So if there was an exception we had account records created with less than 4 child records.
We solved this issue by using ExecuteTransactionRequest, and creating all the child records as part of the transaction so that we either have all the 4 records created or none.
All the child records were getting created at the same time
We also got huge performance improvement, the code that was initially taking 5 hours to process 10000 records, was now taking only 1 hour to process them.
Few points to remember:-
- ExecuteMultipleRequest can contain ExecuteTransactionRequest and not vice versa.
- Batch Size of 1000 applies to both. In Online the max parallel request could be 2.
- ExecuteTransactionRequest can have mix of Create and Update Request.
Hope it helps..