Process Background Jobs with Laravel Queues
Move slow work out of HTTP requests with Laravel 13 database queues, jobs, retries and workers.
Move slow work out of HTTP requests with Laravel 13 database queues, jobs, retries and workers. This tutorial focuses on a small implementation you can run, inspect and extend instead of hiding the important behavior behind scaffolding.
Prerequisites#
- Laravel 13, PHP, Composer and a configured test database
- Use placeholder credentials and a non-production environment.
- Know the basic syntax of Laravel.
What we will build#
We will implement the core path, exercise it with a realistic request or test, and identify the production concerns that should remain outside a minimal example.
Step 1: Prepare the project#
Create a focused branch and confirm the current application or sample database works before changing it. Keep secrets in environment variables, commit an example environment file without values, and make the smallest schema change that supports the use case.
Step 2: Implement the core behavior#
class GenerateReport implements ShouldQueue
{
use Queueable;
public int $tries = 3;
public array $backoff = [10, 60, 300];
public function __construct(public int $reportId) {}
public function handle(ReportService $reports): void
{
$reports->generate($this->reportId);
}
}
// Dispatch after the database transaction commits.
GenerateReport::dispatch($report->id)->afterCommit();Keep validation at the boundary. Allow-list client-controlled fields, return an explicit response, and avoid exposing internal exceptions or credentials.
Step 3: Test the successful path#
php artisan queue:work --queue=default --tries=3
php artisan queue:failedConfirm both the response and the resulting state. A status code alone is not enough when the operation changes a database, dispatches work or writes a file.
Step 4: Test failures#
Repeat the test with missing fields, invalid types, unauthorized access and a dependency failure. The application should reject the request predictably without leaking a stack trace, secret or filesystem path.
Common problems#
- Inputs are trusted too early: validate and normalize at the system boundary.
- The example works only once: test repeated and concurrent requests where the operation changes state.
- Errors are inconsistent: use one documented error shape and attach a request identifier in production.
Best practices#
- Use the current supported runtime and pin important dependencies.
- Keep examples minimal, but preserve authentication, validation and error handling.
- Add automated coverage for the behavior most likely to regress.
- Log identifiers and outcomes, never passwords, tokens or complete sensitive payloads.
Conclusion#
You now have a working foundation for process background jobs with laravel queues. Extend it by separating infrastructure from business rules, adding authorization where resources belong to users, and measuring the behavior under realistic data and failure conditions.