Concept 4 of 10Asynchronous Apex

Choosing asynchronous Apex: future, queueable, batch, scheduled, cursors and events

Why work goes async, what each async option is good at, and a decision table you can defend in an interview.

Your confidence:Not rated

Simple explanation

Work goes async when it would be too slow or too heavy for the user's transaction: callouts after DML, large data volumes, work that can wait, or work that must be isolated from the user's save.

OptionBest forKey traits
Future method (@future)Legacy fire-and-forget: a callout or mixed-DML fixPrimitive parameters only, no job ID, no chaining. Salesforce recommends Queueable instead.
QueueableMost new async work: callouts, chaining steps, passing objectsJob ID, complex parameters, chaining, Finalizers, delay and duplicate control via AsyncOptions
Batch ApexProcessing large record sets in chunks (up to 50 million rows from a QueryLocator)start/execute/finish, scope up to 2,000, Database.Stateful, only 5 active jobs at once (more wait in the flex queue)
Queueable + Apex CursorLarge data processing without using batch slotsA cursor over up to 50 million rows, fetched in chunks by a self-chaining queueable
Scheduled ApexRun at a time or on a recurring scheduleCron expression; usually starts a batch or queueable
Platform Events (EventBus.publish)Decoupling: publish now, process in a separate subscriber transactionPublish-after-commit or immediately; subscribers are triggers or flows with their own limits and retries

Rules of thumb: default to Queueable; use Batch (or Cursor + Queueable) for large volumes; use Scheduled only to start other work; use Platform Events when the producer shouldn't know or wait for the consumer.

Real-world analogy

Think of a post office. A future method is a postcard: cheap, no tracking. A queueable is a tracked parcel: you get a tracking number, it can carry anything, and the next parcel can be sent when it arrives. Batch Apex is a freight train for bulk goods, with only five tracks in the station. Scheduled Apex is the timetable. A platform event is a public announcement: whoever is subscribed acts on it.

Worked example

An opportunity is closed-won, and an order must be created in ERP. The trigger can't call out after DML, and the rep shouldn't wait, so it enqueues a Queueable:

public with sharing class OpportunityAfterUpdateHandler {
    public static void handle(List<Opportunity> newList, Map<Id, Opportunity> oldMap) {
        Set<Id> wonIds = new Set<Id>();
        for (Opportunity opp : newList) {
            Opportunity before = oldMap.get(opp.Id);
            if (opp.IsWon && !before.IsWon) {
                wonIds.add(opp.Id);
            }
        }
        if (!wonIds.isEmpty()) {
            // One job for the whole chunk, never one per record.
            System.enqueueJob(new ErpOrderSubmissionJob(wonIds));
        }
    }
}

Diagram

Rendering diagram…

Key points

  • Go async for callouts after DML, heavy volume, deferrable work or isolation.
  • Queueable is the default for new async code: job ID, objects as input, chaining, Finalizers, AsyncOptions.
  • Batch Apex for chunked processing of large sets; only 5 active batch jobs, others wait in the flex queue.
  • Apex Cursors + Queueable are a documented alternative to batch for large volumes.
  • Scheduled Apex starts work on a timetable; keep its own logic minimal.
  • Platform Events decouple producer and consumer, with separate transactions and retries.

Limits and gotchas

  • Async jobs enqueued in a transaction that rolls back are never run.
  • From a synchronous transaction you can enqueue up to 50 queueable jobs; from a queueable, only one child job.
  • Future methods can't be called from batch or future contexts, and can't take sObjects as parameters.
  • Async execution has an org-wide daily limit (historically the greater of 250,000 or user licences Γ— 200) βš‘ verify.
  • Async work runs in system context as the user who enqueued it. Sharing and user mode still matter in the async class.

🎀 How to say this in an interview

The classic question is "future vs queueable vs batch". Answer with the decision table, then add one modern point (Finalizers, AsyncOptions or Apex Cursors) to show you're current, and one operational point (monitoring AsyncApexJob, retries).

Common follow-ups

Why can't you call out directly from a trigger?

A trigger runs inside the save transaction with uncommitted DML, and callouts aren't allowed with pending DML. It would also make the user wait on an external system. Enqueue a Queueable (with Database.AllowsCallouts) or publish a platform event instead.

How do you monitor async jobs in production?

Query AsyncApexJob (status, errors, items processed), use Finalizers or the batch finish method to log outcomes, raise platform events on failure, and alert on failed or long-running jobs. Apex Jobs and Flex Queue in Setup help for manual checks.