Laravel Tutorials
1 min read
Running Laravel's Scheduler on Multiple Servers
A comprehensive guide to configuring distributed scheduled tasks across multi-server Laravel deployments without duplicate executions.
Running Laravel's Scheduler on Multiple Servers
When running a high-availability infrastructure with multiple web and worker nodes, having every server run php artisan schedule:run can result in duplicate executions of critical tasks (such as customer billing or email dispatches).
Using the onOneServer() Mutex
Laravel provides built-in distributed mutex locking through your cache store (Redis, Memcached, or DynamoDB) using the onOneServer() modifier:
// app/Console/Kernel.php or routes/console.php
use Illuminate\Support\Facades\Schedule;
Schedule::command('emails:send-digest')
->dailyAt('06:00')
->onOneServer();
Key Production Tips
- Shared Cache Store: Ensure all application nodes share the exact same Redis or centralized cache cluster.
- NTP Clock Synchronization: Verify that server clocks are tightly synchronized with NTP to avoid mutex contention timeouts.