Our services
If you can think it, we can make it brainsoft.
If you can think it, we can make it brainsoft.
Written By: BrainSoft In Backend
Laravel is fast enough for almost anything until it suddenly is not. When a page that felt instant starts taking two seconds, the cause is usually one of a short list of familiar problems. Here is where to look, roughly in the order that returns the most for the least effort.
Install a request profiler such as Laravel Debugbar, Clockwork or Telescope in your local environment and open the slow page. If you see dozens or hundreds of near-identical queries, you have an N+1 problem: a relationship loaded once per row instead of once per request. Eager loading with with() usually fixes it in a line. This single issue accounts for a large share of "the app got slow" reports.
A query that filters or sorts on an unindexed column is fine with a thousand rows and painful with a million. Run EXPLAIN on the queries your profiler flags as slow, and add indexes for the columns you filter, join and order by. Composite indexes matter when you filter on several columns at once.
Sending mail, calling third-party APIs, generating PDFs and processing uploads do not belong in the request-response cycle. Push them onto a queue and let a worker handle them. The user gets their response immediately, and a slow external service stops being your slow page. Check that the queue runs on a real driver and not sync.
Some results are costly to compute and change rarely: a navigation tree, a dashboard total, a rendered marketing page. Cache those with a sensible expiry and a clear rule for clearing them. Do not reach for caching to paper over an N+1 or a missing index. Fix the query, then cache what is still slow.
Once the application code is in order, the platform is worth a look. Enable and size OPcache. Run config:cache, route:cache and view:cache as part of the deploy. Give PHP-FPM enough workers for your traffic and not so many that they exhaust memory. These help, but only after the queries are sensible. Runtime tuning cannot rescue a page that runs 300 queries.
Install a profiler such as Telescope or Debugbar and count the queries on the slow page. Dozens of near-identical queries is the signature of an N+1.
It can hide the symptom, but if the underlying query is slow the page will be slow again once the cache expires. Fix the query first, then cache what is still expensive.
Yes, with the standard toolkit: queues, proper indexing, OPcache and horizontal scaling behind a load balancer. The framework itself is rarely the actual bottleneck.
If a Laravel app you rely on has gotten slow and you cannot find the cause, our web development team can profile it — reach out.