Kritim Yantra
Jun 19, 2025
Solution? Laravel Queues! But how do you run them on budget shared hosting?
Let’s break it down—step by step.
Imagine a busy restaurant:
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.
Most shared hosts don’t allow long-running processes (like queue:work
).
But we can trick the system using cron jobs!
Shared hosts usually support database queues. Edit .env
:
QUEUE_CONNECTION=database
Then, create the jobs table:
php artisan queue:table
php artisan migrate
Example: SendWelcomeEmail
php artisan make:job SendWelcomeEmail
// app/Jobs/SendWelcomeEmail.php
public function handle()
{
Mail::to($this->user->email)->send(new WelcomeEmail());
}
Instead of sending mail directly:
Mail::to($user)->send(new WelcomeEmail()); // ❌ Blocks the request
Do this:
SendWelcomeEmail::dispatch($user); // ✅ Runs in background
Since queue:work
won’t stay running, we use cron jobs to trigger queue:restart
and queue:work --once
every minute.
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:
Trigger a job:
SendWelcomeEmail::dispatch($user);
Check if it worked:
php artisan queue:failed # 👀 Any errors?
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!
✅ 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.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google