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: