Apex CPU Time – How to Resolve Conflict Between Run Time and CPU Time in Apex

apexcpu-time

According to documentation.

RUN_TIME : The amount of time that the request took in milliseconds.
Requests with a value over five seconds are considered long-running requests for the purposes of the Concurrent Long-Running Apex Limit.

But we also have CPU_TIME which has 10 seconds and 60 seconds timeout depending on Sync and Async transaction.

Question

Say we have 10 concurrent requests running in Apex (SOQL may take more time, but as SOQL is not counted against CPU TIME we should be good), which is still under CPU_TIME limit, and a 11th request comes. Why does SF throws long running concurrent requests exception.

This means technically when the load is heavy the total execution time should be less than 5 seconds, even though CPU time is 10/60 seconds.

Best Answer

The five second concurrent transaction limit is primarily there to prevent us from consuming too many resources. You can view the 10/60 second limit as a "burst" limit, while the 5 second limit is what we're expected to achieve on average. If your code is constantly running into the concurrent limit issue, you should consider moving some of your processes to asynchronous threads or optimize your code.

Related Topic