Web Development

Laravel Performance: How I Cut Response Time by 60%

January 02, 2026 β€’ 2 min read β€’ By Amey Lokare

🐌 The Problem

My Laravel API was slow. Average response time was 800ms. Users were complaining. I had to do something.

After optimization, response time is down to 320ms. That's a 60% improvement.

The result: 60% faster responses. Here's what I did.

πŸ“Š Before and After

Metric Before After Improvement
Average Response Time 800ms 320ms 60% faster
P95 Response Time 1,200ms 450ms 62% faster
Database Queries 15 per request 3 per request 80% reduction

βœ… Optimizations That Worked

1. Eager Loading

Fixed N+1 query problems:

// Before: N+1 queries
$posts = Post::all();
foreach ($posts as $post) {
    echo $post->author->name; // Query for each post
}

// After: Eager loading
$posts = Post::with('author')->get();
foreach ($posts as $post) {
    echo $post->author->name; // No additional queries
}

Impact: Reduced queries from 15 to 3 per request.

2. Query Caching

Cached expensive queries:

// Cache expensive queries
$users = Cache::remember('active_users', 3600, function () {
    return User::where('active', true)->get();
});

3. Response Caching

Cached entire responses for static data:

// Cache API responses
Route::get('/api/posts', function () {
    return Cache::remember('api_posts', 300, function () {
        return Post::with('author')->get();
    });
});

4. Database Indexing

Added indexes on frequently queried columns:

// Migration
Schema::table('posts', function (Blueprint $table) {
    $table->index('published_at');
    $table->index(['status', 'published_at']);
});

5. OPcache

Enabled OPcache for PHP:

; php.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000

6. Route Caching

Cached routes in production:

php artisan route:cache
php artisan config:cache
php artisan view:cache

πŸ“Š Performance Breakdown

Optimization Time Saved Impact
Eager Loading 300ms High
Query Caching 150ms High
Response Caching 100ms Medium
Database Indexing 50ms Medium

πŸ’‘ Key Takeaways

  • Eager loading fixes N+1 queries (biggest win)
  • Caching expensive queries saves time
  • Database indexes speed up queries
  • OPcache improves PHP performance
  • Route/config caching helps in production

Laravel performance optimization is about fixing the basics: eager loading, caching, and indexing. These simple changes made a huge difference.

Comments

Leave a Comment

Related Posts