Laravel Queue Workers Stuck on 15‑Second Timeouts After Composer Install: Why Redis Flushes on VPS Cause Crashes and How to Fix It Fast
You’ve just run composer install on a fresh Laravel project, hit php artisan queue:work, and the workers die after exactly 15 seconds. Nothing else is running, the logs are empty, and the console screams “timeout”. Sound familiar? You’re not alone—this is a classic symptom of a mis‑behaving Redis instance on a low‑tier VPS that silently wipes its own data during a high‑CPU spike.
When queue workers die, your email, notifications, and API jobs disappear. In a production SaaS that processes hundreds of requests per second, a 15‑second pause can cost you revenue, users, and reputation. Fixing it quickly restores reliability and keeps your Laravel‑WordPress hybrid stack humming.
Common Causes
- Redis running out of memory and auto‑evicting keys (default
volatile‑lrupolicy). - Linux OOM‑killer terminating the Redis process after Composer’s heavy CPU load.
- Supervisor misconfiguration that restarts workers with a hard 15‑second timeout.
- Improper
php-fpmrequest_terminate_timeoutset to 15 seconds. - Docker or VPS snapshots that reset the
/var/lib/redisdirectory during a restart.
Step‑by‑Step Fix Tutorial
1. Verify Redis Memory Limits
# Check Redis memory usage
redis-cli INFO memory | grep used_memory_human
# Show maxmemory setting
redis-cli CONFIG GET maxmemory
maxmemory to 75% of your VPS RAM and use allkeys‑lru to avoid silent evictions.2. Adjust Redis Config
# /etc/redis/redis.conf
maxmemory 2gb
maxmemory-policy allkeys-lru
stop-writes-on-bgsave-error no
3. Restart Redis with Systemd
sudo systemctl daemon-reload
sudo systemctl restart redis.service
sudo systemctl enable redis.service
4. Increase Supervisor Timeout
# /etc/supervisor/conf.d/laravel-queue.conf
[program:laravel-queue]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work redis --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
numprocs=4
redirect_stderr=true
stdout_logfile=/var/log/laravel-queue.log
stopwaitsecs=3600 ; <-- increase from default 10
5. Tune PHP‑FPM
# /etc/php/8.2/fpm/php.ini
request_terminate_timeout = 300
request_terminate_timeout to 0 on production; it disables timeout checks and can hide real crashes.6. Re‑run Composer with Optimizations
composer install --optimize-autoloader --no-dev
php artisan config:cache
php artisan route:cache
VPS or Shared Hosting Optimization Tips
- Use a dedicated Redis VM or managed Redis (e.g., AWS ElastiCache) for high‑traffic apps.
- Allocate at least 1 GB RAM for Redis on a 2 GB VPS.
- Enable swap only as a safety net; never rely on it for production.
- On shared hosting, switch to
databasedriver for queues if Redis isn’t allowed. - Set
opcache.memory_consumptionto 256M for PHP‑FPM on Ubuntu 22.04.
Real World Production Example
Acme SaaS runs Laravel 10 on a 4‑core 8 GB Ubuntu 20.04 VPS behind Nginx. After a recent Composer upgrade, the queue workers started timing out. Applying the steps above reduced memory‑eviction events from 200+ per minute to zero, and queue latency dropped from 15 seconds to < 200 ms.
Before vs After Results
| Metric | Before Fix | After Fix |
|---|---|---|
| Queue Success Rate | 78 % | 99.9 % |
| Avg Job Latency | 15 s | 0.18 s |
| Redis Evictions | 215/min | 0/min |
Security Considerations
- Bind Redis to
127.0.0.1or use a private VPC network. - Enable
requirepassand rotate the password every 90 days. - Use
tls-portandtls-cert-fileif you must expose Redis externally. - Set proper file permissions on
/var/lib/redis(ownerredis:redis).
Bonus Performance Tips
- Configure Nginx fastcgi cache for static Blade views.
- Enable
opcache.validate_timestamps=0on production. - Run
php artisan horizoninstead of raw queue workers for better metrics. - Split heavy jobs into smaller chunks using Laravel’s
batchAPI. - Use Cloudflare page rules to cache API GET responses for 5 minutes.
FAQ
Q: My VPS restarts every time Composer hitsphp artisan optimize. Why?
A: Composer spawns many PHP processes that can exceed the VPS memory limit, triggering the OOM‑killer. Increase swap or upgrade RAM, and addCOMPOSER_MEMORY_LIMIT=-1to the build script.
Q: Should I useredis-cli flushallduring deployments?
A: No. Flushing clears every queued job and cached config. Instead, gracefully stop Horizon, runphp artisan config:cache, and restart Horizon.
Final Thoughts
Queue timeouts after a Composer install are rarely a Laravel bug; they’re a symptom of server‑level resource contention—most often Redis memory pressure on a cheap VPS. By giving Redis a proper memory ceiling, extending Supervisor’s timeout, and tightening PHP‑FPM limits, you turn a flaky dev environment into a rock‑solid production pipeline.
If you love the speed of a tuned Laravel‑WordPress stack, consider moving to a managed Redis provider and a VPS with at least 2 GB RAM dedicated to caching. The upfront cost pays for itself in reduced downtime, higher API speed, and happier customers.
No comments:
Post a Comment