How to Run Laravel Job Queues in Production on Shared Hosting

Author

Kritim Yantra

Jun 19, 2025

How to Run Laravel Job Queues in Production on Shared Hosting

Ever faced this frustrating situation?

  • Your Laravel app sends emails, processes data, or generates reports
  • But everything freezes when a slow task runs! 😤
  • You can’t afford a fancy server (just shared hosting).

Solution? Laravel Queues! But how do you run them on budget shared hosting?

Let’s break it down—step by step.


🔍 Why Use Queues? (The Traffic Cop Analogy)

Imagine a busy restaurant:

  • If the chef cooks & serves every order one by one, customers wait forever.
  • Instead, orders go to the kitchen (queue), and the chef cooks in the background while the waiter keeps taking new orders.

Laravel Queues work the same way!
Users don’t wait for slow tasks (like sending 1000 emails).
Your app stays fast because heavy work happens in the background.


🛠 Setting Up Queues on Shared Hosting

Most shared hosts don’t allow long-running processes (like queue:work).
But we can trick the system using cron jobs!

Step 1: Configure Your Queue Driver

Shared hosts usually support database queues. Edit .env:

QUEUE_CONNECTION=database  

Then, create the jobs table:

php artisan queue:table  
php artisan migrate  

Step 2: Create a Job

Example: SendWelcomeEmail

php artisan make:job SendWelcomeEmail  
// app/Jobs/SendWelcomeEmail.php  
public function handle()  
{  
    Mail::to($this->user->email)->send(new WelcomeEmail());  
}  

Step 3: Dispatch the Job

Instead of sending mail directly:

Mail::to($user)->send(new WelcomeEmail()); // ❌ Blocks the request  

Do this:

SendWelcomeEmail::dispatch($user); // ✅ Runs in background  

⏳ Running the Queue Worker (The Hack!)

Since queue:work won’t stay running, we use cron jobs to trigger queue:restart and queue:work --once every minute.

Step 1: Add a Cron Entry

In your shared hosting cron tab (e.g., cPanel → Cron Jobs):

* * * * * cd /path-to-your-project && php artisan queue:restart && php artisan queue:work --once  

💡 What this does:

  • Runs every minute.
  • Processes one job per minute (adjust as needed).

Step 2: Test It!

Trigger a job:

SendWelcomeEmail::dispatch($user);  

Check if it worked:

php artisan queue:failed # 👀 Any errors?  

🔥 Alternative: Use a "Poor Man’s Queue"

If cron isn’t reliable, try "queue on page load":

// In a high-traffic route (e.g., homepage)  
if (rand(1, 10) === 1) { // Randomly run jobs 10% of the time  
    Artisan::call('queue:work --once');  
}  

Warning: This is not ideal, but works in a pinch!


� Key Takeaways

Queues prevent slow tasks from blocking users.
Use database driver on shared hosting.
Cron jobs can run queue:work --once every minute.
Test with queue:failed to debug issues.

🚀 Next Steps

  • Monitor queues (log failures).
  • Upgrade to a VPS if traffic grows (for real-time queues).
Ajay Yadav

Ajay Yadav

Senior Full-Stack Engineer

7 + Years Experience

Transforming Ideas Into Digital Solutions

I architect and build high-performance web applications with modern tech:

Laravel PHP 8+ Vue.js React.js Flask Python MySQL

Response time: under 24 hours • 100% confidential

Tags

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts