Laravel 11 with Octane and Reverb delivers exceptional performance for demanding applications. Proper optimization can reduce response times by 80%. At ZIRA Software, these techniques power our highest-traffic applications.
Laravel Octane Setup
composer require laravel/octane
php artisan octane:install
# Choose: Swoole (recommended) or RoadRunner
// config/octane.php
return [
'server' => env('OCTANE_SERVER', 'swoole'),
'https' => false,
'listeners' => [
WorkerStarting::class => [
EnsureUploadedFilesAreValid::class,
],
RequestReceived::class => [
EnsureUploadedFilesCanBeMoved::class,
],
],
'warm' => [
// Services to pre-resolve
\App\Services\PaymentService::class,
\App\Services\NotificationService::class,
],
'flush' => [
// Services to flush between requests
],
'tables' => [
// Swoole tables for shared memory
'cache' => [
'rows' => 1000,
'columns' => [
['name' => 'value', 'type' => 'string', 'size' => 10000],
['name' => 'expires_at', 'type' => 'int'],
],
],
],
'max_execution_time' => 30,
];
Octane with Supervisor
# /etc/supervisor/conf.d/octane.conf
[program:octane]
command=php /var/www/app/artisan octane:start --server=swoole --host=0.0.0.0 --port=8000 --workers=4 --task-workers=6 --max-requests=1000
directory=/var/www/app
autostart=true
autorestart=true
user=www-data
redirect_stderr=true
stdout_logfile=/var/www/app/storage/logs/octane.log
stopwaitsecs=30
Performance Comparison
Standard Laravel (FPM):
├── Requests/sec: ~500
├── Memory: ~50MB per worker
└── Latency: ~100ms
Laravel Octane (Swoole):
├── Requests/sec: ~5,000
├── Memory: ~200MB shared
└── Latency: ~10ms
Database Optimization
// Eager loading
$posts = Post::with(['author', 'comments.user', 'tags'])->get();
// Select only needed columns
$users = User::select('id', 'name', 'email')->get();
// Chunking for large datasets
User::chunk(1000, function ($users) {
foreach ($users as $user) {
// Process
}
});
// Use cursor for memory efficiency
foreach (User::cursor() as $user) {
// Process without loading all
}
// Database indexing
Schema::table('orders', function (Blueprint $table) {
$table->index(['user_id', 'created_at']);
$table->index(['status', 'created_at']);
});
Caching Strategies
// Route caching (production)
php artisan route:cache
php artisan config:cache
php artisan view:cache
php artisan event:cache
// Query caching
$popularPosts = Cache::remember('posts:popular', 3600, function () {
return Post::with('author')
->withCount('views')
->orderByDesc('views_count')
->take(10)
->get();
});
// Cache tags for easy invalidation
Cache::tags(['posts', 'homepage'])->put('featured', $posts, 3600);
Cache::tags(['posts'])->flush(); // Clear all post caches
// Model caching
class Post extends Model
{
protected static function booted()
{
static::saved(fn() => Cache::tags(['posts'])->flush());
static::deleted(fn() => Cache::tags(['posts'])->flush());
}
}
Queue Optimization
// config/queue.php - Redis queue
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => 'default',
'retry_after' => 90,
'block_for' => 5, // Long polling
],
// Horizon configuration
'environments' => [
'production' => [
'supervisor-1' => [
'connection' => 'redis',
'queue' => ['high', 'default', 'low'],
'balance' => 'auto',
'minProcesses' => 1,
'maxProcesses' => 10,
'balanceMaxShift' => 1,
'balanceCooldown' => 3,
'tries' => 3,
],
],
],
Reverb Performance
// config/reverb.php
'servers' => [
'reverb' => [
'host' => env('REVERB_HOST', '0.0.0.0'),
'port' => env('REVERB_PORT', 8080),
'options' => [
'tls' => [],
],
'max_request_size' => 10000,
'scaling' => [
'enabled' => true, // Redis pub/sub for horizontal scaling
'channel' => 'reverb',
],
],
],
// Connection handling
'apps' => [
'apps' => [[
'ping_interval' => 60,
'max_message_size' => 10000,
'activity_timeout' => 30, // Close inactive connections
]],
],
Response Optimization
// Compress responses
// nginx.conf
gzip on;
gzip_types application/json text/plain application/javascript;
// API pagination
public function index()
{
return PostResource::collection(
Post::cursorPaginate(20) // Cursor pagination for large datasets
);
}
// Lazy loading in resources
class PostResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'author' => new UserResource($this->whenLoaded('author')),
];
}
}
Production Deployment
#!/bin/bash
# deploy.sh
php artisan down
git pull origin main
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache
php artisan octane:reload
php artisan queue:restart
php artisan up
Monitoring
// Laravel Pulse for performance monitoring
// Tracks: slow queries, requests, jobs, exceptions
// Custom metrics
Pulse::record('api_calls', 'stripe', $responseTime)->avg();
// Telescope for development
// Disable in production for performance
Conclusion
Laravel 11 with Octane and Reverb achieves exceptional performance through application servers, caching, and proper optimization. These techniques handle thousands of requests per second efficiently.
Need performance optimization? Contact ZIRA Software for Laravel performance consulting.