0
0
Laravelframework~5 mins

Queue configuration in Laravel

Choose your learning style9 modes available
Introduction

Queues help your app do tasks in the background. This keeps your app fast and smooth.

Sending emails without making users wait
Processing uploaded files after saving them
Running reports or data cleanup without slowing the app
Handling notifications or messages in the background
Syntax
Laravel
<?php

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

    'connections' => [
        'sync' => [
            'driver' => 'sync',
        ],

        'database' => [
            'driver' => 'database',
            'table' => 'jobs',
            'queue' => 'default',
            'retry_after' => 90,
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => env('REDIS_QUEUE', 'default'),
            'retry_after' => 90,
            'block_for' => null,
        ],

        // other drivers like sqs, beanstalkd, etc.
    ],

    'failed' => [
        'driver' => env('QUEUE_FAILED_DRIVER', 'database-uuids'),
        'database' => env('DB_CONNECTION', 'mysql'),
        'table' => 'failed_jobs',
    ],
];

This file is usually config/queue.php in Laravel.

The default key sets which queue driver Laravel uses by default.

Examples
This example uses the sync driver, which runs jobs immediately without queueing. Good for local testing.
Laravel
<?php

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

    'connections' => [
        'sync' => [
            'driver' => 'sync',
        ],
    ],
];
This example uses the database driver. Jobs are stored in the jobs table and retried after 120 seconds if they fail.
Laravel
<?php

return [
    'default' => env('QUEUE_CONNECTION', 'database'),

    'connections' => [
        'database' => [
            'driver' => 'database',
            'table' => 'jobs',
            'queue' => 'emails',
            'retry_after' => 120,
        ],
    ],
];
This example uses the redis driver for fast queueing with Redis server.
Laravel
<?php

return [
    'default' => env('QUEUE_CONNECTION', 'redis'),

    'connections' => [
        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => 'default',
            'retry_after' => 90,
            'block_for' => null,
        ],
    ],
];
Sample Program

This command shows the current queue driver and dispatches a job to send a welcome email. If the queue driver is sync, the job runs immediately and prints the message.

Laravel
<?php

namespace AppConsoleCommands;

use IlluminateConsoleCommand;
use IlluminateSupportFacadesQueue;
use AppJobsSendWelcomeEmail;

class QueueDemoCommand extends Command
{
    protected $signature = 'queue:demo';
    protected $description = 'Demonstrate queue configuration and job dispatch';

    public function handle()
    {
        $this->info('Current default queue connection: ' . config('queue.default'));

        // Dispatch a job to the queue
        SendWelcomeEmail::dispatch('user@example.com');

        $this->info('Dispatched SendWelcomeEmail job to the queue.');
    }
}

// Job class
namespace AppJobs;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateQueueInteractsWithQueue;
use IlluminateQueueSerializesModels;

class SendWelcomeEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected string $email;

    public function __construct(string $email)
    {
        $this->email = $email;
    }

    public function handle()
    {
        // Simulate sending email
        echo "Sending welcome email to {$this->email}\n";
    }
}

// To run this demo, set QUEUE_CONNECTION=sync in .env for immediate execution.
// Then run: php artisan queue:demo
OutputSuccess
Important Notes

Queue drivers like database or redis require setup of tables or servers.

Using sync driver runs jobs immediately, which is good for testing but not for production.

Set retry_after to control how long Laravel waits before retrying a failed job.

Summary

Queues let your app do slow tasks in the background.

Configure queue drivers in config/queue.php.

Use sync for testing, and database or redis for real queueing.