Introduction
Modern web and API applications often perform time-consuming tasks like sending emails, processing images, or generating reports. If these tasks run during a userβs request, they slow down response times.
Laravel Queues & Jobs for APIs solves this problem, allowing you to push heavy tasks into the background while keeping the API fast and responsive.
For interviews, expect questions like βHow do queues work in Laravel?β or βWhen would you use queues instead of synchronous execution?β.
What are Queues in Laravel?
A queue is a system that holds jobs (tasks) to be processed later by a worker.
API Example:
- Instead of sending an email directly after registration, push it to a queue.
- User gets an instant response (201 Created).
- Email gets sent in the background.
Laravel supports multiple queue drivers:
- Database
- Redis
- Amazon SQS
- Beanstalkd
- Sync (runs immediately, for local dev)
Creating a Job in Laravel
Step 1: Generate a Job
php artisan make:job SendWelcomeEmail
Step 2: Define Logic
app/Jobs/SendWelcomeEmail.php
class SendWelcomeEmail implements ShouldQueue {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user;
public function __construct(User $user) {
$this->user = $user;
}
public function handle() {
Mail::to($this->user->email)->send(new WelcomeMail($this->user));
}
}
Step 3: Dispatch Job in Controller
public function register(Request $request) {
$user = User::create($request->all());
// Push email sending to queue
dispatch(new SendWelcomeEmail($user));
return response()->json(['message' => 'User registered successfully'], 201);
}
π Now the API responds instantly, while the email is sent in the background.
Running Queue Workers
php artisan queue:work
π This listens for jobs and processes them in the background.
For local testing:
php artisan queue:listen
Configuring Queue Drivers
Set driver in .env:
QUEUE_CONNECTION=database
If using database:
php artisan queue:table php artisan migrate
π Jobs will be stored in the jobs table until processed.
Delaying Jobs
dispatch(new SendWelcomeEmail($user))->delay(now()->addMinutes(10));
π Useful for reminders, follow-ups, or scheduled tasks.
Chaining Jobs
SendInvoice::withChain([
new SendEmailReceipt($user),
new UpdateStats($user)
])->dispatch();
π Great for workflows like order processing.
Queues in APIs: Real Use Cases
- User Registration β Send welcome email asynchronously.
- E-commerce API β Process payments, send invoices, update inventory.
- Media API β Resize images or transcode videos in the background.
- Reports API β Generate large CSV/PDF reports asynchronously.
- Notifications β Send push notifications without blocking API calls.
Common Beginner Mistakes
- Forgetting to run
queue:workβ jobs stay stuck. - Using sync driver in production β jobs block requests.
- Not retrying failed jobs β silent job failures.
- Dispatching jobs inside loops instead of batching.
Sample Interview Questions & Answers
Q: Why use queues in Laravel?
A: To handle heavy tasks asynchronously, keeping apps fast and responsive.
Q: What are some queue drivers Laravel supports?
A: Database, Redis, Amazon SQS, Beanstalkd, and Sync.
Q: What is the difference between queue:listen and queue:work?
A: queue:work is optimized for production, while queue:listen reloads on every job (useful for dev).
Q: How do you handle failed jobs in Laravel?
A: Configure failed_jobs table (php artisan queue:failed-table) and monitor via php artisan queue:failed.
Q: Can you delay jobs in Laravel?
A: Yes, using delay() method when dispatching a job.
Q: Whatβs the difference between sync and async jobs?
A: Sync runs immediately within the request; async pushes to a queue and runs later by a worker.
Mini Project Idea
π Build a File Upload API with Queued Processing:
- User uploads a file via API.
- Job is queued to process/resize the file.
- API responds instantly with
{"status": "processing"}. - Later, user can check status via another endpoint.
Closing Note
Laravel Queues & Jobs for APIs, scalable, and user-friendly. They are essential for handling background tasks like notifications, payments, and report generation.
Laravel Framework Mastery
Laravel Authentication & Authorization
β Secure your web applications effortlessly by implementing Laravelβs robust authentication and authorization features for users and roles
Laravel as an API Backend
β Build powerful, scalable APIs with Laravel, leveraging routes, controllers, and resources for seamless data handling and integration
Laravel Passport vs Sanctum
β Understand the key differences between Laravel Passport and Sanctum to choose the right authentication method for your web and mobile APIs























0 Comments