Laravel interview question · Most asked

A Laravel endpoint is slow. How do you find and fix the cause?

Summary

Measure first: look at query count and time (Telescope, Debugbar, slow query log, APM), then fix the biggest cost, usually N+1 queries, missing indexes, too much data loaded, or slow external calls in the request.

Longer Answer

Typical findings, roughly in order of frequency:

  1. N+1 queries: eager load; enable preventLazyLoading in development.
  2. Missing indexes: run EXPLAIN on the slow queries; add composite indexes matching WHERE and ORDER BY.
  3. Loading too much: get() on large tables, SELECT *, hydrating thousands of models; paginate and select columns.
  4. Synchronous side effects: email, PDF generation, third-party APIs; queue them.
  5. Repeated expensive computation: cache with a TTL.
  6. Framework overhead: php artisan optimize, OPcache, Composer optimized autoloader; Octane if bootstrap cost dominates.

Verify each fix with the same measurement, and add a regression guard (query-count assertions in tests, APM alerts).

Key Points

  • Measure before changing anything.
  • N+1, indexes, data volume, synchronous I/O.
  • Cache and queue where appropriate.
  • Confirm with the same metric.

Deep dive: Code Clinic: review a Laravel orders controller

Explanation

What interviewers look for

A systematic, measurement-driven approach. Jumping straight to 'add Redis' or 'use Octane' is a red flag.

Follow-up questions

  • How would you assert the number of queries in a test?

More most-asked Laravel questions

← All Laravel questions