Laravel Job Batching for Large Background Tasks

Job batches in Laravel let you run many related jobs as a single unit, with progress tracking and cancellation. Here's when to use them and how to set them up.

Richard GamoraRichard GamoraFullstack developer·4 min read
LaravelQueueJobs

If you have ever needed to run a thousand related jobs and report progress to the user, you have probably reinvented this from scratch. Laravel's job batching feature, added a few versions back, gives you that pattern out of the box.

What batches solve

A batch is a group of jobs dispatched together with a shared identity. You can track how many have completed, cancel the whole batch, run a callback when it finishes, and run a different callback if any job inside it fails. All four are common needs that used to require a custom progress table and a custom dispatcher.

Setting one up

php$batch = Bus::batch([
    new ImportRow($row1),
    new ImportRow($row2),
    // ...
])->then(function (Batch $batch) {
    // all jobs done
})->catch(function (Batch $batch, Throwable $e) {
    // first failure
})->finally(function (Batch $batch) {
    // ran whether or not failures occurred
})->dispatch();

return ['batch_id' => $batch->id];

You can then load the batch by ID with Bus::findBatch($id) and check progress: $batch->totalJobs, $batch->processedJobs(), $batch->progress(). It is a lot of useful behavior for a small amount of code.

When not to use batches

Batches add a few rows to the job_batches table per dispatch and a progress check on every job completion. For a firehose of unrelated jobs (signups, emails, webhook deliveries), this overhead is wasted — those should be plain queued jobs.

Batches earn their keep when the user wants to know the status. CSV imports, bulk emails to a known list, and large recalculation jobs all benefit. If no one is ever going to ask "is it done yet," plain jobs are simpler and cheaper.

About the author

Richard Gamora

Richard Gamora

Fullstack developer based in the Philippines, working mostly with Laravel and Vue.js, with eight years of production experience across web and mobile.

me@richardgamora.comUpwork ↗

More on Laravel