Laravel performance checklist: 8 fixes before you scale
A Laravel app usually feels fast until it has real data. Then a page that ran 12 queries runs 400, a report times out, and someone suggests a bigger server. A bigger server rarely fixes it. Most slow Laravel apps are slow for the same handful of reasons, and each one is cheap to fix once you know where to look.
This is the checklist our team works through before anyone talks about scaling out. Code examples follow the Laravel 13 docs and work on recent versions unless noted.
1. Find out where the time goes
Guessing wastes days. Laravel can tell you when one request spends too long in the database. Put this in the boot method of AppServiceProvider:
use Illuminate\Database\Connection;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
DB::whenQueryingForLongerThan(500, function (Connection $connection, QueryExecuted $event) {
Log::warning('Slow DB time in one request', ['url' => request()->fullUrl()]);
});
The threshold (500 ms here) is the total query time for the request, not a single query. A week of these log lines gives you a short list of pages to fix first. Telescope or Pulse can show the same thing in more detail if you already run them.
2. Kill N+1 queries, and make them fail loudly
The most common problem by far: a loop that touches a relationship on every item, so 50 posts become 51 queries. Eager load what the page uses, and count related rows without loading them:
$posts = Post::with('author:id,name')
->withCount('comments')
->latest()
->paginate(20);
// $post->author->name and $post->comments_count need no extra queries
Then stop new ones from sneaking in. Laravel can throw an exception when code lazy loads a relationship. Turn it on outside production so it breaks tests and local pages, not customer requests:
use Illuminate\Database\Eloquent\Model;
public function boot(): void
{
Model::preventLazyLoading(! $this->app->isProduction());
}
The Eloquent relationships docs also show how to log violations instead of throwing, which is a gentler way to start on a large codebase.
3. Index the columns you filter and sort by
Eager loading cuts the number of queries. Indexes make each query cheap. If a hot query filters by account_id and sorts by created_at, one composite index covers both:
Schema::table('orders', function (Blueprint $table) {
$table->index(['account_id', 'created_at']);
});
Column order matters: put the column you filter on with = first, then the one you sort or range on. Run EXPLAIN on the slow query before and after. If the plan still shows a full table scan, the index doesn't match the query.
4. Process big tables in chunks
Order::all() on a large table loads every row into memory at once. Jobs and commands that walk through big tables should take them in pieces:
Invoice::where('status', 'pending')
->lazyById(500)
->each(fn (Invoice $invoice) => $invoice->sendReminder());
Use chunkById or lazyById rather than plain chunk when you update rows while you loop. They page by primary key, so updated rows don't shift the pages and get skipped. If you need eager loading inside the loop, prefer lazy over cursor: the docs note cursor can't eager load relationships.
5. Move slow work to a queue
Sending email, building PDFs, calling a slow third-party API: none of it needs to happen while the user waits. Put it in a job:
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
class GenerateInvoicePdf implements ShouldQueue
{
use Queueable;
public function __construct(public Invoice $invoice) {}
public function handle(): void
{
// build the PDF and store it
}
}
GenerateInvoicePdf::dispatch($invoice);
Two things catch teams out. First, the sync queue driver runs jobs immediately, inside the request, so check that QUEUE_CONNECTION isn't sync in production. Second, queue workers are long-lived processes that don't see new code. Run php artisan queue:restart on every deploy, with Supervisor (or similar) bringing the workers back up. Set retries and a timeout on each job too: Laravel 13 uses the #[Tries] and #[Timeout] attributes, and Laravel 12 uses $tries and $timeout properties.
6. Cache the expensive reads
Dashboards, settings, menus and counts often get rebuilt on every request even though they change rarely. Cache::remember stores the result for a set time. Cache::flexible goes a step further and serves the slightly old value while it refreshes in the background, so no user waits for the rebuild:
$stats = Cache::flexible('dashboard:stats', [300, 600], function () {
return $this->buildDashboardStats();
});
Here the value is fresh for 5 minutes. Between 5 and 10 minutes, users get the cached copy while Laravel recalculates it after the response is sent. When the underlying data changes, call Cache::forget('dashboard:stats') so nobody sees stale numbers longer than they should.
7. Take cache, sessions and queues off the database
New Laravel apps use the database driver for cache, sessions and queues by default. That's a sensible start, but under load it means your database also handles session reads and writes, cache lookups and queue polling on top of your real queries. When traffic grows, move them to Redis:
CACHE_STORE=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
You'll need the PhpRedis extension or the predis/predis package. Move one driver at a time and watch the database load drop after each change.
8. Deploy like it's production
A few settings make a real difference on every request:
- Run
php artisan optimizein your deploy script. It caches config, events, routes and views (see Laravel's deployment docs). Once config is cached,.envis no longer loaded, soenv()calls outside theconfig/files returnnull. Move them into config first. - Keep
APP_DEBUG=falsein production. It's a security setting as much as a speed one. - Make sure OPcache is on, and check its limits. The defaults are 128 MB of memory and 10,000 cached files. Count your PHP files, vendor folder included, and raise
opcache.max_accelerated_filesif you're close. If you setopcache.validate_timestamps=0for speed, OPcache stops checking for changed files, so you must reload PHP-FPM on each deploy. The OPcache settings on php.net explain each option.
A short checklist
- Slow-request logging is on and someone reads it
- Lazy loading throws outside production; hot pages use
with()andwithCount() - Hot queries have matching indexes, confirmed with
EXPLAIN - Large tables are processed with
lazyByIdorchunkById - Slow work runs in queued jobs, and workers restart on deploy
- Expensive reads are cached and cleared when the data changes
- Cache, sessions and queues run on Redis once traffic justifies it
optimizeruns on deploy, debug is off, OPcache is sized for the app
If you're on Laravel 12, plan the move to 13 alongside this work. Laravel 12 stopped getting bug fixes on 13 August 2026 and gets security fixes until 24 February 2027. Laravel 13 needs PHP 8.3 or newer.
We start with this list whenever we pick up a Laravel app that is struggling. If you need more hands for it, we have Laravel developers who can join your team.
Disclosure: I work at EchoInnovate IT, a software development company with teams in the USA and India.
Which of these has bitten you hardest? I'd like to hear about it in the comments.
About the author
A Leading Android & iPhone App Development Company, Their responsibility is to deliver an outstanding user experience providing an exceptional and intuitive application design. designing ideas using various methods and the latest technology.
Comments
Join the discussionNo comments yet. Be the first to comment.