0
0
Laravelframework~10 mins

Job chaining and batching in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Job chaining and batching
Create Jobs
Chain Jobs?
YesSet chain order
Dispatch chained jobs
Batch Jobs?
YesCreate batch
Dispatch batch
Dispatch single job
Job(s) execute in order or parallel
Jobs complete with success/failure callbacks
This flow shows how Laravel creates jobs, optionally chains or batches them, dispatches, and then executes with callbacks.
Execution Sample
Laravel
use App\Jobs\Job1;
use App\Jobs\Job2;

Job1::withChain([new Job2()])->dispatch();
This code dispatches Job1 and chains Job2 to run after Job1 finishes.
Execution Table
StepActionJobs CreatedChain SetBatch CreatedDispatch ActionJob Execution OrderCallback Triggered
1Create Job1 instanceJob1NoNoNoNoneNo
2Create Job2 instance for chainJob1, Job2Yes (Job2 after Job1)NoNoNoneNo
3Dispatch Job1 with chainJob1, Job2YesNoJob1 dispatchedJob1 firstNo
4Job1 executesJob1, Job2YesNoJob1 runningJob1 runningNo
5Job1 completes successfullyJob1, Job2YesNoJob1 doneJob1 doneNo
6Job2 dispatched automatically after Job1Job1, Job2YesNoJob2 dispatchedJob2 nextNo
7Job2 executesJob1, Job2YesNoJob2 runningJob2 runningNo
8Job2 completes successfullyJob1, Job2YesNoJob2 doneJob2 doneSuccess callback triggered
9All chained jobs doneJob1, Job2YesNoNoAll doneChain complete callback triggered
10ExitJob1, Job2YesNoNoAll doneAll callbacks done
💡 All chained jobs executed in order; callbacks triggered after completion.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6After Step 9
Jobs CreatedNoneJob1, Job2Job1, Job2Job1, Job2Job1, Job2
Chain SetNoYes (Job2 after Job1)YesYesYes
Batch CreatedNoNoNoNoNo
Dispatch ActionNoNoJob1 dispatchedJob2 dispatchedNo
Job Execution OrderNoneNoneJob1 firstJob2 nextAll done
Callback TriggeredNoNoNoNoChain complete callback
Key Moments - 3 Insights
Why does Job2 only start after Job1 finishes?
Because the chain sets Job2 to run only after Job1 completes successfully, as shown in execution_table step 6 where Job2 dispatch happens after Job1 done.
What happens if a job in the chain fails?
The chain stops executing further jobs and failure callbacks run. This is implied by the chain behavior in the execution_table where jobs run sequentially and callbacks trigger on success or failure.
How is batching different from chaining?
Batching runs multiple jobs in parallel as a group, while chaining runs jobs one after another. Our example shows chaining with no batch created (Batch Created stays 'No').
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is the job execution order?
AJob1 is running
BJob2 is running
CBoth Job1 and Job2 running in parallel
DNo job running
💡 Hint
Check the 'Job Execution Order' column at step 4 in the execution_table.
At which step does Job2 get dispatched automatically?
AStep 3
BStep 6
CStep 5
DStep 7
💡 Hint
Look for 'Job2 dispatched' in the 'Dispatch Action' column in the execution_table.
If we added a batch instead of a chain, which variable would change in variable_tracker?
AChain Set
BJobs Created
CBatch Created
DCallback Triggered
💡 Hint
Check the 'Batch Created' row in variable_tracker to see when batches are created.
Concept Snapshot
Laravel Job Chaining & Batching:
- Chain jobs to run one after another using withChain()
- Batch jobs to run many in parallel using Bus::batch()
- Dispatch chains or batches to queue
- Callbacks run on success or failure
- Chains run sequentially; batches run concurrently
Full Transcript
In Laravel, you create jobs to do tasks. You can chain jobs so one runs after another finishes, or batch jobs to run many at once. When you chain jobs, Laravel waits for the first job to finish before starting the next. If a job fails, the chain stops. Batching runs jobs in parallel and lets you track progress. This example shows creating two jobs, chaining them, dispatching the first, then the second runs automatically after the first completes. Callbacks run after all jobs finish. This helps organize complex tasks in order or groups.