Krishnan's Personal Website


Home | About | Blog | Projects (Work) | Projects (Personal) | Connect with me


How I Fixed My Freezing Laravel Queue Workers



Published On: Oct 31 2026
Written By: Krishnan Sethuraman
Category: Infrastructure


How I Fixed My Freezing Laravel Queue Workers

A few weeks ago, I found myself dealing with a frustrating infrastructure problem. My background worker server, a dedicated, headless instance running nothing but a Laravel queue worker to process RabbitMQ tasks, kept silently freezing up.

There was absolutely no heavy server load. CPU usage was near zero, and RAM was totally fine. Yet, out of nowhere, the notifications would stop sending. The only fix was manually logging into the server and restarting the Systemd service.

My initial, chaotic engineering instinct was to rewrite the entire background worker process in C or C++ to make it hyper-fast and un-freezable. But before doing a massive, time-consuming rewrite, I looked closer at how long-running PHP scripts actually behave.

Here is exactly why workers freeze at zero load, and how I fixed mine with a few tweaks to my Systemd service file and deployment workflow.

 

The Invisible Culprit: Idle Connections

When a PHP CLI script runs indefinitely, it eventually hits a wall. Because it isn't receiving constant, heavy traffic, the database connection sits idle. By default, MySQL silently drops idle connections after 8 hours.

The next time a notification message arrives in RabbitMQ, the worker tries to process it, runs into a dead or timed-out database socket, and lock up. Because the PHP process doesn't technically "crash" or exit, Systemd assumes everything is fine and never triggers its restart policy. The worker essentially becomes a zombie.

Instead of writing custom binary code, the fix comes down to configuring the Laravel runtime to proactively heal itself.

 

Fix 1: Forcing Workers to Gracefully Recycle

Laravel’s worker command includes built-in parameters explicitly designed to combat memory creep and network drops. Instead of running a plain php artisan queue:work command forever, you can pass boundaries to the script:

php artisan queue:work --queue=notifications --rest=1 --tries=3 --max-time=3600 --max-jobs=5000
  • --max-time=3600: This tells the worker to run for exactly one hour and then gracefully exit.
  • --max-jobs=5000: If the server experiences a sudden wave of data and processes 5,000 jobs before the hour is up, it exits.

When the worker exits cleanly, Systemd sees it close and instantly spins up a fresh instance. This keeps database connections fresh, wipes out memory leaks, and clears out frozen sockets without dropping or losing any messages.

 

Fix 2: Setting Up Fail-Safe Scaling via Systemd Templates

Even though my server load is non-existent right now, I converted my standard configuration into a Systemd template. By adding a single @ character to the service filename (e.g., laravel-worker@.service), Systemd lets you spin up independent, simultaneous clones of your worker.

Here is the clean, self-healing configuration file:

[Unit]
Description=Team Tasks Notification Worker Instance %I
After=network.target rabbitmq-server.service

[Service]
User=jenkins
Group=jenkins
Restart=always
RestartSec=5s
WorkingDirectory=/var/www/html/path-to-worker

ExecStart=/usr/bin/php artisan queue:work --queue=notifications --rest=1 --tries=3 --max-time=3600 --max-jobs=5000
KillMode=process
TimeoutStopSec=30s

[Install]
WantedBy=multi-user.target

Because RabbitMQ uses a built-in message acknowledgment feature, running multiple workers gives you instant redundancy. If Instance 1 drops a network packet or drops mid-job, RabbitMQ instantly notices and hands the task to Instance 2.

Since the baseline load is zero, running two concurrent instances uses a tiny, negligible bit of idle RAM but ensures 100% uptime coverage.

 

Fix 3: Aligning the User to the CI/CD Pipeline

To deploy code effortlessly, my Jenkins pipeline owns the project directory. Initially, I had jumped to using brute-force permission workarounds like chmod -R 777 on the storage and cache folders to make sure the app could write logs.

But 777 permissions are messy and unnecessary. The cleaner, more reliable path is making the Systemd runtime user match the deployment automation user.

By setting User=jenkins and Group=jenkins right inside the service file, the application can naturally read the code, write logs, and manage internal caches without hitting permission walls.

To clean up the temporary permissions on the server, I ran a quick reset sequence:

# find /var/www/html/path-to-worker -type d -exec chmod 755 {} \;
# find /var/www/html/path-to-worker -type f -exec chmod 644 {} \;


# chmod -R 775 /var/www/html/path-to-worker/storage
# chmod -R 775 /var/www/html/path-to-worker/bootstrap/cache

 

Fix 4: The Jenkins "Wake Up" Command

Because Systemd keeps these workers loaded inside server memory, deploying fresh code via Jenkins won't immediately update what the worker is doing. It will continue running the old code version until it hits its one-hour expiration limit.

To make code updates go live instantly, I added one simple line to the very end of my Jenkins deployment automation shell script:

php artisan queue:restart

This commands loops through the system and tells all active Laravel workers to finish their exact current task, drop out cleanly, and let Systemd reboot them using the brand-new codebase in milliseconds.

 

Wrapping Up

By letting Laravel handle its own process lifecycles, matching the worker to the deployment user, and letting Systemd manage the uptime loop, the freezing issues completely vanished. It is a lightweight, zero-cost architecture that leaves the codebase intact while keeping background processing rock-solid.

 

Related Articles

Krishnan Sethuraman
Krishnan Sethuraman

Founder & CTO of Geedesk. Passionate about building software from scratch, launching SaaS products, and helping teams deliver enterprise-grade solutions.

Like what you are reading?

Discover more similar articles sent to your email