How does delayed Queueable job execution work and what are its primary use cases?
Answer
Basics: Previously, queueable jobs started as soon as resources were free. With the delay overload you tell the platform not to start the job before a minimum number of minutes has passed.
How it works: Call System.enqueueJob(new MyJob(), 5); and the job is queued with a minimum wait of 5 minutes. It returns the job Id like a normal enqueue. For more control use AsyncOptions: set MinimumQueueableDelayInMinutes, MaximumQueueableStackDepth and a DuplicateSignature (built with QueueableDuplicateSignature.Builder) to prevent duplicate jobs. Admins may also have an org-level default delay setting in Apex Settings; verify in current release notes.
Advanced / deep dive: Use cases: retry with backoff after a failed callout, spacing calls to a rate-limited API, letting a record lock or an external process finish, or batching bursts of triggers into a single later job via duplicate signatures. Combine with Finalizers: attach System.attachFinalizer to detect failure and re-enqueue with a longer delay. The delay is a minimum, not a guarantee of exact start time. A delayed job still counts toward the 50-jobs-per-transaction limit. Compare: Scheduled Apex has a heavier setup and minute granularity at best, while Platform Events or Flow Scheduled paths are alternatives for declarative designs.
Code / config example:
public class RetryJob implements Queueable, Database.AllowsCallouts {
private Integer attempt;
public RetryJob(Integer attempt) { this.attempt = attempt; }
public void execute(QueueableContext ctx) {
// callout here; on failure retry with growing delay
if (attempt < 3) {
System.enqueueJob(new RetryJob(attempt + 1), 2 * attempt);
}
}
}