Setup Laravel Queue with Database Driver

Summary: Use database queue driver for local development.


Laravel offers a robust queuing system that lets you defer time-consuming tasks for later processing, improving your application’s response time and user experience. While there are several queue drivers such as Redis, Beanstalkd, SQS, and database, using the database driver is an excellent choice for local development because it's simple to set up and requires no additional services.

This article will guide you through setting up and using Laravel’s queue system with the database driver.


1. Why Use the Database Driver?

During development, you often don't want to worry about installing and configuring external services. The database queue driver uses your app’s existing database to store queued jobs. This makes local testing, debugging, and small-scale deployments straightforward.

Advantages:

  • No external dependencies (apart from your database)
  • Easy job inspection using your regular DB tools
  • Smooth transition to other drivers in production

2. Prerequisites

  • Laravel installed (8.x, 9.x, or 10.x)
  • A configured database connection
  • Access to your terminal/command-line

3. Queue Configuration

a) Set Queue Driver to Database

Open your .env file and set:

QUEUE_CONNECTION=database

You can confirm this by checking config/queue.php:

'default' => env('QUEUE_CONNECTION', 'sync'),

4. Migrate the Jobs Table

The database driver stores queued jobs in a table named jobs. Laravel provides a migration you can use.

Run the queue tables migration:

php artisan queue:table
php artisan migrate

This will create:

  • jobs table (for pending jobs)
  • Optionally, failed_jobs table (for failed jobs; see next section)

5. Configuring Failed Job Logging (Optional but Recommended)

To track jobs that fail:

php artisan queue:failed-table
php artisan migrate

Check your config/queue.php and .env:

QUEUE_FAILED_DRIVER=database-uuids

This records failed jobs in the failed_jobs table for inspection.


6. Creating and Dispatching Jobs

a) Create a New Job

Generate a job class:

php artisan make:job SendWelcomeEmail

Update the handle() method in app/Jobs/SendWelcomeEmail.php:

public function handle()
{
    // Your email sending logic, e.g.:
    \Mail::to($this->user)->send(new WelcomeEmail($this->user));
}

b) Dispatch the Job

In your controller or service:

use App\Jobs\SendWelcomeEmail;

// ...

SendWelcomeEmail::dispatch($user);

7. Running the Queue Worker

With database driver, you need a background worker to process queued jobs:

php artisan queue:work

For development, you can simply run this in a second terminal window.

Tip: Use php artisan queue:listen if you want it to reload your code with every job (useful in local development).


8. Inspecting and Managing Jobs

You can peek at your queued jobs using a database GUI or artisan:

php artisan queue:failed      # List failed jobs
php artisan queue:retry all   # Retry all failed jobs
php artisan queue:flush       # Delete all failed jobs

9. Queue Worker in Production

For production, it is better to use a process supervisor (like Supervisor on Linux) to keep queue:work running. But for local development, running the command manually suffices.


10. Switching to a Production-Ready Driver Later

Once you're ready to scale, just switch the QUEUE_CONNECTION in your .env to Redis, SQS, or another driver. Laravel’s queue system abstracts the driver logic, so your job classes and dispatching code won’t need changes!


Summary

Using Laravel’s database queue driver makes it easy to test queued jobs in development without extra services. By configuring your queue connection, running the migrations, and spinning up a queue worker, you can take advantage of Laravel's job system with minimal setup.

Skip the overhead—let the database manage your queues while you develop, and save scaling the infrastructure for later!


Further Reading: