# What are the exact depth limits and execution constraints for chaining Queueable Apex jobs?

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

Read it on the site: https://www.forcechallenges.com/interview-questions/what-are-the-exact-depth-limits-and-execution-constraints-for-chaining-queueable-apex-jobs

## Answer

**Basics:** Chaining means a queueable's `execute` method enqueues another queueable, so work proceeds in sequential steps (fetch, transform, save), each in a fresh transaction with new governor limits. It is the standard way to process a large workload in pieces or to order dependent async operations.

**How it works:** In the parent, call `System.enqueueJob(new NextJob(...))` once. The child starts after the parent completes. Each job is an independent transaction with async limits (12 MB heap, 60,000 ms CPU). If the parent fails, the child is not enqueued unless it was already enqueued before the failure, so ordering and Finalizers matter.

**Advanced / deep dive:** Limits to remember: 50 `System.enqueueJob` calls per synchronous transaction; exactly 1 from within a queueable's `execute` (and a limited number from batch `execute`); no stack depth limit in production but 5 in Developer Edition and trial orgs. `System.enqueueJob(job, delayMinutes)` accepts a 0 to 10 minute delay, useful for rate-limited APIs. `System.AsyncOptions` provides `MaximumQueueableStackDepth`, `MinimumQueueableDelayInMinutes` and a duplicate signature for de-duplication; check current release notes for exact behaviour. Infinite chains are a real risk, so add a termination condition and a depth or retry counter in state. Because you can only fan out one at a time from within a queueable, parallelism requires launching multiple jobs from a synchronous context or using Batch Apex. Tests cannot chain queueables; calling `System.enqueueJob` inside a running test queueable raises a stack-depth error, so guard with `Test.isRunningTest()`. Finalizers can enqueue one follow-up job and are the reliable place to retry or alert after failure.

**Code / config example:**
```apex
public class ChunkedJob implements Queueable {
    private List<Id> remaining;
    private Integer chunkSize = 200;
    public ChunkedJob(List<Id> remaining) { this.remaining = remaining; }

    public void execute(QueueableContext ctx) {
        Integer n = Math.min(chunkSize, remaining.size());
        List<Id> current = new List<Id>();
        for (Integer i = 0; i < n; i++) current.add(remaining[i]);
        // ... process 'current' ...
        List<Id> rest = new List<Id>();
        for (Integer i = n; i < remaining.size(); i++) rest.add(remaining[i]);
        if (!rest.isEmpty() && !Test.isRunningTest()) {
            System.enqueueJob(new ChunkedJob(rest), 1);   // one child, 1 minute delay
        }
    }
}
```

**Hidden gotchas & must-remember points:**
- Only ONE child per running queueable.
- Unlimited depth in production, 5 in Developer Edition and trial orgs.
- 50 enqueues per synchronous transaction.
- Delay is 0 to 10 minutes via the two-argument `enqueueJob`.
- Chaining in tests throws "Maximum stack depth has been reached"; guard it.
- Always include a stop condition to prevent runaway chains.

**Best practice:** Use chaining for sequential dependent steps with a clear exit condition, and use Batch Apex for very large data sets.

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