Engineering / Engineering journal

Scaling a Laravel Monolith Before Splitting It

A Laravel monolith under load rarely needs to be split first. How to find the real bottleneck and the order in which fixes tend to pay off.

Sylantix Engineering Team7 min read
A development workstation showing application profiling and a job queue.
Sylantix / Engineering journalLaravel · Performance · Architecture
In this article 9 sections
TL;DR

Key takeaways

  • Measure first. Slow Laravel apps are usually slow in a handful of queries or one blocking call, not everywhere.
  • Queues, caching and database work fix most load problems at a fraction of the cost of a rewrite.
  • Sharding and extracted services are real tools, but only once data volume or team boundaries demand them.
  • Microservices move complexity onto the network. Take that trade only when the monolith has run out of cheaper options.

Find where the request spends its time

When a Laravel application slows down, the cause may be concentrated in a few queries, external calls or jobs running during web requests. Splitting the application into services will not necessarily remove those bottlenecks.

Start with production measurements. The sequence below covers database access, caching, queues and infrastructure before considering whether any part needs to become a separate service.

Step one: find out where the time goes

Guessing is the default and it is almost always wrong. The query everybody blames turns out to be fine, and the real cost hides in a loop nobody looked at.

Profile real requests

Start with the slowest endpoints in production, not on a laptop with ten rows of seed data. Application performance monitoring, request tracing or even timing middleware will show where a request spends its time: database, external APIs, view rendering, or plain PHP work. Sort by total time, not by slowest single call. An endpoint that takes 300 milliseconds but runs a million times a day matters more than a report that takes eight seconds once a week.

Read the slow query log

Turn on the database slow query log with a low threshold for a few days. It is boring, free and brutally honest. You will see the same handful of queries again and again: a missing index on a foreign key, a sort on an unindexed column, a count over a table that grew from thousands of rows to tens of millions.

Hunt the N+1

Eloquent makes it pleasant to write code that loads a list of orders and then, inside a loop, touches each order's customer, items and invoices. Every touch is another query. With twenty orders nobody notices. With two thousand, a page fires thousands of queries. Eager loading the relationships, or reshaping the query, often turns a multi-second page into a fast one without changing anything else. Laravel can also be told to complain loudly about lazy loading in development, which stops new cases from creeping back in.

Step two: get slow work out of the request

Many requests are slow because they do work the user does not need to wait for. Sending emails, generating PDFs, calling a CRM, recalculating statistics, resizing images: none of it has to happen before the response goes back.

Queues with Horizon

Laravel queues backed by Redis, supervised with Horizon, are the cheapest scaling tool in the framework. Move the slow parts into jobs, return to the user immediately, and let workers handle the rest. Horizon shows throughput, wait times and failed jobs per queue, so you can see where work piles up.

Two habits make queues safe at scale. Split work into separate queues by priority, so a flood of report jobs cannot delay password reset emails. And make jobs idempotent, because sooner or later one will run twice.

Watch the queue, not just the web servers

Once work moves to queues, slowness can hide there. A dashboard that shows how long jobs wait before they start is worth more than another CPU graph. If wait times grow during peak hours, add workers for that queue rather than scaling everything.

Step three: stop computing the same thing twice

After queries are fixed and slow work is queued, look at repetition. A surprising amount of load comes from answering the same question for every visitor.

Layers of caching

There are several places to cache, and they solve different problems:

  • Query or model caching for data that changes rarely, such as plans, settings or reference tables.
  • Computed results such as dashboard totals or permission maps, stored in Redis and invalidated when the underlying data changes.
  • Full responses for pages that look the same for many users.
  • A CDN in front of static assets and public pages, so those requests never reach PHP at all.

The hard part is invalidation, not storage. Decide for each cached item what event makes it stale, and tie the cache key to that event. A cache with a vague expiry time is a bug waiting for a busy day.

Step four: give the database room

At some point the database becomes the shared bottleneck that every request waits on. Before you think about splitting it, there is a lot of room to work with.

Indexes and query shape

Revisit indexes with the slow query log in hand. Composite indexes that match how you filter and sort, covering indexes for hot read paths and removing unused indexes that slow down writes all help. Sometimes the fix is a query rewrite: replacing a giant join with two simple queries, or paginating with a cursor instead of an offset that scans further on every page.

Read replicas

Most SaaS products read far more than they write. Laravel can send reads to replicas and writes to the primary with a small configuration change. Watch for replication lag on screens where a user expects to see what they just saved, and route those reads to the primary.

Archiving and partitioning

Tables full of old events, logs or audit rows slow down everything that touches them. Moving history to an archive table, or partitioning by date, keeps the working set small without deleting anything the business needs.

Step five: when splitting is justified

There are real reasons to go further, and it helps to name them precisely.

Sharding by tenant

When a single database cannot hold the data or the write volume, even after the steps above, sharding becomes the honest answer. In multi-tenant SaaS the natural shard key is usually the tenant: each customer's data lives on one shard, and a routing layer sends queries to the right place. This is a serious change. Cross-tenant reports, migrations and backups all get harder. Do it because the numbers demand it, not because it sounds like what big companies do.

Extracting one service

Pulling a single part out of the monolith makes sense when that part has very different needs: heavy CPU work such as video or document processing, a component with its own release rhythm, or a separate team that keeps stepping on the main codebase. Extract that one piece, behind a clear API or a queue, and leave the rest alone.

Why microservices are rarely the first fix

A monolith call is a function call. A service call is a network request that can time out, fail halfway or return stale data. Every boundary you add brings its own deployment, monitoring, versioning and failure modes. If the original slowness came from an N+1 query or a missing index, splitting the code carries that query into a new service and adds network latency on top.

Teams that split too early often end up with a distributed monolith: services that cannot be deployed independently because they share a database or call each other synchronously for everything. It is slower than what they started with and much harder to change.

For the wider architecture question, see monolith vs microservices. A well-structured monolith, with clear modules inside it, queues for background work and a tuned database, carries most products a very long way. When it stops being enough, you will know exactly which part to extract, because you measured it.

A real example

We went through this sequence with a B2B SaaS platform whose Laravel monolith was buckling under growth. Profiling, a queue-based architecture with Horizon, multi-layered caching and, where data volume required it, a sharding strategy prepared the system for ten times the traffic. Average API response time dropped by 63% and the monthly infrastructure bill fell by 28%. The full case study describes each step.

Profile the slowest endpoints

Pick your ten slowest endpoints by total time in production. For each one, write down where the time goes: database, external calls, rendering or computation. Turn on the slow query log and lazy loading warnings, and fix the first three things you find. Then decide what can move to a queue. Only after that list is empty is it worth talking about service boundaries. Our notes on Laravel development describe how we approach this kind of work.

Facing this in your own product?

Describe your situation in a few sentences. Our engineering team replies within 24-48 hours on business days, and the first 30-minute consultation is free.

Sylantix Engineering Team

Written by the senior engineers who build these systems for clients: backends, AI features, mobile apps and the infrastructure under them. Questions about your own case go straight to them.

About the team →