Laravel / PerformanceJune 20266 min read

Fixing Slow Laravel Apps: What I Actually Do Before Blaming the Server

Every developer has been there: a project runs blazing fast locally with 20 mock records, but once you seed 10,000 rows or push to production, pages take 4 seconds to load. My first instinct years ago was to upgrade the VPS. Now, I look straight at the database tab in Laravel Debugbar and start fixing queries.

#Laravel#PHP#MySQL#Performance#Redis

01. The hidden trap of Eloquent lazy loading

Eloquent makes relationships so simple to write that you do not notice you are firing hundreds of database calls inside a single foreach loop. This classic N+1 problem is almost always the main culprit behind slow response times.

Eager loading with `with()` solves this immediately, but you also need to stop fetching columns you do not need. Pulling entire text columns or large JSON blobs across 50 records adds huge memory overhead on the PHP worker.

  • Always eager load relations using `with()` on list pages
  • Select only the necessary columns instead of `select *`
  • Use `Model::preventLazyLoading(!app()->isProduction())` in local dev
  • Paginate everything—never dump full collections into views

02. Where caching actually makes sense

Caching is not a magic fix for bad schema design, but it works wonders for static lookups. Navigation menus, site settings, categorized dropdowns, and dashboard counts that do not change every second should never hit MySQL repeatedly on every request.

I use Redis as the cache driver and wrap expensive aggregations in `Cache::remember()`. The trick is keeping your cache keys predictable so you can invalidate them via model observers when data updates.

03. Queues keep the request-response cycle fast

If a user clicks 'Complete Order' and has to wait while your server compiles an invoice PDF, connects to an SMTP server to send an email, and updates external stock via an API, they will think your site is frozen.

Pushing notifications, heavy exports, and third-party API calls to database or Redis queues makes the UI respond instantly while the worker handles the heavy lifting in the background.

Performance tuning in Laravel is rarely about micro-optimizing PHP loops. If you fix your queries, cache static data, and queue external work, 90% of your performance bottlenecks disappear.