Queues help your app do tasks in the background. This keeps your app fast and smooth.
Queue configuration in Laravel
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
sync driver, which runs jobs immediately without queueing. Good for local testing.Laravel
<?php return [ 'default' => env('QUEUE_CONNECTION', 'sync'), 'connections' => [ 'sync' => [ 'driver' => 'sync', ], ], ];
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, ], ], ];
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
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.
Practice
1. What is the main purpose of queue configuration in Laravel?
easy
Solution
Step 1: Understand the role of queues in Laravel
Queues allow Laravel to run tasks in the background, improving app speed and user experience.Step 2: Identify what queue configuration controls
Queue configuration tells Laravel which driver to use and how to manage these background jobs.Final Answer:
To define how Laravel handles background jobs -> Option DQuick Check:
Queue config = background job handling [OK]
Hint: Queues handle background jobs, not database or auth [OK]
Common Mistakes:
- Confusing queue config with database settings
- Thinking queue config manages user login
- Assuming it controls web server setup
2. Which of the following is the correct way to set the default queue driver in Laravel's
config/queue.php file?easy
Solution
Step 1: Recall Laravel's queue config syntax
The default queue driver is set with the key 'default' and uses the environment variable 'QUEUE_CONNECTION'.Step 2: Match the correct syntax
'default' => env('QUEUE_CONNECTION', 'sync'), correctly uses 'default' => env('QUEUE_CONNECTION', 'sync').Final Answer:
'default' => env('QUEUE_CONNECTION', 'sync') -> Option CQuick Check:
Default driver uses QUEUE_CONNECTION [OK]
Hint: Default driver key is 'default' with QUEUE_CONNECTION env [OK]
Common Mistakes:
- Using 'QUEUE_DRIVER' instead of 'QUEUE_CONNECTION'
- Using wrong array keys like 'driver' or 'connection'
- Confusing default driver with connection name
3. Given this snippet from
config/queue.php:
'connections' => [
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
],
]
What will happen if a job fails when using the 'database' queue driver?medium
Solution
Step 1: Understand Laravel's failed job handling
Laravel logs failed jobs to a 'failed_jobs' table if the failed job feature is configured.Step 2: Connect the database driver behavior
Using the 'database' driver with a jobs table means failed jobs can be recorded for later review.Final Answer:
The failed job will be logged in the 'failed_jobs' table if configured -> Option BQuick Check:
Failed jobs logged in 'failed_jobs' table [OK]
Hint: Failed jobs log to 'failed_jobs' table if set up [OK]
Common Mistakes:
- Assuming jobs retry forever without limit
- Thinking failed jobs are deleted immediately
- Believing Laravel crashes on job failure
4. You set
'default' => env('QUEUE_CONNECTION', 'redis') in config/queue.php but your jobs are not being processed. Which of these is the most likely cause?medium
Solution
Step 1: Check Redis connection requirements
Laravel requires the Redis server to be running and reachable for the Redis queue driver to work.Step 2: Rule out other causes
Queue workers are started via CLI withphp artisan queue:work, not routes. Redis is a valid driver, so no need to switch to database. Laravel fully supports Redis queues.Final Answer:
The Redis server is not running or not reachable -> Option AQuick Check:
Redis driver needs Redis server running [OK]
Hint: Redis driver needs Redis server running and reachable [OK]
Common Mistakes:
- Thinking queue workers run via routes
- Believing Redis is unsupported
- Ignoring Redis server status
5. You want to configure Laravel to use Redis for queues but fallback to the database driver if Redis is unavailable. Which approach correctly implements this fallback in
config/queue.php?hard
Solution
Step 1: Understand Laravel's queue fallback behavior
Laravel does not automatically fallback between drivers; fallback must be handled in code.Step 2: Identify correct fallback implementation
Set 'default' => env('QUEUE_CONNECTION', 'redis'), then in your job code catch Redis exceptions and dispatch to database queue describes setting Redis as default and catching Redis failures in job code to dispatch to database queue.Step 3: Rule out other approaches
Simply listing both connections without extra code lacks fallback logic. Manually changing the default or .env file is not automatic. Laravel has no built-in automatic fallback.Final Answer:
Set 'default' => env('QUEUE_CONNECTION', 'redis'), then in your job code catch Redis exceptions and dispatch to database queue -> Option AQuick Check:
Fallback requires code handling, not just config [OK]
Hint: Laravel needs code to fallback between queue drivers [OK]
Common Mistakes:
- Assuming Laravel auto-fallbacks between drivers
- Expecting config alone handles fallback
- Not catching exceptions in job code
