Docs cipi.yml Web panel API MCP Guides Compare GitHub Telegram channel
  1. Home
  2. Guides
  3. Laravel queues and Horizon in production
Laravel

Laravel queues and Horizon in production

Queues explained from zero, then everything for production on your own server: what a worker is, adding and scaling queues under Supervisor, timeouts and retries, switching to Horizon on Valkey, protecting the dashboard, deploys that never run old code, failed jobs, alerts and cipi.yml.

By Updated 3 min read

A queue lets your app answer the visitor immediately and do slow work (e-mails, PDFs, imports, webhooks) a moment later, in the background. A worker is a long-running process that takes jobs from the queue and runs them. This guide sets both up for production, then switches to Horizon when you want a dashboard.

Key ideas, in plain words#

TermMeaning
JobA class with a handle() method, dispatched with SendInvoice::dispatch($order)
QueueA named line of jobs: default, emails, exports
ConnectionWhere jobs are stored: database (Cipi's default) or redis (Valkey)
Workerphp artisan queue:work, running forever, processing jobs
SupervisorThe Linux service that keeps workers running and restarts them if they crash
HorizonLaravel's worker manager for Redis queues, with a dashboard
Step 1 of 5Request

The controller calls SendInvoice::dispatch($order) and answers the visitor straight away.

Before you start#

  • A Laravel app deployed with Cipi: it already has one worker on default.
  • Jobs implement ShouldQueue. Without it they run inline, in the request.
bash
sudo cipi worker list shop
Queue workers
$ cipi worker add shop --queue=emails --processes=2
$ cipi worker list shop

1. Add and scale queues#

Put slow or bursty work on its own queue, so a big export never delays password-reset e-mails:

bash
sudo cipi worker add shop --queue=emails --processes=3
sudo cipi worker add shop --queue=exports --processes=1 --timeout=7200
sudo cipi worker edit shop --queue=default --processes=2
sudo cipi worker list shop
FlagMeaningDefault
--queueQueue to consumedefault
--processesParallel processes1
--triesAttempts before a job goes to failed_jobs3
--timeoutSeconds a worker process lives before it's recycled (--max-time), and how long a stop waits for the running job3600

Plain workers consume the database connection. To move queues to Valkey, use Horizon (step 4).

php
SendInvoice::dispatch($order)->onQueue('emails');
ExportOrders::dispatch($user)->onQueue('exports');

2. Timeouts and retries, the part people get wrong#

  • $timeout on the jobthe job is killed after N seconds (Laravel's default: 60)
  • retry_afterthe queue hands the job to another worker after N seconds
  • --timeout of the workerhow long the process lives, and how long a stop waits for it
  • $tries / $backoffper job: how many attempts, how long between
RuleExample
Long jobs declare their own limitpublic $timeout = 7000; on ExportOrders
The worker waits at least that longcipi worker add shop --queue=exports --timeout=7200
retry_after > the longest job'retry_after' => 7260 for that connection
Jobs that call APIs get a backoffpublic $backoff = [10, 60, 300];
Limit attemptspublic $tries = 5; or retryUntil()
Make jobs safe to run twiceCheck state first: "already sent?"

retry_after is set per connection in config/queue.php. If it's shorter than a job's runtime, the job runs twice.

3. Failed jobs#

bash
sudo cipi app artisan shop queue:failed
sudo cipi app artisan shop queue:retry all
sudo cipi app artisan shop queue:retry 5f1c…           # one job
sudo cipi app artisan shop queue:prune-failed --hours=168

Add a failed() method to the job to notify yourself, or alert on the count from the scheduler.

4. Switch to Horizon#

Horizon needs the queues on Redis. Each app has its own Valkey ready.

bash
# on your laptop
composer require laravel/horizon
php artisan horizon:install
git commit -am "Add Horizon" && git push
# on the server
sudo cipi app env shop --set=QUEUE_CONNECTION=redis
sudo cipi app artisan shop config:cache
sudo cipi worker horizon enable shop
sudo cipi worker horizon status shop

Horizon replaces the app's queue:work workers: one or the other. Define supervisors in config/horizon.php:

php
'environments' => [
    'production' => [
        'fast' => ['connection' => 'redis', 'queue' => ['default', 'emails'],
                   'balance' => 'auto', 'minProcesses' => 1, 'maxProcesses' => 6, 'tries' => 3],
        'slow' => ['connection' => 'redis', 'queue' => ['exports'],
                   'maxProcesses' => 1, 'timeout' => 7200, 'tries' => 1],
    ],
],
The deploy findsIt does
Horizon, queues on Redis, no workerEnables Horizon
Horizon plus existing workersExplains how to switch; changes nothing
Horizon, queues not on RedisTells you which variable to set

5. Protect the dashboard#

/horizon is closed in production until you open it:

php
// app/Providers/HorizonServiceProvider.php
protected function gate(): void
{
    Gate::define('viewHorizon', fn ($user) => in_array($user->email, [
        'you@example.com',
    ]));
}

For extra safety, basic auth or Zero Trust in front of the app.

6. Deploys don't break jobs#

Step 1 of 3Stop workers

During a deploy, workers stop first and finish their current job.

Workers stop gracefully: the running job finishes first, up to the worker's --timeout. Jobs dispatched by the old code must still work with the new code, so don't rename a job class or change its constructor in the same deploy that removes the old one.

7. Get alerted#

SignalHow
Worker crashed repeatedlySupervisor restarts it; Cipi alerts cover server events
Long waitsHorizon's waits config sends a notification when a queue waits too long
Failed jobsfailed() on the job, or a scheduled count check
Nothing processing at allsudo cipi pending lists apps with Horizon installed but no worker

8. In cipi.yml#

yaml
workers:
  horizon: false
  queues:
    - { queue: default, processes: 2 }
    - { queue: emails, processes: 3, tries: 5, timeout: 300 }
    - { queue: exports, processes: 1, timeout: 7200 }

Or horizon: true to hand everything to Horizon. More on cipi.yml.

Troubleshooting#

ProblemFix
Jobs never runNo worker on that queue (cipi worker list shop), job not ShouldQueue, or QUEUE_CONNECTION=redis with plain workers: they read database, so enable Horizon or set it back
Job runs twiceretry_after shorter than the job's runtime
MaxAttemptsExceededException / job killed at 60 sSet $timeout on the job and raise retry_after
Code change ignored by jobsWorkers not restarted: sudo cipi worker restart shop
Horizon shows "Inactive"Not enabled: cipi worker horizon enable shop; or QUEUE_CONNECTION isn't redis
/horizon returns 403The viewHorizon gate doesn't include your user
Memory grows over timeA job leaks: --max-jobs / maxJobs in Horizon restarts processes regularly

Questions

Workers or Horizon?

Plain workers are enough for a few queues. Horizon adds a dashboard, metrics, auto-balancing between queues and tagged job search, and needs the queues on Redis (Valkey). One or the other per app, not both.

Do workers run old code after a deploy?

No. Cipi stops them before the release swap and restarts them on the new code, so no job runs old code against a migrated database.

My job takes 20 minutes and gets killed.

Laravel kills a job after 60 seconds unless the job says otherwise: set public $timeout = 1500; on it, give its worker a --timeout at least as long, and set retry_after in config/queue.php higher, or it may be picked up twice.

Where do failed jobs go?

The failed_jobs table. List them with cipi app artisan shop queue:failed, retry with queue:retry all. Horizon shows them in the dashboard.

How many processes should I run?

Start with 1–2 per queue and watch the wait time. Each process uses about the memory of one request (often 50–150 MB). Horizon can scale between a min and max for you.

Who can open /horizon in production?

Nobody until you allow them in the viewHorizon gate. Add your team's e-mails, or put the whole app behind basic auth or Zero Trust.

Do queued jobs survive a server restart?

Yes. Jobs live in the database or in Valkey (saved to disk), and Supervisor starts the workers again at boot.

Try it on your own server

Free and open source. One command on a fresh Ubuntu 24.04 or 26.04 VPS.

wget -O - https://cipi.sh/setup.sh | bash
Read the quickstart