To efficiently scale your Laravel app to handle 5000 RPS using Laravel Cloud (and leveraging Laravel Octane), here are some insights and recommended settings:
1. Ensure You're Using Octane's Strengths
Laravel Octane keeps your application in memory between requests, offering significant speedups for high RPS workloads. Make sure you’re using a supported server (Swoole or RoadRunner).
2. Octane Worker Settings
For high concurrency, adjust the following Octane settings in your config/octane.php:
// config/octane.php
'workers' => env('OCTANE_WORKERS', 24), // Start with number of CPU cores * 2
'task_workers' => env('OCTANE_TASK_WORKERS', 8),
'max_requests' => env('OCTANE_MAX_REQUESTS', 5000), // Restart worker after this many requests
You may need to increase OCTANE_WORKERS if you have more CPU cores available. Test what your CPU can handle and increment as needed.
3. Optimize Your App
- Cache Config/Routes: Always use
php artisan config:cacheandphp artisan route:cache. - Database: Use a managed DB and optimize queries (limit N+1 problems).
- Queue Jobs: Offload heavy work to queues.
- Sessions/Cache driver: Use Redis for both, not file/database drivers.
4. Use a Load Balancer
- If possible, scale horizontally—run multiple instances of your app behind a load balancer.
- Leverage Laravel Cloud's scaling settings to increase the number of containers as needed.
5. Scale Database & Dependencies
- Ensure your DB, cache, queue, etc., can handle the load (don’t let these become bottlenecks).
6. Monitor Octane Memory Leaks
- Memory leaks or long request times may force workers to restart. Use monitoring/logs to track this.
7. Test Practically
- Use tools like wrk, siege, or ab (Apache Bench) to simulate load.
Example benchmarking command:
wrk -t16 -c500 -d30s https://your-app-url.com/
8. Sample octane.ini for Swoole
If you're using Swoole, you can fine-tune via an .ini or in your config/octane.php:
'swoole' => [
'options' => [
'reactor_num' => env('SWOOLE_REACTOR_NUM', 12), // usually CPU cores
'worker_num' => env('SWOOLE_WORKER_NUM', 24),
'max_request' => env('SWOOLE_MAX_REQUEST', 5000),
'task_worker_num' => env('SWOOLE_TASK_WORKER_NUM', 8),
// Other Swoole specific options
],
],
Summary Checklist
- Use Octane (Swoole/RoadRunner)
- Set workers to at least number of CPU cores * 2
- Use Redis for cache/sessions
- Offload heavy processes to queues
- Use horizontal scaling with a Load Balancer
- Optimize queries, Eloquent usage, and avoid bootstrapping Laravel on each request
- Test with real-world RPS and monitor
Further reading:
You might need more than just CPU if you run into connection limits on DB/cache, so scale dependencies accordingly.
Let me know if you have questions about a specific configuration or error you’re hitting!