Traditional PHP reinitializes on every request. Laravel Octane keeps application in memory, boosting performance 3-5x. At ZIRA Software, Octane powers high-traffic applications handling thousands of requests per second.
What is Octane?
Traditional PHP-FPM:
Request → Bootstrap Laravel → Execute → Shutdown
Request → Bootstrap Laravel → Execute → Shutdown
(Repeat for each request)
With Octane:
Bootstrap Laravel once → Keep in memory
Request → Execute → Response (no bootstrap!)
Request → Execute → Response
Installation
composer require laravel/octane
# With Swoole
php artisan octane:install --server=swoole
# With RoadRunner
php artisan octane:install --server=roadrunner
Install Swoole (recommended):
# macOS
pecl install swoole
# Ubuntu
sudo apt-get install php-swoole
Configuration
config/octane.php:
return [
'server' => env('OCTANE_SERVER', 'swoole'),
'swoole' => [
'options' => [
'log_level' => env('SWOOLE_LOG_LEVEL', 0),
'max_request' => env('SWOOLE_MAX_REQUESTS', 500),
'package_max_length' => env('SWOOLE_PACKAGE_MAX_LENGTH', 10 * 1024 * 1024),
],
],
'max_execution_time' => env('OCTANE_MAX_EXECUTION_TIME', 30),
'warm' => [
...config('app.providers'),
],
];
Running Octane
# Development
php artisan octane:start
# Production
php artisan octane:start --server=swoole --host=0.0.0.0 --port=8000 --workers=4
# With custom worker count
php artisan octane:start --workers=auto
# Watch for changes (development)
php artisan octane:start --watch
Performance Comparison
Benchmarks (Laravel application):
# PHP-FPM
Requests per second: ~500
# Laravel Octane (Swoole)
Requests per second: ~2,500
# 5x improvement
Memory Management
Important: Application stays in memory, avoid memory leaks:
// Bad: Accumulates in memory
class UserController
{
protected static $cache = [];
public function index()
{
self::$cache[] = User::all(); // Memory leak!
}
}
// Good: Use instance properties
class UserController
{
public function index()
{
$users = User::all(); // Cleared after request
return view('users.index', compact('users'));
}
}
Concurrent Tasks
use Laravel\Octane\Facades\Octane;
Route::get('/dashboard', function () {
[$users, $orders, $revenue] = Octane::concurrently([
fn () => User::count(),
fn () => Order::count(),
fn () => Order::sum('total'),
]);
return view('dashboard', compact('users', 'orders', 'revenue'));
});
Ticks and Intervals
// Run every second
Octane::tick('update-cache', fn () => Cache::put('time', now()))
->seconds(1);
// Run immediately, then every 10 seconds
Octane::tick('process-queue', fn () => ProcessQueuedJobs::dispatch())
->seconds(10)
->immediate();
Reloading Workers
# Reload workers (zero downtime)
php artisan octane:reload
# Stop server
php artisan octane:stop
Production Deployment
Supervisor configuration:
[program:octane]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/artisan octane:start --server=swoole --host=127.0.0.1 --port=8000 --workers=4
autostart=true
autorestart=true
user=forge
redirect_stderr=true
stdout_logfile=/path/to/octane.log
stopwaitsecs=3600
Nginx configuration:
location / {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
Best Practices
1. Avoid static properties:
// Bad
protected static $data = [];
// Good
protected $data = [];
2. Clear after each request:
// In service provider
Octane::tick('clear-cache', function () {
// Clear temporary data
})->immediate();
3. Use concurrent tasks:
[$data1, $data2] = Octane::concurrently([
fn () => $this->fetchData1(),
fn () => $this->fetchData2(),
]);
Monitoring
# Check worker status
php artisan octane:status
# View metrics
# Available in Octane dashboard
Conclusion
Laravel Octane dramatically improves performance by keeping application in memory. Perfect for high-traffic applications needing maximum speed.
Need performance optimization? Contact ZIRA Software for Octane implementation.