Async Apex in Salesforce — Types & When to Use Each

Introduction

Asynchronous Apex (Async Apex) in Salesforce lets you run long-running or resource-intensive processes outside the standard synchronous request — preventing governor limit issues and improving user experience. This post explains what Async Apex is, why it matters, the common asynchronous patterns in Salesforce, and short code examples for each.

Why use Async Apex?

Async Apex is ideal when you need to:

  • Process large volumes of records beyond synchronous limits
  • Perform callouts that take time to respond
  • Run scheduled or periodic jobs
  • Chain or queue jobs to avoid blocking the request

Key considerations

Async Apex has its own set of limits and behaviors (separate governor limits, execution context, and monitoring via the AsyncApexJob object). Use the right pattern for the requirement to balance performance and limits.

Common Async Apex Types (Overview)

Salesforce supports several asynchronous processing techniques. The primary ones are:

  • Future methods (@future)
  • Queueable Apex (Queueable interface)
  • Batch Apex (Database.Batchable)
  • Scheduled Apex (Schedulable interface)
  • Platform Events (for event-driven async processing)
  • Continuation (for long-running callouts in Visualforce/Lightning)

Detailed Patterns & Examples

1) Future methods

Use when you need simple, fire-and-forget asynchronous execution (and optionally perform callouts with @future(callout=true)).

Pros: Simple to implement. Good for light async tasks.

Cons: Limited monitoring/chaining, primitive parameter types only.


public class MyFutureClass {
@future
public static void doAsyncWork(Set recordIds) {
// long-running logic here
}
}

2) Queueable Apex

Queueable Apex is a more modern replacement for complex future method use-cases. It supports complex types, job chaining, and is easier to monitor.

Pros: Supports complex objects, chaining, better job information in the UI.

Cons: Still limited to smaller batch sizes vs. Batch Apex; limited concurrent jobs per transaction.


public class MyQueueable implements System.Queueable {
public void execute(System.QueueableContext ctx) {
// async logic
}
}

// enqueue
System.enqueueJob(new MyQueueable());

3) Batch Apex

Batch Apex is for processing large volumes of data (thousands — millions). Implement Database.Batchable<SObject> and optional Database.Stateful for state across batches.

Pros: Handles massive data volumes, chunked processing, better for long-running jobs.

Cons: More boilerplate; governance on number of batch jobs scheduled concurrently.


public class MyBatch implements Database.Batchable<SObject> {
public Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator([SELECT Id FROM Account WHERE ...]);
}
public void execute(Database.BatchableContext bc, List<SObject> scope) {
// process chunk
}
public void finish(Database.BatchableContext bc) {
// post-processing
}
}

// run
Database.executeBatch(new MyBatch(), 200);

4) Scheduled Apex

Use Scheduled Apex to run jobs at specified times or intervals. Implement Schedulable and schedule via the UI or System.schedule.


public class DailyJob implements Schedulable {
public void execute(SchedulableContext sc) {
// scheduled logic (often calls a batch or queueable)
}
}
// schedule
System.schedule('Daily Job', '0 0 2 * * ?', new DailyJob());

5) Platform Events

Platform Events are great for decoupled, event-driven async architectures. Publish events and have subscribers (Apex triggers, processes, or external systems) consume them asynchronously.

6) Continuation

Continuation is used for long-running callouts from a Visualforce Page or Lightning component without blocking the request thread. It’s specialized for response-driven async callouts.

When to choose which?

Use these guidelines:

  • Short, simple fire-and-forget: Future methods
  • Medium complexity with chaining or complex parameters: Queueable
  • Large-scale record processing: Batch Apex
  • Periodic jobs: Scheduled Apex (often invoking Batch/Queueable inside)
  • Event-driven integrations or decoupling: Platform Events
  • Long-running callouts from UI: Continuation

Monitoring & Best Practices

Monitor async jobs via the Salesforce UI (Apex Jobs, Scheduled Jobs) or query AsyncApexJob and CronTrigger/CronJobDetail for scheduled jobs. Consider these best practices:

  • Keep transactions small; prefer batch for large volumes.
  • Avoid mixing heavy synchronous processing with async kickoff inside loops.
  • Handle retries and idempotency for resilient processing.
  • Be mindful of async limits (e.g., max queued jobs, batch job limits).

Conclusion

Async Apex is essential for scalable Salesforce solutions. Choosing the right async pattern — future, queueable, batch, scheduled, platform events, or continuation — depends on data size, complexity, and orchestration needs. Understanding limits and monitoring options will help you design robust async processes.