Start free trial
Start free trial Log in

14-day free trial. No credit card.

How to find slow endpoints in Laravel production

From Nginx access logs to full per-request tracing: four layers, and how to choose.

  • Guide
  • 3 min read

How do I find the slowest endpoints in a Laravel app?

Enable request-time logging in your web server as a baseline, add a Laravel middleware that records controller action and duration, then install an APM that aggregates by route pattern with p95 trending. The critical move is grouping by route pattern (like /orders/{id}) rather than raw URL, and tracking p95 not average. NightOwl and Laravel Nightwatch Cloud do this aggregation automatically.

Updated

"The app feels slow" is where most performance work starts, and you can't debug it as stated. The first job isn't fixing anything. It's turning that complaint into a list: which endpoints are slow, for how many requests, and by how much. Until you have that list you're optimizing by guesswork, and the route you assume is slow is often fine while the real problem sits on a route you rarely look at. The four layers below build that list, from free web server logs to per-request traces.

Layer 1: Web server access logs

Nginx and Apache can record request duration. Free, always-on, zero Laravel code.

nginx.conf

nginx
log_format timing '$remote_addr - [$time_iso8601] '
                  '"$request" $status $body_bytes_sent '
                  'rt=$request_time uct="$upstream_connect_time" '
                  'uht="$upstream_header_time" urt="$upstream_response_time"';

access_log /var/log/nginx/access.log timing;

Sort the log by $request_time to find slow requests. What you're missing: controller name, user context, query breakdown, per-route aggregation. Fine for a quick baseline, useless for root cause.

Layer 2: Middleware timing

A Laravel middleware records start/end time and the resolved route. You get controller-aware timing without a full APM.

app/Http/Middleware/MeasureRequestTime.php

php
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;

class MeasureRequestTime
{
    public function handle(Request $request, Closure $next): Response
    {
        $start = microtime(true);
        $response = $next($request);
        $duration = (microtime(true) - $start) * 1000;

        if ($duration > 500) {
            Log::warning('Slow request', [
                'route' => $request->route()?->uri(),
                'controller' => $request->route()?->getActionName(),
                'method' => $request->method(),
                'duration_ms' => round($duration, 2),
                'status' => $response->getStatusCode(),
                'user_id' => auth()->id(),
            ]);
        }

        return $response;
    }
}

Register it globally in bootstrap/app.php with $middleware->append(MeasureRequestTime::class) inside withMiddleware(). Good for logging outliers. Missing: aggregation. A single slow request doesn't tell you much. You want to know which route pattern is consistently slow at p95.

Layer 3: Route aggregation with p95

The real unlock is aggregating by normalized route pattern. One record per request, grouped by route?->uri(), with p95 and p99 percentiles computed over a time window.

Metrics to surface per route:

  • Request count
  • Error rate (5xx %)
  • p50 / p95 / p99 duration
  • Throughput (requests/min)
  • Average DB time / external HTTP time per request (requires instrumentation layer)

Layer 4: Per-request trace correlation

Once you've identified /orders/{id} is slow at p95, the next question is why. You need per-request traces that break down the slow request into its component spans: DB queries, cache calls, external HTTP, view rendering.

The Laravel Nightwatch package records every span per request with a shared trace ID. NightOwl and Nightwatch Cloud both consume this data, so you can drill from the slow route to one of its slow requests to the query that made it slow. In NightOwl the request timeline also colours repeated SQL and lists it on a Duplicates tab, which is where an N+1 shows up.

Fix priorities: what to tackle first

  1. High-traffic routes with high p95: biggest total pain. Often N+1s or missing indexes.
  2. Low-traffic routes with catastrophic tail: /api/export, /admin/report. Users don't hit them often, but when they do they hurt.
  3. Routes trending slower over time: data growth outpacing indexes. Profile the plan.
  4. Routes blocked on external HTTP: move to async/queued work. See the outgoing HTTP guide.
  5. Routes with high error rate AND high p95: failing slowly, worst possible combination.

See which routes are slow in production

NightOwl groups requests by route, with Avg and P95 on the list and P99 on the route page. Filter a route to its requests at or above P95, then open one to see every query, cache call and HTTP call it made.

No credit card needed.

Terminal
composer require nightowl/agent
php artisan nightowl:install
php artisan nightowl:agent

Frequently asked questions

Anything else, write to support@usenightowl.com

How do I find the slowest routes in a Laravel app?

Three progressive options. (1) Enable access logs with request timing in your web server (Nginx's $request_time or Apache's %D) and grep for slow ones. Free, coarse. (2) Use a middleware that records controller action + duration to your database or log channel. This gives you per-route aggregation with a bit of code. (3) Install an APM that records every request with route, controller, duration, and status, grouped by pattern. This is what NightOwl and Laravel Nightwatch Cloud do. Grouping by route pattern (not raw URL) is the non-obvious bit.

What's the difference between a slow request and a slow endpoint?

A slow request is a single event: /api/orders/742 took 2.3s. A slow endpoint is an aggregate pattern: the /api/orders/{id} route has a p95 of 1.8s over the last 24 hours. Request-level tells you what happened; endpoint-level tells you what's broken. You need aggregation by route pattern, grouping /api/orders/742 and /api/orders/891 together.

Should I measure average or p95 latency for Laravel endpoints?

P95, always. Average is misleading. A route where 90% of requests take 80ms and 10% take 1.5s averages about 220ms, which looks fine, while its p95 is 1.5s. P95 (95th percentile) tells you 'the slowest 5% of requests take at least this long', which is closer to what users actually perceive. P99 is stricter; useful for SLO tracking.

How do I monitor Laravel endpoint performance in production without affecting performance?

The instrumentation needs to be out-of-process and async. Writing timing data to your primary database from a middleware adds latency on every request. Writing to a separate log or a local socket that a worker reads is cheap. Laravel's nightwatch package sends its data to an out-of-process TCP agent for exactly this reason, which keeps the added overhead under 3ms per request.

How can I correlate slow endpoints to slow database queries?

You need per-request trace context linking HTTP requests to DB queries. A middleware generates a trace ID; the query listener records it alongside SQL. When you drill into a slow /api/orders request, you see every query it fired. Laravel Telescope does this locally; NightOwl and Nightwatch Cloud do it in production with aggregation across traffic.

What counts as a 'slow' Laravel endpoint?

Depends on the endpoint. An API call a UI waits on at 500ms is slow, a full page render at 500ms is fine, a background export at 500ms is excellent. As rough defaults: p95 under 200ms for API endpoints that block a UI, under 500ms for full page renders and write endpoints, and seconds are fine for work that doesn't block a user. Set per-endpoint targets rather than a global threshold.

Do Nginx/Apache access logs help find slow endpoints?

They help (they're cheap and always on), but they miss context. You see that /orders/742 took 2s but not why. No controller name, no query breakdown, no user context. Use them as a baseline or for non-Laravel traffic (static assets, redirects), then layer APM on top for actionable drilling.

What are the most common causes of slow Laravel endpoints?

The usual suspects: N+1 queries, missing database indexes, synchronous external API calls in the request path, full-table scans on growing tables, and unbounded eager loads (with() everything). Less common but dramatic: cache misses on hot paths, over-eager Blade partials, and serialization of huge Eloquent collections.

Related

Flat pricing, whatever your traffic does

Every plan starts with a 14-day free trial. No credit card needed.

Hobby
$5 a month

One app for a side project. Issues and alerts, with a 14-day dashboard window.

Start my 14-day trial
TeamRecommended
$15 a month

Up to 3 apps and 5 people per team. Full history, the MCP server, data clearing and agent health.

Start my 14-day trial
Agency
$69 a month

Unlimited client apps and people, one overview of every app, and apps you can hand over.

Start my 14-day trial