Most optimization guides stop at route caching and eager loading. In real-world applications handling thousands of requests per minute, deeper optimizations are required.
Use Database Indexes Strategically
Many developers focus on Laravel code while ignoring database performance.
For example:
CREATE INDEX idx_users_email ON users(email);
Without an index, PostgreSQL may scan millions of rows.
Check query execution:
EXPLAIN ANALYZE
SELECT * FROM users
WHERE email = 'user@example.com';
A properly indexed query can execute hundreds of times faster.
Avoid Loading Unnecessary Columns
Instead of:
$users = User::all();
Use:
$users = User::select('id', 'name', 'email')->get();
Reducing selected columns lowers memory usage and network overhead.
Use Cursor Pagination for Large Datasets
Traditional pagination becomes slower on large tables.
Instead of:
User::paginate(20);
Use:
User::cursorPaginate(20);
Cursor pagination is significantly faster for datasets containing hundreds of thousands of records.
Cache Expensive Aggregations
Consider a dashboard showing:
Total Orders
Total Revenue
Active Customers
Running aggregate queries on every request is wasteful.
Cache::remember(
'dashboard_stats',
now()->addMinutes(30),
fn() => [
'orders' => Order::count(),
'revenue' => Order::sum('amount'),
]
);
This can reduce database load by more than 90%.
Use Laravel Octane
Laravel Octane keeps the application in memory using Swoole or RoadRunner.
Benefits:
Faster request handling
Lower bootstrap overhead
Better CPU utilization
Many teams report 2x–5x performance improvements after adopting Octane.
Move Heavy Work to Queues
Bad:
public function register(Request $request)
{
$user = User::create(...);
Mail::to($user)->send(new WelcomeMail());
ProcessUserAnalytics::run($user);
return response()->json();
}
Good:
SendWelcomeMail::dispatch($user);
ProcessUserAnalytics::dispatch($user);
Users receive instant responses while background jobs handle processing.
Optimize N+1 Problems Automatically
Install Laravel Telescope:
composer require laravel/telescope
Telescope reveals hidden N+1 queries before they reach production.
Reduce API Response Size
Instead of returning entire models:
return User::all();
Use API Resources:
return UserResource::collection($users);
Smaller JSON responses improve mobile performance and reduce bandwidth costs.
Monitor Slow Queries in Production
A query taking 500ms may seem harmless.
At 1,000 requests per minute:
500ms × 1000 requests
Hundreds of wasted CPU seconds every minute
Enable slow-query logging and review execution plans regularly.
Real Production Results
On a recent Laravel + PostgreSQL project, implementing:
Redis caching
Query optimization
Database indexing
Queue workers
Laravel Octane
Reduced average response time from 820ms to 120ms and lowered database CPU utilization by over 70%.
Performance optimization is one of the highest ROI activities for any Laravel application because every improvement affects every user request.
For more Laravel, PostgreSQL, and backend scaling insights, visit:
Top comments (0)