# What advantages does Queueable Apex provide over traditional @future methods in modern Salesforce development?

**Topic:** Apex · **Difficulty:** Medium · **Tags:** Apex, Asynchronous Apex, Queueable Apex

Read it on the site: https://www.forcechallenges.com/interview-questions/what-advantages-does-queueable-apex-provide-over-traditional-future-methods-in-modern-salesforce-development

## Answer

**Basics:** Asynchronous Apex runs work later, in its own transaction with higher limits. `@future` was the first tool but is restrictive. Queueable Apex is a class implementing `Queueable` with an `execute(QueueableContext)` method; you create an instance, set its member variables, and call `System.enqueueJob`.

**How it works:** `Id jobId = System.enqueueJob(new MyJob(records));` serialises the object, queues it and returns the AsyncApexJob Id. When resources free up, the platform deserialises it and runs `execute` with async limits (12 MB heap, 60,000 ms CPU, 200 SOQL queries). Inside `execute` you may enqueue exactly one child job to chain steps.

**Advanced / deep dive:** Advantages over `@future`: (1) member variables can be SObjects, lists, maps, and your own serialisable classes; future accepts only primitives or collections of primitives. (2) You get a job Id for monitoring or aborting with `System.abortJob`. (3) Chaining runs sequentially, passing state forward. (4) `System.enqueueJob(job, delayMinutes)` supports a 0 to 10 minute delay. (5) `System.attachFinalizer` registers a `System.Finalizer` that runs after the job, success or failure, even on uncatchable limit exceptions, enabling cleanup and retry. (6) `System.AsyncOptions` (with `QueueableDuplicateSignature`, `MinimumQueueableDelayInMinutes`, `MaximumQueueableStackDepth`) supports de-duplication and depth control; verify availability and details in current release notes. (7) A future method cannot call another future, whereas Queueable can be enqueued from many contexts (up to 50 per synchronous transaction). Callouts need `Database.AllowsCallouts` only for batch; Queueable can make callouts directly. Trade-offs: serialised state counts against heap, chains add latency, and queueable throughput is shared with the org's async pool (250,000 or licenses x 200 async executions per 24 hours, whichever is greater).

**Code / config example:**
```apex
public class SyncAccountsJob implements Queueable, Database.AllowsCallouts {
    private List<Account> accounts;           // non-primitive state: fine in Queueable
    public SyncAccountsJob(List<Account> accts) { this.accounts = accts; }

    public void execute(QueueableContext ctx) {
        System.attachFinalizer(new SyncFinalizer(ctx.getJobId()));
        // ... callout / DML work with accounts ...
    }
}
public class SyncFinalizer implements Finalizer {
    private Id jobId;
    public SyncFinalizer(Id jobId) { this.jobId = jobId; }
    public void execute(FinalizerContext fc) {
        if (fc.getResult() != ParentJobResult.SUCCESS) {
            System.debug('Failed: ' + fc.getException());   // log or retry
        }
    }
}
// Caller
Id jobId = System.enqueueJob(new SyncAccountsJob(accts), 2); // run after 2 min
```

**Hidden gotchas & must-remember points:**
- Only ONE child job can be enqueued from a running queueable.
- Chain depth is unlimited in production but limited to 5 in Developer Edition and trial orgs.
- You cannot chain queueables inside a test; guard with `Test.isRunningTest()` and test each job via `System.enqueueJob` between `Test.startTest()` and `Test.stopTest()`.
- Serialised state is a snapshot; data may be stale by run time.
- Non-serialisable members (for example `Http` objects or some system types) cause errors.
- From a Batch `execute` you can enqueue only limited jobs; check the limit rather than looping.

**Best practice:** Prefer Queueable over `@future` for new code, pass Ids where possible, and add a finalizer for error handling.

---
Source: ForceChallenges · last updated 2026-11-02
