<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Olamilekan Lamidi - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Olamilekan Lamidi - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Fri, 12 Jun 2026 05:19:01 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/olamilekanlamidi/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Scale Laravel Applications for High-Traffic Production Systems ]]>
                </title>
                <description>
                    <![CDATA[ Your first scaling problem rarely arrives with a bang. For a while, everything is fine: pages load fast, the database barely breaks a sweat, and the team ships features without thinking much about inf ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-scale-laravel-applications-for-high-traffic-production-systems/</link>
                <guid isPermaLink="false">6a2b48a3a381db4fd3f61555</guid>
                
                    <category>
                        <![CDATA[ Laravel ]]>
                    </category>
                
                    <category>
                        <![CDATA[ scaling ]]>
                    </category>
                
                    <category>
                        <![CDATA[ production ]]>
                    </category>
                
                    <category>
                        <![CDATA[ web performance ]]>
                    </category>
                
                    <category>
                        <![CDATA[ performance ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Olamilekan Lamidi ]]>
                </dc:creator>
                <pubDate>Thu, 11 Jun 2026 23:45:39 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/uploads/covers/5e1e335a7a1d3fcc59028c64/8882176c-0420-4fc9-8d72-129640aac231.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Your first scaling problem rarely arrives with a bang. For a while, everything is fine: pages load fast, the database barely breaks a sweat, and the team ships features without thinking much about infrastructure.</p>
<p>Then traffic climbs. A campaign over-performs. A marketplace onboards a popular seller. A SaaS product signs a couple of enterprise accounts.</p>
<p>Suddenly, <code>/dashboard</code> takes two seconds instead of 300 milliseconds. Queue jobs that used to clear in seconds sit waiting for minutes. You have database CPU spikes every afternoon.</p>
<p>So you add another app server, and response time barely moves because the real culprit was a slow query on a large table all along.</p>
<p>If you have run Laravel in production, you've probably lived some version of this. The good news is that scaling Laravel almost never means abandoning the framework. It means learning where pressure builds and making the application behave predictably under load.</p>
<p>In this guide, you'll learn how to find common bottlenecks, tune the database, use Redis effectively, move slow work onto queues, optimize APIs, and monitor a Laravel application in production.</p>
<p>None of this requires a single heroic rewrite. The biggest wins usually come from practical work: removing inefficient queries, pushing slow tasks onto queues, adding the right indexes, caching carefully chosen data, and measuring whether each change actually helped.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>You'll get the most out of this guide if you're already comfortable with:</p>
<ul>
<li><p>Building applications with Laravel and PHP</p>
</li>
<li><p>Writing Eloquent queries and database migrations</p>
</li>
<li><p>Using queues, jobs, and scheduled commands</p>
</li>
<li><p>Reading a basic database query plan</p>
</li>
<li><p>Deploying Laravel to a production server or platform</p>
</li>
<li><p>Working with Redis and either MySQL or PostgreSQL in a production-like setup</p>
</li>
</ul>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a href="#heading-what-happens-when-laravel-apps-start-growing">What Happens When Laravel Apps Start Growing</a></p>
</li>
<li><p><a href="#heading-common-laravel-bottlenecks">Common Laravel Bottlenecks</a></p>
</li>
<li><p><a href="#heading-how-to-optimize-the-database">How to Optimize the Database</a></p>
</li>
<li><p><a href="#heading-how-to-scale-with-redis">How to Scale with Redis</a></p>
</li>
<li><p><a href="#heading-how-to-use-queue-driven-architectures">How to Use Queue-Driven Architectures</a></p>
</li>
<li><p><a href="#heading-how-to-optimize-api-performance">How to Optimize API Performance</a></p>
</li>
<li><p><a href="#heading-how-to-monitor-laravel-in-production">How to Monitor Laravel in Production</a></p>
</li>
<li><p><a href="#heading-an-example-high-traffic-laravel-architecture">An Example High-Traffic Laravel Architecture</a></p>
</li>
<li><p><a href="#heading-lessons-learned-the-hard-way">Lessons Learned the Hard Way</a></p>
</li>
<li><p><a href="#heading-a-pre-launch-scaling-checklist">A Pre-Launch Scaling Checklist</a></p>
</li>
<li><p><a href="#heading-conclusion">Conclusion</a></p>
</li>
<li><p><a href="#heading-references">References</a></p>
</li>
</ul>
<h2 id="heading-what-happens-when-laravel-apps-start-growing">What Happens When Laravel Apps Start Growing</h2>
<p>Traffic changes a system's behavior because it turns small inefficiencies into permanent costs. A query that takes 80 milliseconds is harmless when it runs a few hundred times an hour. Run it 30 times per page view on a page that gets thousands of hits a minute, and that same query becomes a capacity problem.</p>
<p>The pressure tends to show up in predictable places. More requests mean more PHP workers, more database connections, more queue volume, and more Redis operations.</p>
<p>The database, whether MySQL or PostgreSQL, is usually the first thing to buckle. Queues back up when work is created faster than workers can drain it. Caches only help when hit rates stay high and misses stay controlled. And scaling everything horizontally can turn sloppy code into an expensive cloud bill.</p>
<p>That's why scaling work has to start with measurement, not guesswork. Before you change anything, you want to know what is actually saturated: request CPU, database I/O, lock contention, Redis latency, queue depth, an external API, or oversized payloads.</p>
<p>A typical request in a growing Laravel app travels through several layers. The user sends a request, a load balancer routes it to an app server, and Laravel checks Redis for a cached result. On a miss, it queries the database, stores the computed result back in Redis, and hands any slow follow-up work to a queue. A worker picks up that job later while Laravel returns the response right away.</p>
<p>Here's the important part: adding more app servers does nothing for a slow query, a missing index, or an overloaded queue. Horizontal scaling only pays off once the shared dependencies behind those servers can keep up.</p>
<h2 id="heading-common-laravel-bottlenecks">Common Laravel Bottlenecks</h2>
<p>Laravel itself causes very few scaling problems. Most issues come from how application code talks to the database, the network, and background workers.</p>
<h3 id="heading-n1-queries">N+1 Queries</h3>
<p>The classic offender is the N+1 query. You load a list of models, then lazily touch a relationship on each one:</p>
<pre><code class="language-php">use App\Models\Post;

$posts = Post::latest()-&gt;take(50)-&gt;get();

foreach (\(posts as \)post) {
    echo $post-&gt;author-&gt;name;
}
</code></pre>
<p>That's one query for the posts plus one query per author: 51 queries for a single page. Eager load the relationship instead:</p>
<pre><code class="language-php">use App\Models\Post;

$posts = Post::with('author')
    -&gt;latest()
    -&gt;take(50)
    -&gt;get();

foreach (\(posts as \)post) {
    echo $post-&gt;author-&gt;name;
}
</code></pre>
<p>In production, these are sneaky. They often hide inside API Resources, Blade components, and authorization checks, where the relationship access isn't obvious from the controller.</p>
<h3 id="heading-missing-indexes">Missing Indexes</h3>
<p>Adding an index is one of the highest-return fixes you can make. Take a query like this:</p>
<pre><code class="language-php">\(orders = Order::where('account_id', \)accountId)
    -&gt;where('status', 'paid')
    -&gt;whereBetween('created_at', [\(start, \)end])
    -&gt;latest()
    -&gt;paginate(50);
</code></pre>
<p>If <code>orders</code> has millions of rows and no useful compound index, the database scans far more rows than it needs to. Add an index that matches how you actually query:</p>
<pre><code class="language-php">use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    public function up(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $table-&gt;index(['account_id', 'status', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $table-&gt;dropIndex(['account_id', 'status', 'created_at']);
        });
    }
};
</code></pre>
<p>Indexes aren't free, though. They take up space and slow down writes. Add them for real, repeated query patterns, not for every column that ever appears in a <code>where</code> clause.</p>
<h3 id="heading-inefficient-eager-loading">Inefficient Eager Loading</h3>
<p>You can also swing too far the other way. Loading every relationship "just in case" burns memory and ships data the request never uses:</p>
<pre><code class="language-php">$users = User::with([
    'profile',
    'teams',
    'roles.permissions',
    'invoices.lineItems.product',
])-&gt;get();
</code></pre>
<p>That might be fine for an admin detail page showing one user. On a list page, it's a liability. Constrain the eager loads and select only the columns you need:</p>
<pre><code class="language-php">$users = User::query()
    -&gt;select(['id', 'name', 'email'])
    -&gt;with([
        'profile:id,user_id,avatar_url',
        'teams:id,name',
    ])
    -&gt;latest()
    -&gt;paginate(25);
</code></pre>
<p>One caveat: tightly scoped select lists can break later code that expects a column you didn't load. Keep this technique close to read-heavy endpoints where the payoff is obvious.</p>
<h3 id="heading-synchronous-processing">Synchronous Processing</h3>
<p>High-traffic apps need short web requests. Sending email, generating PDFs, calling third-party APIs, resizing images, and building exports usually belong outside the request cycle. This version can hurt you:</p>
<pre><code class="language-php">public function store(Request $request)
{
    \(order = Order::create(\)request-&gt;validated());

    Mail::to(\(order-&gt;user)-&gt;send(new OrderReceipt(\)order));

    return response()-&gt;json($order, 201);
}
</code></pre>
<p>Push the work onto a queue instead:</p>
<pre><code class="language-php">public function store(StoreOrderRequest $request)
{
    \(order = Order::create(\)request-&gt;validated());

    SendOrderReceipt::dispatch($order-&gt;id);

    return response()-&gt;json([
        'id' =&gt; $order-&gt;id,
        'status' =&gt; 'accepted',
    ], 202);
}
</code></pre>
<p>Now your response time no longer depends on your mail provider. If the provider has a slow afternoon, the queue absorbs it and your users don't have to wait.</p>
<h3 id="heading-large-payloads">Large Payloads</h3>
<p>Oversized JSON responses hurt everyone in the chain: the app server serializing them, the network carrying them, and the client parsing them. A frequent mistake is returning whole models when you meant to return a summary:</p>
<pre><code class="language-php">return User::with('orders', 'invoices', 'teams')-&gt;findOrFail($id);
</code></pre>
<p>Define an explicit API Resource instead:</p>
<pre><code class="language-php">use Illuminate\Http\Resources\Json\JsonResource;

class UserSummaryResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'id' =&gt; $this-&gt;id,
            'name' =&gt; $this-&gt;name,
            'avatar_url' =&gt; $this-&gt;profile?-&gt;avatar_url,
            'plan' =&gt; $this-&gt;subscription_plan,
        ];
    }
}
</code></pre>
<p>A small, deliberate response contract keeps endpoint cost easy to reason about and prevents accidental coupling.</p>
<h3 id="heading-expensive-joins">Expensive Joins</h3>
<p>Joins are useful, but expensive joins across large tables can dominate your database time, especially when they sort or filter on columns that aren't indexed:</p>
<pre><code class="language-php">$rows = DB::table('orders')
    -&gt;join('users', 'users.id', '=', 'orders.user_id')
    -&gt;join('accounts', 'accounts.id', '=', 'users.account_id')
    -&gt;where('accounts.region', 'us-east')
    -&gt;where('orders.status', 'paid')
    -&gt;orderByDesc('orders.created_at')
    -&gt;limit(100)
    -&gt;get();
</code></pre>
<p>At scale, you may need to denormalize a small field, precompute a reporting table, or move analytics off the primary transactional database entirely. Do not treat denormalization as an admission of defeat. Copying a stable field like <code>account_id</code> onto <code>orders</code> can remove a costly join from a hot path. The price you pay is keeping that duplicated data consistent, which can be a worthwhile trade-off.</p>
<h2 id="heading-how-to-optimize-the-database">How to Optimize the Database</h2>
<p>When a Laravel app slows down, the database is usually the first place to look.</p>
<h3 id="heading-add-indexes-around-real-query-patterns">Add Indexes Around Real Query Patterns</h3>
<p>Start with your slow query log, database metrics, and traces rather than intuition. If the app constantly looks up active subscriptions by account, build a compound index that matches that access pattern:</p>
<pre><code class="language-php">Schema::table('subscriptions', function (Blueprint $table) {
    $table-&gt;index(['account_id', 'status', 'renews_at']);
});
</code></pre>
<p>Then write the query so it can actually use the index:</p>
<pre><code class="language-php">\(subscription = Subscription::where('account_id', \)accountId)
    -&gt;where('status', 'active')
    -&gt;where('renews_at', '&gt;=', now())
    -&gt;orderBy('renews_at')
    -&gt;first();
</code></pre>
<p>Get in the habit of running <code>EXPLAIN</code> after you add an index to confirm that the plan changed. An index the optimizer ignores is just write overhead.</p>
<h3 id="heading-use-eager-loading-deliberately">Use Eager Loading Deliberately</h3>
<p>Match eager loading to what the endpoint actually returns. For list endpoints, keep relationships shallow and constrained:</p>
<pre><code class="language-php">$projects = Project::query()
    -&gt;select(['id', 'account_id', 'name', 'updated_at'])
    -&gt;withCount('openTasks')
    -&gt;with([
        'owner:id,name',
    ])
    -&gt;where('account_id', $accountId)
    -&gt;latest('updated_at')
    -&gt;paginate(30);
</code></pre>
<p>When you only need a number, <code>withCount</code> beats loading a whole relationship to count it:</p>
<pre><code class="language-php">$teams = Team::query()
    -&gt;withCount([
        'members',
        'invitations as pending_invitations_count' =&gt; fn (\(query) =&gt; \)query-&gt;whereNull('accepted_at'),
    ])
    -&gt;paginate(25);
</code></pre>
<p>Your memory footprint stays flat, which matters much more on a list page than on a detail page.</p>
<h3 id="heading-optimize-queries-before-adding-hardware">Optimize Queries Before Adding Hardware</h3>
<p>A bigger database instance buys you time. It also hides the inefficient queries that put you there until the next traffic jump exposes them again. Before you reach for a larger machine, find your highest-cost queries. In local or staging environments, logging slow ones is easy:</p>
<pre><code class="language-php">use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

DB::listen(function (QueryExecuted $query) {
    if ($query-&gt;time &gt; 100) {
        Log::warning('Slow query detected', [
            'sql' =&gt; $query-&gt;toRawSql(),
            'time_ms' =&gt; $query-&gt;time,
        ]);
    }
});
</code></pre>
<p>Be careful doing this in production. Bindings can contain sensitive data, and verbose logging at high volume can become its own performance problem.</p>
<h3 id="heading-process-large-tables-with-chunking">Process Large Tables with Chunking</h3>
<p>Never pull an entire large table into memory for a batch job:</p>
<pre><code class="language-php">User::where('is_active', true)
    -&gt;chunkById(1000, function ($users) {
        foreach (\(users as \)user) {
            RefreshUserSearchIndex::dispatch($user-&gt;id);
        }
    });
</code></pre>
<p><code>chunkById</code> is safer than offset-based chunking when rows can change while the job runs, because it tracks the last seen ID instead of a numeric offset. For very large exports, stream the records or write them out in batches.</p>
<h3 id="heading-use-cursor-pagination-for-high-volume-feeds">Use Cursor Pagination for High-Volume Feeds</h3>
<p>Offset pagination gets slower the deeper a user scrolls, because the database still has to skip every row it's not returning. For feeds, audit logs, messages, and timelines, cursor pagination is usually the better fit:</p>
<pre><code class="language-php">$events = AuditEvent::query()
    -&gt;where('account_id', $accountId)
    -&gt;orderByDesc('id')
    -&gt;cursorPaginate(50);

return AuditEventResource::collection($events);
</code></pre>
<p>It relies on a stable, indexed ordering column and uses next/previous cursors rather than arbitrary page numbers, which is what an infinite-scroll feed usually needs.</p>
<h3 id="heading-split-reads-with-read-replicas">Split Reads with Read Replicas</h3>
<p>As read traffic grows, replicas can take load off the primary:</p>
<pre><code class="language-php">'mysql' =&gt; [
    'driver' =&gt; 'mysql',
    'read' =&gt; [
        'host' =&gt; [
            env('DB_READ_HOST', '127.0.0.1'),
        ],
    ],
    'write' =&gt; [
        'host' =&gt; [
            env('DB_WRITE_HOST', '127.0.0.1'),
        ],
    ],
    'sticky' =&gt; true,
    'database' =&gt; env('DB_DATABASE', 'laravel'),
    'username' =&gt; env('DB_USERNAME', 'root'),
    'password' =&gt; env('DB_PASSWORD', ''),
],
</code></pre>
<p>The <code>sticky</code> option keeps reads on the write connection after a write within the same request, which helps avoid some read-after-write surprises.</p>
<p>Replicas come with replication lag, and that lag matters. Don't route payment confirmations, password changes, permission checks, or anything else consistency-sensitive to a replica that might be a few seconds stale unless the business flow can genuinely tolerate seeing old data.</p>
<h2 id="heading-how-to-scale-with-redis">How to Scale with Redis</h2>
<p>Redis often does a lot in a Laravel production stack: caching, sessions, rate limiting, queues, locks, and Horizon metrics. It's fast, but it still needs thought: sensible key design, expiration policies, memory monitoring, and a real plan for invalidation.</p>
<h3 id="heading-caching">Caching</h3>
<p>Cache expensive reads that get requested often and can tolerate being slightly out of date:</p>
<pre><code class="language-php">use Illuminate\Support\Facades\Cache;

$stats = Cache::remember(
    "accounts:{$account-&gt;id}:dashboard-stats",
    now()-&gt;addMinutes(5),
    fn () =&gt; DashboardStats::forAccount($account)-&gt;calculate()
);
</code></pre>
<p>Short time-to-live values go a surprisingly long way. A five-minute cache can wipe out thousands of duplicate queries while keeping the data fresh enough for most dashboards.</p>
<p>When the data changes after a known event, invalidate it explicitly:</p>
<pre><code class="language-php">Order::created(function (Order $order) {
    Cache::forget("accounts:{$order-&gt;account_id}:dashboard-stats");
});
</code></pre>
<p>Caching works best when your keys are predictable and your invalidation is tied to domain events rather than guesswork.</p>
<h3 id="heading-sessions">Sessions</h3>
<p>For horizontally scaled app servers, file-based sessions are a trap: the next request can land on a different server that has never seen the session. Store sessions in Redis or a database so any server can handle any request:</p>
<pre><code class="language-env">SESSION_DRIVER=redis
CACHE_STORE=redis
QUEUE_CONNECTION=redis
</code></pre>
<h3 id="heading-rate-limiting">Rate Limiting</h3>
<p>Rate limits protect you from abusive clients, runaway loops, and endpoints that get hammered:</p>
<pre><code class="language-php">use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;

RateLimiter::for('api', function (Request $request) {
    return Limit::perMinute(120)-&gt;by(
        optional(\(request-&gt;user())-&gt;id ?: \)request-&gt;ip()
    );
});
</code></pre>
<p>Expensive endpoints deserve stricter limits:</p>
<pre><code class="language-php">RateLimiter::for('exports', function (Request $request) {
    return Limit::perHour(10)-&gt;by($request-&gt;user()-&gt;id);
});
</code></pre>
<p>Let business cost drive the numbers. Login, search, export, and webhook endpoints rarely need the same limit.</p>
<h3 id="heading-queues">Queues</h3>
<p>Redis is a common queue backend because it's quick and Horizon supports it well:</p>
<pre><code class="language-env">QUEUE_CONNECTION=redis
</code></pre>
<p>Dispatch work onto named queues from the request:</p>
<pre><code class="language-php">GenerateInvoicePdf::dispatch($invoice-&gt;id)
    -&gt;onQueue('documents');
</code></pre>
<p>Split work by profile, such as <code>default</code>, <code>emails</code>, <code>webhooks</code>, <code>documents</code>, and <code>imports</code>, because each workload can need different worker counts and retry rules. Keep the names meaningful. During an incident, "the documents queue is 20 minutes behind" tells you far more than "default is slow."</p>
<h2 id="heading-how-to-use-queue-driven-architectures">How to Use Queue-Driven Architectures</h2>
<p>Queues are one of Laravel's best scaling tools. They let the app accept work quickly and process it asynchronously with controlled concurrency. They also make the system more resilient: when a third-party API goes down, jobs retry on their own instead of tying up your PHP-FPM request workers.</p>
<h3 id="heading-laravel-queues">Laravel Queues</h3>
<p>A good job is small, idempotent, and safe to retry:</p>
<pre><code class="language-php">use App\Mail\OrderReceiptMail;
use App\Models\Order;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Mail;

class SendOrderReceipt implements ShouldQueue
{
    use Queueable;

    public int $tries = 3;
    public int $backoff = 60;

    public function __construct(public int $orderId)
    {
    }

    public function handle(): void
    {
        \(order = Order::with('user')-&gt;findOrFail(\)this-&gt;orderId);

        Mail::to(\(order-&gt;user)-&gt;send(new OrderReceiptMail(\)order));
    }
}
</code></pre>
<p>Pass IDs into jobs rather than full Eloquent models. The model might change before the job runs, and serializing a whole model bloats the payload. For external APIs, add timeouts and guard against duplicate work:</p>
<pre><code class="language-php">use App\Models\Order;
use App\Services\CrmClient;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class SyncOrderToCrm implements ShouldQueue
{
    use Queueable;

    public int $tries = 3;
    public int $backoff = 60;

    public function __construct(public int $orderId)
    {
    }

    public function handle(CrmClient $crm): void
    {
        \(order = Order::findOrFail(\)this-&gt;orderId);

        if ($order-&gt;crm_synced_at) {
            return;
        }

        \(crm-&gt;upsertOrder(\)order-&gt;external_reference, [
            'total' =&gt; $order-&gt;total,
            'status' =&gt; $order-&gt;status,
        ]);

        $order-&gt;forceFill(['crm_synced_at' =&gt; now()])-&gt;save();
    }
}
</code></pre>
<p>The <code>crm_synced_at</code> check is the whole point. Jobs run more than once in real life, and idempotency is what keeps a retry from double-charging or double-syncing.</p>
<h3 id="heading-horizon">Horizon</h3>
<p>Horizon gives you visibility and control over Redis queues. A typical setup runs different supervisors for different workloads:</p>
<pre><code class="language-php">'production' =&gt; [
    'supervisor-default' =&gt; [
        'connection' =&gt; 'redis',
        'queue' =&gt; ['default', 'emails'],
        'balance' =&gt; 'auto',
        'maxProcesses' =&gt; 20,
        'tries' =&gt; 3,
    ],

    'supervisor-documents' =&gt; [
        'connection' =&gt; 'redis',
        'queue' =&gt; ['documents'],
        'balance' =&gt; 'simple',
        'maxProcesses' =&gt; 5,
        'tries' =&gt; 2,
        'timeout' =&gt; 300,
    ],
],
</code></pre>
<p>The separation matters: a long-running document job shouldn't starve a quick password-reset email.</p>
<h3 id="heading-failed-jobs-and-retries">Failed Jobs and Retries</h3>
<p>Retries only help when failures are temporary. Retrying a job that's permanently broken just burns capacity. For jobs with a business deadline, use <code>retryUntil</code>:</p>
<pre><code class="language-php">use DateTime;
use Throwable;

public function retryUntil(): DateTime
{
    return now()-&gt;addMinutes(30);
}

public function failed(Throwable $exception): void
{
    ImportBatch::whereKey($this-&gt;batchId)-&gt;update([
        'status' =&gt; 'failed',
        'failed_reason' =&gt; $exception-&gt;getMessage(),
    ]);
}
</code></pre>
<p>Use <code>failed</code> to flag the problem somewhere a human will see it. Whatever you do, don't set unlimited retries on jobs that hit a third-party service.</p>
<h3 id="heading-queue-monitoring">Queue Monitoring</h3>
<p>Track queue depth, wait time, failure rate, and processing time together. Depth alone can mislead you. When depth starts climbing, walk through it methodically: are workers keeping pace with incoming jobs? If the queue keeps growing, check how long individual jobs take. If the slow part is the database, fix the query or dial back worker concurrency. If it's an external API, add backoff or a circuit breaker. If the work is CPU-bound, scale workers or break the jobs into smaller pieces.</p>
<p>Be careful with the "scale workers" instinct, though. Adding more workers without checking the database first can make an incident worse. More workers mean more concurrent queries, more locks, and more pressure on the primary exactly when it's already struggling.</p>
<h2 id="heading-how-to-optimize-api-performance">How to Optimize API Performance</h2>
<p>APIs earn special attention because clients call them repeatedly and payloads tend to grow quietly over months.</p>
<h3 id="heading-api-resources">API Resources</h3>
<p>Resources keep your response shape intentional:</p>
<pre><code class="language-php">class OrderResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'id' =&gt; $this-&gt;id,
            'status' =&gt; $this-&gt;status,
            'total' =&gt; $this-&gt;total,
            'placed_at' =&gt; $this-&gt;created_at-&gt;toIso8601String(),
            'customer' =&gt; new CustomerSummaryResource($this-&gt;whenLoaded('customer')),
        ];
    }
}
</code></pre>
<p><code>whenLoaded</code> is doing real work here. It stops the resource from quietly triggering a lazy query when the relationship wasn't eager loaded:</p>
<pre><code class="language-php">$orders = Order::query()
    -&gt;with('customer:id,name')
    -&gt;where('account_id', $accountId)
    -&gt;latest()
    -&gt;paginate(50);

return OrderResource::collection($orders);
</code></pre>
<h3 id="heading-pagination">Pagination</h3>
<p>Returning unbounded collections is an easy way to create an API performance problem you won't notice until a client has a lot of data:</p>
<pre><code class="language-php">$perPage = min((int) request('per_page', 50), 100);

\(orders = Order::where('account_id', \)accountId)
    -&gt;latest()
    -&gt;paginate($perPage);
</code></pre>
<p>Cap the page size. If a client genuinely needs every record for an export, make that an async job rather than a giant synchronous response.</p>
<h3 id="heading-response-optimization">Response Optimization</h3>
<p>Stop returning fields nobody reads. On read-heavy endpoints, selecting only the columns you need cuts both database I/O and serialization cost:</p>
<pre><code class="language-php">$products = Product::query()
    -&gt;select(['id', 'name', 'slug', 'price', 'thumbnail_url'])
    -&gt;where('is_visible', true)
    -&gt;orderBy('name')
    -&gt;paginate(40);
</code></pre>
<p>It's also worth turning on compression at the web server or load balancer. JSON compresses extremely well, and that's often a small config change with a real bandwidth payoff.</p>
<h3 id="heading-rate-limiting">Rate Limiting</h3>
<p>Design API rate limits around identity and endpoint cost:</p>
<pre><code class="language-php">Route::middleware(['auth:sanctum', 'throttle:api'])
    -&gt;group(function () {
        Route::get('/orders', [OrderController::class, 'index']);
        Route::post('/exports/orders', [OrderExportController::class, 'store'])
            -&gt;middleware('throttle:exports');
    });
</code></pre>
<p>This keeps casual browsing and expensive exports under separate policies, so one heavy user can't squeeze out everyone else.</p>
<h3 id="heading-caching-api-responses">Caching API Responses</h3>
<p>Cache responses that are expensive to compute and can tolerate being a little stale:</p>
<pre><code class="language-php">public function index(Request $request)
{
    \(accountId = \)request-&gt;user()-&gt;account_id;
    \(page = \)request-&gt;integer('page', 1);

    \(cacheKey = "api:accounts:{\)accountId}:orders:v1:page:{$page}";

    return Cache::remember(\(cacheKey, now()-&gt;addSeconds(60), function () use (\)accountId) {
        return OrderResource::collection(
            Order::with('customer:id,name')
                -&gt;where('account_id', $accountId)
                -&gt;latest()
                -&gt;paginate(50)
        )-&gt;response()-&gt;getData(true);
    });
}
</code></pre>
<p>Notice the <code>v1</code> in the key. Bumping that version number lets you invalidate an entire response format at once when the shape changes. Always scope the key to the tenant or user for anything that's not truly global.</p>
<h2 id="heading-how-to-monitor-laravel-in-production">How to Monitor Laravel in Production</h2>
<p>The teams that catch problems before customers do are the ones collecting signals from everywhere: Laravel, queues, the database, Redis, the infrastructure, and external services.</p>
<p>Laravel gives you several good starting points. Horizon shows queue throughput, failed jobs, wait times, and worker balancing. Telescope surfaces request details, queries, exceptions, jobs, mail, and cache events. Your logs capture slow operations, unexpected retries, and external failures. Your metrics track latency, error rate, queue depth, job runtime, database CPU, lock waits, cache hit ratio, and Redis memory. Your alerting ties all of it back to something a customer would actually feel.</p>
<p>That last part is where teams often make mistakes. The best alerts are about symptoms, not machines being busy: p95 API latency over 800ms for 10 minutes, checkout error rate above 1%, the emails queue waiting more than 5 minutes, database CPU over 85% with slow queries rising, Redis memory over 80%, or failed payment webhooks crossing a threshold.</p>
<p>A useful mental model is this: logs tell you what happened, metrics tell you whether the system is healthy, and traces tell you where the time went. In practice, wrapping your expensive business operations in a bit of instrumentation pays off quickly:</p>
<pre><code class="language-php">use Illuminate\Support\Facades\Log;

$startedAt = microtime(true);

\(report = \)builder-&gt;forAccount($account)-&gt;build();

Log::info('Billing report generated', [
    'account_id' =&gt; $account-&gt;id,
    'duration_ms' =&gt; (int) ((microtime(true) - $startedAt) * 1000),
    'invoice_count' =&gt; $report-&gt;invoiceCount(),
]);
</code></pre>
<p>When something is failing at 2am, a log line like that can tell you which account, import, or report is causing the pressure.</p>
<p>One more thing worth internalizing: monitor wait time, not just throughput. A queue can process thousands of jobs a minute and still be unhealthy if important jobs sit waiting too long before they start. Users feel the wait, not the throughput.</p>
<h2 id="heading-an-example-high-traffic-laravel-architecture">An Example High-Traffic Laravel Architecture</h2>
<p>A high-traffic Laravel setup generally separates four things: stateless web requests, shared cache and session storage, asynchronous workers, and database roles.</p>
<p>Users hit a load balancer, which spreads traffic across a fleet of stateless Laravel app servers. Those servers use Redis for cache, sessions, rate limits, queues, and Horizon data. Queue workers handle slow or unreliable work off to the side. A MySQL primary takes all writes and any consistency-sensitive reads, while a read replica absorbs read-heavy endpoints that can tolerate some replication lag.</p>
<p>The flow looks like this:</p>
<pre><code class="language-text">Users
  -&gt; Load balancer
  -&gt; Stateless Laravel app servers
  -&gt; Redis for cache, sessions, rate limits, queues, and Horizon data
  -&gt; Primary database for writes and consistency-sensitive reads
  -&gt; Read replica for safe read-heavy endpoints

Redis queue
  -&gt; Queue workers
  -&gt; Database, external APIs, mail providers, object storage, and other services
</code></pre>
<p>This isn't the only valid shape. PostgreSQL can stand in for MySQL, Amazon SQS can replace Redis queues, a CDN can serve static assets and cache public responses, and object storage should hold user uploads. The principle that matters is that each layer has one clear job and can be scaled or tuned on its own.</p>
<p>The flip side of stateless app servers is that anything a user needs after the request ends has to live in shared storage. Uploads, generated files, and session state shouldn't sit on a single server's local disk, or they may disappear from the user's point of view when the load balancer sends the next request somewhere else.</p>
<h2 id="heading-lessons-learned-the-hard-way">Lessons Learned the Hard Way</h2>
<h3 id="heading-1-premature-optimization">1. Premature Optimization</h3>
<p>This usually shows up as elaborate infrastructure built before the app has any real visibility into itself.</p>
<p>The practical path works better: measure, rank the bottlenecks, fix the biggest one, repeat. For most Laravel apps, the first round of scaling is mostly indexes, N+1 fixes, queue separation, and trimming payloads.</p>
<h3 id="heading-2-over-caching">2. Over-caching</h3>
<p>Caching can make a system faster and harder to reason about at the same time. One team cached an account-settings response for 30 minutes, then later folded role changes into that same response. The result was that users who had just lost access could still see features until the cache expired.</p>
<p>The fix was splitting stable account metadata away from permission-sensitive state. The lesson is to avoid caching authorization data unless you have thought carefully about invalidation.</p>
<h3 id="heading-3-missing-indexes">3. Missing Indexes</h3>
<p>These hide until a table crosses a size threshold. A query that scanned 20,000 rows in development can scan 20 million in production. Bake index review into feature work, and plan big index migrations carefully so they don't lock a hot table at the worst possible time.</p>
<h3 id="heading-4-queue-overload">4. Queue Overload</h3>
<p>Queues don't remove work, they move it. The classic failure is letting one noisy workload block everything else. A big CSV import floods the default queue, and password-reset emails get stuck behind it. Separate queues are cheap insurance against that entire class of incident.</p>
<h3 id="heading-5-large-transactions">5. Large Transactions</h3>
<p>Long transactions hold locks longer and make failures more expensive. Dispatching a job inside a transaction is especially risky because a worker can grab it before the transaction commits:</p>
<pre><code class="language-php">DB::transaction(function () use ($request) {
    $order = Order::create([...]);
    \(order-&gt;items()-&gt;createMany(\)request-&gt;items);

    GenerateInvoicePdf::dispatch($order-&gt;id);
    SyncOrderToCrm::dispatch($order-&gt;id);
});
</code></pre>
<p>Use after-commit dispatching for any job that depends on committed data:</p>
<pre><code class="language-php">GenerateInvoicePdf::dispatch($order-&gt;id)-&gt;afterCommit();
SyncOrderToCrm::dispatch($order-&gt;id)-&gt;afterCommit();
</code></pre>
<p>Keep transactions scoped to the data that genuinely has to change atomically, and nothing more.</p>
<h3 id="heading-6-treating-symptoms-as-causes">6. Treating Symptoms as Causes</h3>
<p>This is the expensive one. If latency is high because an endpoint runs 300 queries, adding app servers adds database pressure. If jobs are slow because an external API is rate-limiting you, adding workers multiplies the failures.</p>
<p>Good scaling work keeps asking the same questions: What resource is saturated? Which endpoint, job, tenant, or query is causing it? Is this work necessary during the request? Can I reduce it, defer it, cache it, or isolate it? How will I know whether the change helped?</p>
<h2 id="heading-a-pre-launch-scaling-checklist">A Pre-Launch Scaling Checklist</h2>
<p>Run through this before a big launch, a traffic campaign, or an enterprise rollout.</p>
<p><strong>Application and runtime:</strong> Cache config, routes, and views during deploy. Set <code>APP_DEBUG=false</code>. Turn on OPcache. Keep web requests short and move slow work to queues. Store uploads in object storage, not on app-server disk. Keep servers stateless. Set timeouts on every external HTTP call.</p>
<p><strong>Database:</strong> Review slow query logs first. Add indexes for your high-volume filters, joins, and ordering. Hunt for N+1 queries in controllers, resources, policies, and views. Paginate every list endpoint. Use <code>chunkById</code> or cursors for batch work. Avoid long transactions and external calls inside transactions. Confirm your backup and restore process works. Test stale-read behavior if you use replicas.</p>
<p><strong>Redis and cache:</strong> Use Redis for cache, sessions, rate limiting, and queues where it fits. Set TTLs unless you have a clear reason not to. Include tenant, user, locale, and version in keys when relevant. Watch memory and the eviction policy. Avoid caching permission-sensitive responses without careful invalidation. Guard against cache stampedes on expensive recomputation.</p>
<p><strong>Queues:</strong> Separate queues by workload. Configure Horizon supervisors per queue. Set timeouts, retries, and backoff on purpose. Make jobs idempotent where you can. Use <code>afterCommit</code> for jobs that depend on committed data. Monitor wait time, runtime, failures, and retries. Review failed jobs instead of ignoring them.</p>
<p><strong>APIs:</strong> Use Resources to control response shape. Cap <code>per_page</code>. Use cursor pagination for big feeds and logs. Cache expensive reads with safe, versioned keys and short TTLs. Apply rate limits by endpoint cost. Don't return raw Eloquent models. Compress responses at the edge.</p>
<p><strong>Observability:</strong> Track p50, p95, and p99 latency on the endpoints that matter. Track error rates by route and job class. Alert on queue wait time, not just size. Watch database CPU, connections, slow queries, and lock waits. Watch Redis memory, latency, and evictions. Log important business operations with durations and identifiers. Test your alerts before launch night because a silent alert is worse than no alert.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Laravel runs high-traffic production systems well when you design around the real costs of data, concurrency, and external dependencies. Just make sure you measure before you optimize, because guessing wastes time and tends to complicate the wrong layer.</p>
<p>Fix the database first: indexes, query shape, pagination, and eager loading usually deliver the biggest early wins. Lean on queues to keep requests fast and push slow work into controlled background workers. Cache deliberately, with clear keys, sane TTLs, and a plan for invalidation. Keep watching latency, errors, queue wait time, database health, Redis memory, and your external dependencies.</p>
<p>The best scaling work is practical and repeatable. You study the system you actually have, remove waste, isolate slow parts, and give yourself enough visibility to make the next change with confidence. Do that on a loop, and you rarely need the big rewrite.</p>
<h2 id="heading-references">References</h2>
<ul>
<li><p><a href="https://laravel.com/docs/eloquent-relationships">Laravel documentation: Eloquent relationships</a></p>
</li>
<li><p><a href="https://laravel.com/docs/queries">Laravel documentation: Database queries</a></p>
</li>
<li><p><a href="https://laravel.com/docs/cache">Laravel documentation: Cache</a></p>
</li>
<li><p><a href="https://laravel.com/docs/queues">Laravel documentation: Queues</a></p>
</li>
<li><p><a href="https://laravel.com/docs/redis">Laravel documentation: Redis</a></p>
</li>
<li><p><a href="https://laravel.com/docs/routing#rate-limiting">Laravel documentation: Rate limiting</a></p>
</li>
<li><p><a href="https://laravel.com/docs/eloquent-resources">Laravel documentation: Eloquent API resources</a></p>
</li>
<li><p><a href="https://laravel.com/docs/horizon">Laravel Horizon documentation</a></p>
</li>
<li><p><a href="https://laravel.com/docs/telescope">Laravel Telescope documentation</a></p>
</li>
<li><p><a href="https://dev.mysql.com/doc/refman/8.4/en/optimization.html">MySQL documentation: Optimization</a></p>
</li>
<li><p><a href="https://redis.io/docs/latest/">Redis documentation</a></p>
</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
