Hello! Welcome back.
In our last lesson, we took our first steps into the world of asynchronous processing with Laravel Queues. You learned how to create a job, dispatch it to a queue, and run a worker to process it, all using the simple database driver. This allows you to offload slow tasks and create a much faster experience for your users.
Today, we'll build directly on that foundation. Our goal is to configure and use different queue drivers, specifically database and Redis. You'll learn not just how to switch between them, but why you would choose one over the other, which is a key consideration for performance optimization.
A Quick Recap and Why Drivers Matter
In the previous lesson, you set QUEUE_CONNECTION=database in your .env file. This told Laravel to use a table in your main application database (which will be MSSQL for you) as the queue. This is simple and effective.
However, Laravel supports several queue "drivers" or "backends." The most common are:
database: Uses a database table. Simple to set up, but can add load to your primary database.redis: Uses an in-memory data store. It's extremely fast and built for this kind of work, making it a very popular choice for production applications.sync: A "fake" driver that runs jobs immediately (synchronously). Useful for local testing or debugging.- Others like Amazon SQS, Beanstalkd, etc.
Choosing the right driver is a crucial part of building a scalable application. Let's start by taking a closer look at the configuration for the database driver we've already been using.
1. The database Driver Configuration
You've already done the basic setup, but let's understand the configuration options available.
30 Days to Learn Laravel, Ep 25 - Queues Are Easier Than You Think
The Laracasts video you saw previously has a great segment explaining the queue configuration file and how the database driver is defined.
Watch from 00:56 to 03:15. Pay attention to how the config/queue.php file is structured and the specific settings for the database connection, like table, queue, and retry_after.
As the video shows, all queue configurations live in config/queue.php. Let's break down the database connection block:
// in config/queue.php
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
'after_commit' => false,
],
'driver' => 'database': Tells Laravel to use the database queue driver logic.'table' => 'jobs': Specifies the table where jobs are stored. This matches the table you created withphp artisan queue:table.'queue' => 'default': When you dispatch a job without specifying a queue, it goes to thedefaultqueue. You can have multiple named queues (e.g.,high,low) for prioritization, which we'll touch on later.'retry_after' => 90: This is important for reliability. If a worker picks up a job but crashes or times out without finishing, this setting tells Laravel how many seconds to wait before making the job available again for another worker. The default is 90 seconds.
2. Switching to the Redis Driver
While the database driver is great, using your main application database for a high volume of queue jobs can create performance bottlenecks. This is where a dedicated service like Redis shines.
Redis is an in-memory key-value store, which means it reads and writes data from RAM. This makes it incredibly fast. When used for queues, it acts as a dedicated, high-speed message broker, taking the load off your primary database.
Setting up Redis
To use Redis with Laravel, you need two things:
- A running Redis server. (For local development, you can easily run one using Docker or by installing it directly on your machine).
- A PHP client library that allows Laravel to communicate with the Redis server.
Let's configure Laravel to use Redis.
1. Laravel Horizon - Monitor Redis Queue Jobs
This video from Bitfumes walks through the exact steps needed to switch your queue connection to Redis and install the necessary client.
Watch from 02:53 to 04:54. The video demonstrates the three key steps: changing QUEUE_CONNECTION in your .env file, installing the predis/predis package, and setting REDIS_CLIENT in .env to solve a common connection error.
To summarize the steps from the video:
Step 1: Install the Predis Client
Laravel supports two clients, PhpRedis (a PHP extension) and Predis (a package installed via Composer). Predis is often simpler to get started with.
composer require predis/predis
Step 2: Update Your .env file
You need to tell Laravel to use predis as the client and redis as the queue connection.
# Change the default queue connection
QUEUE_CONNECTION=redis
# Tell Laravel's Redis service which client to use
REDIS_CLIENT=predis
# Connection details for your Redis server
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
With these changes, any job you dispatch will now be sent to Redis instead of your database jobs table!
Redis Driver Configuration
Now, let's examine the Redis configuration in config/queue.php.
A Deep Dive Into Laravel Queues
The Honeybadger blog post 'A Deep Dive Into Laravel Queues' has an excellent section that clarifies some of the more confusing parts of the Redis queue configuration.
Read the section titled 'More Common Configuration Options'. Focus on the explanation of the block_for and connection options for the redis connection.
Here's the redis configuration block from config/queue.php:
'redis' => [
'driver' => 'redis',
'connection' => 'default', // This can be confusing!
'queue' => env('REDIS_QUEUE', 'default'),
'retry_after' => 90,
'block_for' => null,
],
Let's clarify two key options from the article:
-
'connection' => 'default': This does not refer to another queue connection. It refers to a Redis database connection defined in yourconfig/database.phpfile. Redis can have multiple databases (numbered 0-15), and this tells the queue which one to use. Thedefaultconnection inconfig/database.phptypically points to database0. -
'block_for' => null: This is a performance optimization specific to Redis. When a worker asks Redis for a job and the queue is empty,block_fortells the worker how many seconds to wait for a new job before checking again. This is more efficient than constantly polling Redis in a tight loop, which would waste CPU cycles. Setting it to5, for example, means the worker will wait up to 5 seconds for a job to appear.
3. Using Multiple Drivers at Once
You don't have to choose just one driver for your entire application. Your .env file sets the default driver, but you can dispatch specific jobs to different connections on the fly.
This is useful for sending high-priority, quick jobs to Redis for speed, while sending long-running, non-urgent jobs (like generating a large report) to the database driver.
A Deep Dive Into Laravel Queues
The same Honeybadger article shows how to dispatch a job to a connection other than your default.
Read the subsection 'Working with Multiple Connections'. It shows the onConnection() method, which is all you need.
As the article explains, you can use the onConnection() method when dispatching a job.
Example: Imagine your default queue is redis, but you want to send a GenerateSalesReport job to the database queue.
use App\Jobs\GenerateSalesReport;
use App\Jobs\SendPushNotification;
// This job will go to the default 'redis' queue
SendPushNotification::dispatch($user, $message);
// This job will be explicitly sent to the 'database' queue
GenerateSalesReport::dispatch($reportParams)->onConnection('database');
This gives you fine-grained control over your background processing strategy, allowing you to use the best tool for each job.
Conclusion
You've now moved beyond the basics and can make informed decisions about your application's queueing strategy. Understanding and using different drivers is fundamental to building high-performance, scalable Laravel applications.
Key Takeaways:
- Drivers provide backends for queues. The most common are
database(simple) andRedis(performant). - Drivers are configured in
config/queue.php, and the default is set withQUEUE_CONNECTIONin.env. - To use the
Redisdriver, you must install a client likepredis/predisand configure your.envfile. - The
Redisdriver offers performance benefits likeblock_forto reduce CPU usage. - You can dispatch a job to a non-default driver using the
onConnection('driver-name')method.
Up Next:
Now that you know how to configure different places for your jobs to live, the next step is to master the process that brings them to life: the worker. In our next lesson, we will dive deep into the php artisan queue:work command, learning how to run queue workers to process jobs, manage multiple queues, and set them up to run continuously in a production environment.