惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
J
Java Code Geeks
C
Check Point Blog
Last Week in AI
Last Week in AI
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
L
LangChain Blog
有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 司徒正美
IT之家
IT之家
Martin Fowler
Martin Fowler
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
U
Unit 42
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Stop Worker Crashes: Fixing Laravel Queue Memory Leaks in...
Prajapati Pa · 2026-05-20 · via DEV Community

The Silent Killer of Background Jobs

When you deploy a B2B SaaS platform at Smart Tech Devs, your Laravel queues handle the heaviest lifting—processing CSV imports, generating PDF reports, and sending thousands of emails. To process these efficiently, we use the php artisan queue:work command. This runs the worker as a daemon process, meaning the PHP script boots up once and stays alive in memory to process job after job without the overhead of rebooting the framework.

However, this speed comes with a dangerous architectural trade-off: Memory Leaks. Because the PHP process never dies, static variables, singleton instances, and Eloquent model caches slowly accumulate in RAM. Over a few hours (or days), your worker hits the PHP memory limit (usually 128MB or 256MB) and violently crashes with a Allowed memory size exhausted fatal error.

The Enterprise Solution: Graceful Restarts

While you should absolutely profile your code to fix severe leaks, minor memory accumulation in a long-running PHP daemon is almost inevitable. The standard DevOps solution is not to endlessly increase your server's RAM, but to architect Graceful Restarts.

Instead of letting the worker run until it explodes, we instruct Laravel to automatically kill the worker process safely after it hits a specific threshold. Because we use a process monitor like Supervisor on our Ubuntu VPS, the moment the worker dies, Supervisor instantly spawns a fresh, clean process with zero memory bloat.

Configuring Supervisor for Scalability

In Laravel, we control these thresholds using two powerful flags: --max-jobs and --max-time.


# /etc/supervisor/conf.d/laravel-worker.conf

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
# The command tells the worker to die after 1,000 jobs OR 1 hour (3600 seconds)
command=php /var/www/smarttechdevs.com/artisan queue:work --max-jobs=1000 --max-time=3600 --tries=3 --timeout=90
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=forge
numprocs=8 # Run 8 parallel workers
redirect_stderr=true
stdout_logfile=/var/www/smarttechdevs.com/storage/logs/worker.log
stopwaitsecs=3600

Why This Architecture Wins

Let’s break down why this specific command (--max-jobs=1000 --max-time=3600) is the gold standard for backend stability:

  • Predictable Memory: By forcing the PHP process to restart after 1,000 jobs, you guarantee the garbage collector flushes the RAM entirely. The worker never lives long enough to hit the fatal memory limit.
  • Safety First: Unlike a hard crash, Laravel waits for the current job to finish successfully before exiting. No data is lost or corrupted mid-process.
  • Zero Downtime: Supervisor detects the exit code and spins up the replacement worker in milliseconds. Your queue throughput remains massive, but your infrastructure stays perfectly healthy.

Conclusion

A reliable SaaS backend shouldn't require developers to wake up at 3 AM to restart failed queue workers. By acknowledging the reality of PHP memory management and architecting graceful restarts via Supervisor, you build a self-healing infrastructure capable of processing millions of background jobs effortlessly.