Fix laravel queue job stalls redis sentinel

Avatar
M

Maksudur Rahman

Software Engineer•
0Views
5mRead
0Reactions

If your Laravel 11 workers suddenly stop pulling jobs and sit idle, you're probably looking at laravel queue job stalls redis sentinel. The symptom is a silent hang – the queue shows pending jobs, but the php artisan queue:work process never logs anything. Below is a fast way to pinpoint the cause and apply a proven fix.

TL;DR / Quick Summary

  • Reproduce the stall with the minimal Docker Compose setup below.

  • Add a health‑check script that pings Sentinel and restarts the worker on failure.

  • Set retry_after ≈ queue:work --timeout + buffer (e.g., 90s).

  • Lower Sentinel down-after-milliseconds to 5‑10 s and enable parallel-syncs.

  • Restart Supervisor; jobs flow again.

Reproducing the stall with Docker Compose

The following files spin up a Laravel 11 app, a Redis master, a replica, and a Sentinel trio. The setup mirrors a typical HA deployment.

docker-compose.yml

# File: docker-compose.yml
version: '3.9'
services:
  app:
    image: php:8.3-fpm
    working_dir: /var/www/html
    volumes:
      - ./:/var/www/html
    depends_on:
      - redis-master
      - sentinel
    environment:
      - REDIS_HOST=sentinel
      - REDIS_SENTINEL_SERVICE=redis-sentinel
      - QUEUE_CONNECTION=redis
    command: "php artisan queue:work redis --sleep=3 --tries=3"

  redis-master:
    image: redis:7-alpine
    command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
    volumes:
      - ./redis/master.conf:/usr/local/etc/redis/redis.conf

  redis-replica:
    image: redis:7-alpine
    command: ["redis-server", "/usr/local/etc/redis/replica.conf"]
    depends_on:
      - redis-master
    volumes:
      - ./redis/replica.conf:/usr/local/etc/redis/replica.conf

  sentinel:
    image: redis:7-alpine
    command: ["redis-sentinel", "/usr/local/etc/redis/sentinel.conf"]
    depends_on:
      - redis-master
      - redis-replica
    volumes:
      - ./redis/sentinel.conf:/usr/local/etc/redis/sentinel.conf
    ports:
      - "26379:26379"

redis/master.conf

# File: redis/master.conf
port 6379
bind 0.0.0.0
protected-mode no
logfile ""

redis/replica.conf

# File: redis/replica.conf
port 6380
replicaof redis-master 6379
bind 0.0.0.0
protected-mode no
logfile ""

redis/sentinel.conf

# File: redis/sentinel.conf
port 26379
sentinel monitor redis-sentinel redis-master 6379 2
sentinel down-after-milliseconds redis-sentinel 5000
sentinel failover-timeout redis-sentinel 15000
sentinel parallel-syncs redis-sentinel 1

Bring the stack up with docker compose up -d. Dispatch a test job:

php artisan tinker <<'EOF'
App\Jobs\ExampleJob::dispatch();
EOF

Watch the worker logs. After a few seconds you’ll see the job start, then the Sentinel forces a master switch. The worker never receives the FINISHED event – it stalls.

Adding health‑check scripts

A tiny Bash script can detect a missing master and force a worker restart.

scripts/redis-health.sh

#!/usr/bin/env bash
# File: scripts/redis-health.sh
HOST=${REDIS_HOST:-sentinel}
SERVICE=${REDIS_SENTINEL_SERVICE:-redis-sentinel}
MASTER=$(redis-cli -h $HOST -p 26379 sentinel get-master-addr-by-name $SERVICE | head -n1)
if [[ -z $MASTER ]]; then
  echo "[WARN] Sentinel reports no master – restarting queue worker"
  supervisorctl restart laravel-worker:* || true
else
  echo "[OK] Master is $MASTER"
fi

Make it executable and add it to Supervisor's eventlistener section:

supervisor/conf.d/laravel-worker.conf

# File: supervisor/conf.d/laravel-worker.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work redis --sleep=3 --tries=3 --timeout=60
autostart=true
autorestart=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/log/laravel/worker.log

[eventlistener:redis-health]
command=/var/www/html/scripts/redis-health.sh
events=TICK_60
process_name=%(program_name)s

Supervisor now runs the health check every minute. When Sentinel reports a missing master, the script triggers a restart, unblocking the stalled jobs.

Tuning retry_after and worker timeout

Laravel treats a job as failed if the worker doesn’t acknowledge it within retry_after. With Sentinel‑driven failovers you need a buffer.

config/queue.php (excerpt)

// File: config/queue.php
'connections' => [
    'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
        'queue' => env('REDIS_QUEUE', 'default'),
        'retry_after' => env('REDIS_RETRY_AFTER', 90), // ← increase from default 60
        'block_for' => null,
    ],
],

Set REDIS_RETRY_AFTER=90 in .env. Also match the Artisan flag:

php artisan queue:work redis --timeout=80 --sleep=3

The extra 10 seconds covers the time Sentinel needs to elect a new master.

Sentinel failover settings that matter

Two Sentinel knobs directly affect stall length:

Setting

Recommended value

Why it helps

down-after-milliseconds

5000 (5 s)

Detects master loss quickly

failover-timeout

15000 (15 s)

Gives workers time to reconnect

parallel-syncs

1

Limits replica lag during failover

Adjust the values in redis/sentinel.conf and restart the Sentinel containers.

Common Pitfalls & Troubleshooting

  • Worker still hangs after restart – Check that the REDIS_HOST env var points to the Sentinel address, not a single Redis node. The driver must resolve the master on each request.

  • php artisan queue:restart does nothing – Supervisor may be holding the old PID. Run supervisorctl reread && supervisorctl update after changing the config.

  • Logs show Connection reset by peer – The replica was still syncing when the worker tried to reconnect. Lower parallel-syncs or increase retry_after.

  • Docker health‑check fails – Ensure the health script has execute permission (chmod +x scripts/redis-health.sh).

  • If you hit a dead‑end, the Fix laravel queue workers stuck after deploy guide covers similar restart loops.

    FAQ

    Why do Laravel workers stall when Sentinel promotes a new master?

    The worker keeps the old TCP connection. Until it reconnects, the queue driver thinks the master is still reachable, so jobs sit in the pending list.

    What retry_after value works best with Sentinel?

    Pick a value a bit higher than your longest job plus the Sentinel down-after-milliseconds. 90 seconds works for most 30‑second jobs.

    Do I need to change down-after-milliseconds?

    Yes. A lower value (5‑10 s) forces Sentinel to notice a failed master fast enough that the worker can reconnect before the job timeout expires.

    Can Supervisor auto‑restart a stalled worker?

    Configure stopwaitsecs and a health‑check event listener as shown. Supervisor alone won’t detect a silent network stall, so the script is essential.


    Diagram: queue flow with Sentinel failover

    flowchart TD
        A[Job dispatched] --> B[Laravel pushes to Sentinel]
        B --> C{Sentinel returns master IP}
        C -->|OK| D[Worker pulls job]
        C -->|Master down| E[Sentinel elects new master]
        E --> D
        D --> F[Job processed]
        F --> G[ACK to Redis]
        style E fill:#f9c,stroke:#333,stroke-width:2px

    That visual captures the moment where the worker waits for the new master. The health‑check script cuts the wait time dramatically.


    Next steps

    1. Spin up the Docker Compose stack.

    2. Verify the stall by killing the master container (docker compose stop redis-master).

    3. Apply the health‑check script and retry_after tweak.

    4. Observe the worker resume processing.

    You now have a reproducible environment, a concrete fix, and a monitoring hook that prevents future stalls.


    Connect with KoderSolution 🚀

    Master modern software engineering, scalable architectures, and advanced backend systems with KoderSolution:

    • 🌐 Official Website: Read more architectural deep-dives and tutorials on kodersolution.com

    • 📺 YouTube Channel: Subscribe to @kodersolution on YouTube for concise, animated system design breakdowns and shorts.

    • 📱 Facebook Page: Follow our official Facebook Page for daily development tips, architecture discussions, and community updates.

    💬 What's your setup? What strategies do you use to keep Laravel queue workers alive during Redis Sentinel failovers, and have you built any custom health‑checks? Share your thoughts and join the discussion below!

    Maksudur Rahman
    M
    Written by•Software Engineer

    Maksudur Rahman

    Senior Developer Advocate & System Lead Moderator.

    React to this article

    Recommended Articles

    View all articles