Interview frequency: …
Short answer
Chaining runs jobs sequentially (if one fails, the rest stop), while Batching runs a group in parallel, tracks progress, and provides success and failure callbacks.
Explanation
Use Bus::chain([new TaskA, new TaskB]) when the next step must wait for the previous one to finish. Use Bus::batch([new ImportCsvPart1, new ImportCsvPart2]) to distribute large work across multiple workers in parallel. A Batch object reports percentage completion and supports then(callback) when every job succeeds, catch() when any job fails, and finally() when the batch ends.
Code example
Bus::batch([
new ProcessImage($img1),
new ProcessImage($img2),
])->then(function (Batch $batch) {
// All images loaded successfully
})->catch(function (Batch $batch, Throwable $e) {
// Partial failure
})->dispatch();Common mistakes
- Forgetting to add the Illuminate\Bus\Batchable Trait to classes used in Batches.