All posts

Taking one endpoint from 800ms to 80ms

Published June 27, 2026 · by Majd Ghithan, written with the help of AI

Taking one endpoint from 800ms to 80ms

The order-details endpoint took 800 milliseconds. Not broken, just slow enough that the app felt heavy everywhere it was used. The team's instinct was "the server needs more memory." My instinct was: I have no idea why it's slow, and neither does anyone guessing.

So before touching anything, I profiled it. An hour later it returned in about 80 milliseconds, and I hadn't added a single server. I'd just found where the 800ms was actually going — which was nowhere anyone expected.

Here's the walk, because the method matters more than this one endpoint.

First: measure, don't guess

You cannot optimize what you haven't measured. The whole failure mode of performance work is a smart engineer optimizing the wrong thing confidently. So step one is always to break the request into where its time actually goes.

I installed Laravel Telescope for this — the Requests tab shows you, per request, the number of queries, the time in each, and how long the whole thing took. Clockwork does the same in a browser panel if you prefer. For a one-off, even a DB::listen() that logs every query plus a couple of timers around suspicious blocks is enough.

Here's what the profile showed for that 800ms:

Where 800ms actually went
6101403515DB queries (94 of them)External API callSerializationFramework + PHP
ms

Two things jumped out immediately, and neither was the server. Ninety-four queries for a single order. And a synchronous HTTP call to a shipping API, sitting right in the middle of the request, adding 140ms every time.

The 94 queries: a classic N+1

Ninety-four queries to show one order is a textbook N+1. The controller loaded the order, then looped its line items, and each iteration lazily loaded the product, the product's category, and the tax rule. One order with ~30 line items, three lazy relations each, and there's your ninety-something queries.

The fix is eager loading — tell Eloquent to fetch the relations up front in a handful of queries instead of one-per-row:

// Before: 1 + (30 × 3) = 91 queries
$order = Order::findOrFail($id);

// After: 4 queries total, regardless of line-item count
$order = Order::with([
    'items.product.category',
    'items.taxRule',
])->findOrFail($id);

Ninety-four queries collapsed to four. That alone took the DB time from 610ms to about 45ms — the single biggest win, and it cost me one with() call.

The 140ms external call: get it out of the request

The shipping API call was fetching a live delivery estimate. Fine feature, wrong place. The user's request was blocking for 140ms waiting on a third party we don't control — and if that API had a bad day, our endpoint would too.

Two options, depending on the product need. If the estimate can be slightly stale, cache it and refresh in the background — the request reads a cached value in under a millisecond. If it must be live, move it out of the critical path: load the page without it and fetch the estimate via a separate async call, so the main content isn't held hostage to a slow dependency.

// Was: blocking 140ms on every request
$estimate = $this->shipping->liveEstimate($order);

// Now: cached, refreshed out-of-band
$estimate = Cache::remember(
    "ship:estimate:{$order->id}", 600,
    fn () => $this->shipping->liveEstimate($order)
);

Cached, that 140ms became effectively zero on all but the occasional refresh.

The result

Same endpoint, after profiling
80022080BeforeAfter eager loadAfter caching the API call
ms

Eight hundred milliseconds to eighty. No new hardware, no framework tricks, no rewrite. Just one with() and one remember(), both aimed precisely at the two things the profile said were expensive.

That's the whole point. The team was ready to spend money on servers to fix a problem that was 91 unnecessary queries and one misplaced API call. If they'd added RAM, the endpoint would've stayed slow and they'd have paid for it monthly. Profiling turned a vague "it's slow, buy more" into two specific, cheap, correct fixes.

Every slow endpoint has a profile like this — a small number of things eating almost all the time, and a long tail that doesn't matter. Your job isn't to make everything faster. It's to measure, find the one or two bars that are tall, and go fix exactly those.

🤖 Heads up: this post was drafted with AI. Spot something wrong or off?Edit it on GitHub & open a PR
Majd Ghithan

Majd Ghithan

Full-Stack Engineer & Tech Lead

More posts

Laravel finally resizes images for you

Laravel finally resizes images for you

Every Laravel app I've built that takes an avatar ended the same way: composer require intervention/image, wire up a facade, write a little service class, and hope the next dev…

July 26, 2026Read more
What actually breaks at 100,000 users (that never breaks in a demo)

What actually breaks at 100,000 users (that never breaks in a demo)

The first time I shipped a dashboard to a hundred thousand active users, nothing I had tested was what broke. Everything that failed had passed every check I ran. It worked on m…

July 25, 2026Read more
Where your queue jobs go to die under load

Where your queue jobs go to die under load

The support ticket said "I never got my invoice email." I checked the logs. The job ran. It succeeded. failedjobs was empty. Everything said the email went out. It did not.

July 18, 2026Read more
The indexes you're missing (and the one that's hurting you)

The indexes you're missing (and the one that's hurting you)

A client sent me a query that took 4.2 seconds. One WHERE, one ORDER BY, a table with three million rows. I added a single index and it dropped to 11 milliseconds. They asked if…

July 11, 2026Read more
Caching in Laravel without the stampede

Caching in Laravel without the stampede

We cached the homepage's "trending products" query for five minutes. It was our slowest query — about 900ms, a big aggregation across orders. Caching it took the homepage from s…

July 4, 2026Read more
The N+1 you can't see (it's hiding in your accessors)

The N+1 you can't see (it's hiding in your accessors)

I've fixed hundreds of N+1 queries. The easy ones are right there in the controller — a foreach with $order-customer inside it, obvious the moment you read the code. Those aren'…

June 20, 2026Read more
The Friday deploy that charged customers twice

The Friday deploy that charged customers twice

It was 4:40 on a Friday. The change was tiny — a one-line tweak to how we called the payment provider, plus a bump to the queue worker's timeout. Small, tested, reviewed. I depl…

June 13, 2026Read more
Code review people don't dread

Code review people don't dread

I once left forty-one comments on a junior's pull request. I was proud of it. Thorough, I told myself. The next day he barely made eye contact, and his next PR sat open for a we…

June 6, 2026Read more
Hiring a mid-level Laravel dev: the signals that actually matter

Hiring a mid-level Laravel dev: the signals that actually matter

The best hire I ever made failed my first question. I asked him to explain service containers and he stumbled, went quiet, then said "honestly I use them every day but I've neve…

May 30, 2026Read more
Saying no to a feature without being the 'no' person

Saying no to a feature without being the 'no' person

For about a year I was the engineer everyone learned to route around. Not because I was wrong, I was usually right, but because my answer to new ideas was a flat "no, that'll br…

May 23, 2026Read more
My first 90 days as a tech lead (and the habit I had to break)

My first 90 days as a tech lead (and the habit I had to break)

Three weeks into leading my first team, I stayed late to "help" by rewriting a junior's feature myself. It was faster my way. I pushed it, felt productive, went home. The next m…

May 16, 2026Read more
Breaking down the task that scares you

Breaking down the task that scares you

There's a specific kind of ticket that makes my stomach drop. Not the hard ones, hard is fine. It's the ambiguous ones. "Migrate billing to the new provider." "Add multi-tenancy…

May 9, 2026Read more
1:1s that aren't just status updates

1:1s that aren't just status updates

For my first few months as a lead, my 1:1s were thirty minutes of me asking "so, what are you working on?" and nodding at answers I already knew from standup. We both left sligh…

May 2, 2026Read more
Get the business logic out of your controllers

Get the business logic out of your controllers

I once opened a OrderController@store method that was 240 lines long. Validation, a discount calculation, three model writes, a Stripe charge, two emails, a Slack notification,…

April 25, 2026Read more
Form Requests are the most underrated thing in Laravel

Form Requests are the most underrated thing in Laravel

Most Laravel devs meet Form Requests once, in a tutorial, use them to hold a rules() array, and never look deeper. That's a shame, because a Form Request is the cleanest place i…

April 18, 2026Read more
Testing Laravel without mocking everything

Testing Laravel without mocking everything

I inherited a codebase once with 900 unit tests and no confidence. Every test mocked the repository, mocked the model, mocked the mailer, mocked the thing three layers down. The…

April 11, 2026Read more
Three Eloquent features that clean up your models

Three Eloquent features that clean up your models

The messiest Laravel model I ever wrote wasn't messy because of Eloquent. It was messy because I ignored the parts of Eloquent that exist specifically to keep it clean. The same…

April 4, 2026Read more
The migration that locked the table for eight minutes

The migration that locked the table for eight minutes

The deploy looked boring. One migration, adding a lastseenat column to the users table with a default of now(). I'd written a hundred like it. I ran it at 2pm on a Tuesday becau…

March 28, 2026Read more
Chasing a memory leak in a Laravel queue worker

Chasing a memory leak in a Laravel queue worker

The alert came in at 4am: one of our queue:work processes had been OOM-killed. The supervisor restarted it, it processed jobs for about forty minutes, and got killed again. It w…

March 21, 2026Read more