Asynchronous Apex is used to run a process in a Separate thread when the are resources available.There is no SLA for these jobs as the resources which are used to run these jobs are shared across the instance.As said above, resources are shared across the instance, All the orgs resided in an instance will get a fair share of the resources.
The benefits of running Asynchronous Jobs are as follows:
- User Efficiency: User Does not have to wait until the process executes as this executes in a Separate Thread.
- Limits : As the process executes in a new thread, A new sets of limits are applied to the process. These limits are generally higher than what a synchronous process has. For limits, Click here. https://developer.salesforce.com/docs/atlas.en-us.216.0.apexcode.meta/apexcode/apex_gov_limits.htm
How Asynchronous Process Works:
- Ensures fair share of the resources across the instance.
- As this works on Multi-tenant architecture, To ensure the jobs are not lost due to instance’s performance failures and server restarts, Platform uses a queue to manage asynchronous requests for multiple organisations in an instance.
- This request cycle consists of 3 stages, they are as follows:
- Enqueue: The request gets in the queue.
- Persistance: Request is persisted i.e.., they are stored in persistent storage for failure recovery
- Dequeue: The request is de-queued and processed.Transaction management is also used to ensure status and messages of the request are not lost if there occurs any failures. Every request is handled by a code (handler) Runs on application servers.
Types of Asynchronous Apex:
Asynchronous Apex comes in four different flavors. Depending upon the use case, user can identify which flavor suits the requirement.
- Future Method : These are the methods which are used in synchronous transactions by using @future annotation on the method.
- Batch Apex : This is used to run large jobs which contains millions of records.
- Queue able Apex : These are also like future methods but has an ability to chain jobs with a class.
- Scheduled Apex : These are scheduled to run at a specific time.
Monitor Asynchronous Jobs:
To Monitor the Asynchronous jobs, Salesforce has provided a UI.
From Setup>>Jobs in Quick find>>Apex jobs.
This section shows the status of the job including with the type of job such as batch, Queue-able etc.
If a job is failed, This section will show the status with failed and reason for the failure of the job.
More information on further posts when we learn on each type of job with it’s use case.