The --stop-when-empty-for option keeps queue workers alive for a specific grace period after the queue empties, preventing rapid process churn during bursty workloads.
Running php artisan queue:work --stop-when-empty in serverless environments or container auto-scalers terminates the worker process immediately when no jobs remain. If new jobs arrive seconds later, new processes must spin up continuously.
The --stop-when-empty-for option adds a configurable idle timeout:
# Stops worker immediately once queue is empty
php artisan queue:work --stop-when-empty
# Keeps worker running for 60 idle seconds before exiting
php artisan queue:work --stop-when-empty-for=60
- Prevents process start/stop churn during intermittent job spikes
- Ideal for AWS ECS, Kubernetes HPA, and Serverless worker instances
- Retains database connection pooling while waiting for trailing jobs
Related Tips
View all tips →Optimize Queue Worker Polling Overheads in Production
Use queue:work with appropriate sleep configuration or Redis blocking pops to reduce database CPU polling overheads.
Dispatch Jobs After Transaction Commit with DB::afterCommit()
Use DB::afterCommit() to defer job dispatching until surrounding database transactions complete successfully.