Laravel Octane High Performance
Introduction
Laravel Octane supercharges your application's performance by serving it using persistent application servers like Swoole and RoadRunner. This eliminates the overhead of bootstrapping Laravel for each request.
Installation
Install Octane via Composer:
composer require laravel/octane
Install the Swoole extension:
pecl install swoole
Publish configuration:
php artisan octane:install
Swoole Server
Start the Octane server with Swoole:
php artisan octane:start --server=swoole
Configure Swoole in octane.php:
return [
'server' => [
'driver' => 'swoole',
'count' => 8,
'options' => [
'task_worker_count' => 4,
'max_request' => 10000,
],
],
];
RoadRunner Server
Install RoadRunner binary:
php artisan octane:download
Start with RoadRunner:
php artisan octane:start --server=roadrunner
Octane Tables
Octane provides in-memory tables for caching:
use Laravel\Octane\Facades\Octane;
Octane::table('users')->set('user:1', [
'name' => 'John Doe',
'email' => 'john@example.com'
]);
$user = Octane::table('users')->get('user:1');
Concurrent Tasks
Execute tasks concurrently:
use Laravel\Octane\Facades\Octane;
$responses = await Octane\concurrent([
fn() => Http::get('https://api.github.com/users/octane'),
fn() => Http::get('https://api.github.com/users/laravel'),
fn() => Http::get('https://api.github.com/users/taylor'),
]);
Optimization Tips
- Use prepared statements for database queries
- Leverage Octane tables for frequently accessed data
- Cache configuration and routes
- Use eager loading to avoid N+1 queries
- Enable opcache for PHP
- Use concurrent requests for parallel operations
Summary
Laravel Octane provides excellent performance improvements for Laravel applications. By using persistent application servers like Swoole or RoadRunner, you can dramatically reduce request latency and handle more concurrent users.
For more information, check out our other tutorials on Laravel Vapor and Laravel Forge.