Laravel Horizon Job Batching for Reliable Workflows | Mohamed Said       [Skip to content](#main)  [ ![](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png) Mohamed SaidLaravel Backend Engineer ](https://www.msaied.com/public) - [Home](https://www.msaied.com/public)
- [Projects](https://www.msaied.com/public/projects)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [About](https://www.msaied.com/public#about)

           [  Contact](https://www.msaied.com/public#contact) Menu 

Menu
----

Close 

 - [HomeStart here](https://www.msaied.com/public)
- [ProjectsCase studies](https://www.msaied.com/public/projects)
- [ArticlesEngineering notes](https://www.msaied.com/public/articles)
- [CertificatesCredentials](https://www.msaied.com/public/certificates)
- [AboutHow I work](https://www.msaied.com/public#about)
- [ContactGet in touch](https://www.msaied.com/public#contact)

  [Start a conversation](https://www.msaied.com/public#contact) [WhatsApp](https://wa.me/201094619204) [Email](mailto:hello@msaied.com) 

 1. [Home](https://www.msaied.com/public)
2. /
3. [Articles](https://www.msaied.com/public/articles)
4. /
5. Job Batching with Laravel Horizon: Reliable Async Workflows at Scale

 Job Batching with Laravel Horizon: Reliable Async Workflows at Scale
=====================================================================

 Learn how to combine Laravel's job batching API with Horizon's queue supervision to build fault-tolerant async workflows — covering batch callbacks, partial failures, and observability patterns.

 ![](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp) [Mohamed Said](https://www.msaied.com/public#person) Published 15 Aug 2026 · Updated 15 Aug 2026 · 3 min read

ShareCopy linkCopied

 ![Job Batching with Laravel Horizon: Reliable Async Workflows at Scale](https://cdn.msaied.com/553/b794b736bfd84f3cbcc6218319916544.png) 

  On this page +1. [Why Job Batching Deserves More Attention](#why-job-batching-deserves-more-attention)
2. [Anatomy of a Batch](#anatomy-of-a-batch)
3. [Making Jobs Batchable](#making-jobs-batchable)
4. [Horizon Configuration for Batch Workloads](#horizon-configuration-for-batch-workloads)
5. [Pruning and Observability](#pruning-and-observability)
6. [Nested Batches and Dynamic Fan-Out](#nested-batches-and-dynamic-fan-out)
7. [Takeaways](#takeaways)

 Why Job Batching Deserves More Attention
----------------------------------------

Laravel's `Bus::batch()` API has been available since Laravel 8, yet most teams still reach for simple `dispatch()` calls or manual counters to coordinate parallel work. Paired with Horizon's real-time supervision, batching gives you a first-class primitive for fan-out/fan-in patterns — think bulk imports, report generation, or multi-tenant data migrations — without building your own orchestration layer.

---

Anatomy of a Batch
------------------

A batch is a collection of jobs that share a lifecycle. Laravel tracks completion, failure counts, and pending jobs in the `job_batches` table.

```php
use Illuminate\Bus\Batch;
use Illuminate\Support\Facades\Bus;

$batch = Bus::batch([
    new ProcessChunk($chunk1),
    new ProcessChunk($chunk2),
    new ProcessChunk($chunk3),
])
->name('nightly-import')
->allowFailures()          // keep running even if some jobs fail
->onProgress(function (Batch $batch) {
    logger()->info('Batch progress', [
        'id'        => $batch->id,
        'pending'   => $batch->pendingJobs,
        'failed'    => $batch->failedJobs,
        'progress'  => $batch->progress(),
    ]);
})
->then(function (Batch $batch) {
    // All jobs succeeded
    ImportCompleted::dispatch($batch->id);
})
->catch(function (Batch $batch, \Throwable $e) {
    // At least one job failed (called once per failure when allowFailures is on)
    ImportFailed::dispatch($batch->id, $e->getMessage());
})
->finally(function (Batch $batch) {
    // Always runs — success or failure
    ImportFinished::dispatch($batch->id);
})
->dispatch();

```

> **Key distinction:** `->catch()` fires for *each* failed job when `allowFailures()` is set. Without it, the first failure cancels the batch and `->catch()` fires once.

---

Making Jobs Batchable
---------------------

Add the `Batchable` trait and guard against cancelled batches:

```php
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;

class ProcessChunk implements ShouldQueue
{
    use Batchable;

    public int $tries = 3;
    public int $backoff = 10;

    public function __construct(private readonly array $rows) {}

    public function handle(): void
    {
        if ($this->batch()?->cancelled()) {
            return; // bail early — another job may have triggered cancellation
        }

        foreach ($this->rows as $row) {
            // process row...
        }
    }
}

```

---

Horizon Configuration for Batch Workloads
-----------------------------------------

Batches benefit from dedicated queues so they don't starve interactive jobs.

```php
// config/horizon.php
'environments' => [
    'production' => [
        'supervisor-default' => [
            'connection' => 'redis',
            'queue'      => ['high', 'default'],
            'processes'  => 5,
        ],
        'supervisor-batch' => [
            'connection' => 'redis',
            'queue'      => ['batch'],
            'processes'  => 20,   // scale independently
            'timeout'    => 300,
        ],
    ],
],

```

Dispatch batch jobs onto the dedicated queue:

```php
Bus::batch($jobs)
    ->onQueue('batch')
    ->dispatch();

```

---

Pruning and Observability
-------------------------

Batch records accumulate. Schedule pruning and expose batch status via an API or Filament panel:

```php
// routes/api.php
Route::get('/imports/{batchId}', function (string $batchId) {
    $batch = Bus::findBatch($batchId);
    abort_unless($batch, 404);

    return response()->json([
        'progress'    => $batch->progress(),
        'pending'     => $batch->pendingJobs,
        'failed'      => $batch->failedJobs,
        'finished_at' => $batch->finishedAt,
    ]);
});

```

In `app/Console/Kernel.php` (or a scheduled command in Laravel 11+):

```php
$schedule->command('queue:prune-batches --hours=48')->daily();

```

---

Nested Batches and Dynamic Fan-Out
----------------------------------

You can add jobs to a running batch from within a job — useful when the total work isn't known upfront:

```php
public function handle(): void
{
    $subJobs = $this->discoverMoreWork();

    if ($subJobs) {
        $this->batch()->add($subJobs);
    }
}

```

Laravel increments `pendingJobs` atomically, so progress tracking stays accurate.

---

Takeaways
---------

- Use `allowFailures()` for resilient fan-out; omit it when partial success is unacceptable.
- Always check `$this->batch()?->cancelled()` at the top of `handle()` to avoid wasted work.
- Isolate batch queues in Horizon so throughput scales independently of interactive queues.
- Prune `job_batches` on a schedule — unbounded growth will hurt query performance.
- Expose batch progress via a lightweight endpoint or admin panel for operational visibility.
- Dynamic `batch()->add()` enables adaptive fan-out when the total job count is unknown at dispatch time.

- [laravel](https://www.msaied.com/public/articles?search=laravel)
- [queues](https://www.msaied.com/public/articles?search=queues)
- [horizon](https://www.msaied.com/public/articles?search=horizon)
- [async](https://www.msaied.com/public/articles?search=async)

 Frequently asked questions 
---------------------------

  What is the difference between `-&gt;catch()` and `-&gt;finally()` in a Laravel batch?`-&gt;catch()` is invoked when one or more jobs fail — once per failure if `allowFailures()` is active, or once on the first failure otherwise. `-&gt;finally()` always runs after the batch finishes, regardless of success or failure, making it the right place for cleanup or notification logic.

   Can I add jobs to a batch after it has already been dispatched?Yes. From within a batchable job you can call `$this-&gt;batch()-&gt;add($moreJobs)`. Laravel increments the pending job counter atomically, so progress reporting and completion callbacks remain accurate.

   How do I prevent the `job\_batches` table from growing indefinitely?Schedule `queue:prune-batches --hours=48` (or your preferred retention window) using Laravel's task scheduler. This removes finished and cancelled batch records older than the specified threshold.

   ![Mohamed Said](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp)About the author
----------------

[Mohamed Said](https://www.msaied.com/public#person)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

[About](https://www.msaied.com/public#about) [GitHub ↗](https://github.com/EG-Mohamed) [LinkedIn ↗](https://www.linkedin.com/in/msaiedm/) [WhatsApp ↗](https://wa.me/201094619204) [Email Address ↗](mailto:hello@msaied.com) [My CV ↗](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)  

   [Previous articleContextual Eloquent Casts: Custom Cast Classes, Value Objects, and Inbound-Only Transforms](https://www.msaied.com/public/articles/contextual-eloquent-casts-custom-cast-classes-value-objects-and-inbound-only-transforms) [Next articleOctane Worker Lifecycle, State Leakage, and Memory Management in Production](https://www.msaied.com/public/articles/octane-worker-lifecycle-state-leakage-and-memory-management-in-production-2)  

   On this page
-------------

1. [Why Job Batching Deserves More Attention](#why-job-batching-deserves-more-attention)
2. [Anatomy of a Batch](#anatomy-of-a-batch)
3. [Making Jobs Batchable](#making-jobs-batchable)
4. [Horizon Configuration for Batch Workloads](#horizon-configuration-for-batch-workloads)
5. [Pruning and Observability](#pruning-and-observability)
6. [Nested Batches and Dynamic Fan-Out](#nested-batches-and-dynamic-fan-out)
7. [Takeaways](#takeaways)

 ###  Have a technical challenge?

 Tell me what you’re building. I reply within two working days.

[Start a conversation](https://www.msaied.com/public#contact) 

   Related articles
-----------------

 [ ![](https://cdn.msaied.com/740/cce86edc21eddcbdd2f2454fadaf9c70.png)  · 3 min read### The Pipeline Pattern in Laravel: Custom Pipelines Beyond Middleware

5 Oct 2026 ](https://www.msaied.com/public/articles/the-pipeline-pattern-in-laravel-custom-pipelines-beyond-middleware-1) [ ![](https://cdn.msaied.com/739/2d6897fdcdcf090613f96f72a64b8a78.png)  · 4 min read### MySQL Full-Text Search in Laravel: Indexes, Relevance Scoring, and Boolean Mode

4 Oct 2026 ](https://www.msaied.com/public/articles/mysql-full-text-search-in-laravel-indexes-relevance-scoring-and-boolean-mode) [ ![](https://cdn.msaied.com/738/073696a3fefe18bec825beec5ac658f5.png)  · 4 min read### Laravel Queue Rate-Limited Middleware: Throttling Jobs Without Losing Work

4 Oct 2026 ](https://www.msaied.com/public/articles/laravel-queue-rate-limited-middleware-throttling-jobs-without-losing-work) 

  Have a technical challenge?
----------------------------

Tell me what you’re building. I reply within two working days.

 [Discuss your project ↗](https://www.msaied.com/public#contact) 

  © 2026 Mohamed Said · Built with Laravel, meant to last.Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

 - [Home](https://www.msaied.com/public)
- [Articles](https://www.msaied.com/public/articles)
- [Certificates](https://www.msaied.com/public/certificates)
- [GitHub](https://github.com/EG-Mohamed)
- [LinkedIn](https://www.linkedin.com/in/msaiedm/)
- [WhatsApp](https://wa.me/201094619204)
- [Email Address](mailto:hello@msaied.com)
- [My CV](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
- [Sitemap](https://www.msaied.com/public/sitemap.xml)
