support@sajjanstudio.net
Sajjan Studio Sajjan Studio
Menu Navigation
Get A Quote Client Portal Login
Laravel 112 Views

Scaling Laravel Applications with Redis & Horizon

S

Shahzad Rasool

Published on January 05, 2025

Scaling Laravel Applications with Redis & Horizon

Learn how to configure Redis queues and Laravel Horizon to process millions of background jobs with real-time monitoring dashboard configurations.

Introduction to Asynchronous Job Processing

In modern web applications, keeping the user interface snappy is critical. Heavy operations like sending email newsletters, generating PDF invoices, resizing media assets, or calling third-party APIs should never block the main request-response cycle. Blocking the request thread results in high latency, degraded user experience, and potential timeout errors. This is where Laravel queues powered by Redis come to the rescue, allowing developers to offload time-consuming tasks to separate background workers.

"Scaling is not about making one server faster; it is about dividing the load among multiple workers concurrently. True application speed is achieved when the client thread is released immediately."

By utilizing an asynchronous architecture, you ensure that web servers focus solely on handling HTTP requests, leaving background workers to process heavy business logic at their own pace. This design pattern improves both frontend responsiveness and server stability under peak traffic loads. When a user requests a heavy report, the server returns a successful response in milliseconds, and the report is compiled in the background.

Why Choose Redis and Horizon?

Redis is an in-memory database that serves as an exceptionally fast, low-overhead message broker for Laravel queues. When combined with Laravel Horizon, you get a beautiful dashboard and code-driven configuration for your Redis queues. Horizon allows you to configure supervisor settings, queue routing rules, auto-scaling thresholds, and maximum execution limits directly inside your codebase. Here is what Horizon provides:

  1. Real-time throughput metrics: Monitor how many jobs are processed per minute, tracking average latency and peak execution speeds.
  2. Auto-scaling workers: Dynamically increase the number of queue workers when job counts spike, and scale them down when the queue is cleared.
  3. Failed job tracking: Review deep stack traces, search by specific payload arguments, and retry failed jobs with a single click in the UI dashboard.
  4. Detailed Job Metrics: Analyze which jobs take the longest to run and identify database queries causing performance bottlenecks.

Horizon Configuration Code Block

Below is a snippet of a typical config/horizon.php supervisor layout, tuned and optimized for high-volume enterprise systems:

'production' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['default', 'emails', 'notifications', 'payments'],
'balance' => 'auto', // Auto-scaling algorithm
'maxProcesses' => 15, // Cap processes limit
'minProcesses' => 3,
'tries' => 3,
'timeout' => 60,
'backoff' => [10, 30, 60], // Retry backoff timers
],
],

Best Practices for Writing Queue Jobs

When engineering high-volume queue tasks, keeping these architectural rules in mind ensures stability:

  1. Keep payloads small: Pass database record IDs rather than full model instances into your job constructor. Eloquent models take up valuable memory space and can be outdated by the time the worker executes the job. Instead, pass the model ID and reload the fresh record using the find() method inside the job handler.
  2. Ensure idempotency: Workers might retry jobs if they time out or lose connection. Ensure that running the same job multiple times does not result in duplicate actions, like charging a customer twice or sending redundant notifications. Use database transactions and check status flags before processing.
  3. Use rate limiters: Protect external APIs by rate-limiting your workers using Redis throttling. If you are calling a third-party service with strict API usage limits, wrap your job execution logic in a Redis rate limiter to prevent IP bans.
  4. Leverage database transactions: Make sure your queue dispatch calls are executed only after the parent database transactions are committed. You can achieve this using the afterCommit() method to prevent workers from picking up records that do not exist yet.

Detailed Setup Walkthrough

To configure Horizon in your production environment, follow these structured steps:

First, install the package via Composer by running composer require laravel/horizon. Once installed, publish the assets and config file using the Artisan command php artisan horizon:install. This will create a configuration file at config/horizon.php where you can define your queues and environments. Next, ensure your environment variables inside the .env file point to your Redis host: set QUEUE_CONNECTION=redis and configure the Redis host and port values correctly. Finally, set up a process manager like Supervisor on your cloud Linux server to monitor the php artisan horizon command continuously, guaranteeing that the daemon is automatically restarted if it exits.

Common Pitfalls to Avoid

One major mistake is neglecting to configure memory limits on the Redis server. By default, if Redis runs out of memory, it will crash or discard keys. Always set maxmemory limits and configure eviction policies (like volatile-lru) inside your redis.conf settings. Another common issue is memory leaks in background workers. Since Laravel processes jobs inside a persistent PHP process, global variables or static instances can consume memory over time. To avoid this, configure Horizon to reload workers periodically using the maxProcesses parameter or schedule cron-based worker recycles.

By implementing Redis and Horizon, Sajjan Studio successfully scales enterprise corporate systems to handle thousands of operations per second without any lag on the client portal. This configuration ensures that critical emails go out instantly without hindering other concurrent requests on the dashboard, delivering a premium user experience.

Let's Create

Ready to build your custom software application?

Submit details of your project to our project managers for a finalized custom quotation document.

Home Services Quote Work Portal