All posts
Architecture

Scaling Laravel From Zero to a Million Users

14 min read

Most Laravel apps do not fail because the framework is slow. They fail because a handful of early decisions quietly turn into bottlenecks once traffic arrives. This post walks through the ones that matter, roughly in the order you hit them.

Start With the Database

The database is almost always the first thing to buckle. Get the fundamentals right early and most other scaling problems become tractable.

Index What You Query

Every column you filter, join, or sort on in a hot path needs an index. Watch the slow query log, not your intuition. A single missing index on a foreign key can turn a 20ms request into a 2-second one under load.

Avoid N+1 From Day One

Eager load relationships with `with()` and enable Eloquent's `preventLazyLoading()` in non-production environments so N+1 queries fail loudly during development instead of silently in production.

Read Replicas

When reads dominate writes, point Eloquent at a read replica for queries and keep writes on the primary. Laravel supports this natively through the database connection config, so the application code barely changes.

Cache Aggressively

Caching is the highest-leverage change you can make. The fastest query is the one you never run.

Cache the Right Layer

Cache computed results and rendered fragments, not raw rows. Caching a row saves one query; caching an assembled view model or an HTTP response saves the whole pipeline behind it.

Pick a Real Cache Store

The file and database cache drivers are fine for local development and useless under real load. Use Redis. It gives you atomic operations, tags, and a natural home for sessions and queues too.

Move Work Off the Request

Anything that does not need to finish before the user sees a response should not run inside the request. Email, image processing, third-party API calls, webhooks, and report generation all belong on a queue.

Queues and Workers

Push slow work into jobs and run Horizon to supervise your Redis workers. Horizon gives you throughput metrics, failed-job visibility, and auto-balancing across queues with almost no code.

Idempotency Matters

Jobs get retried. Design them so running twice is safe: check whether the work is already done, use unique job locks, and never assume exactly-once delivery.

Observe Everything

You cannot scale what you cannot see. Instrument the app before you need to, not during an incident.

Metrics and Tracing

Track request latency percentiles, queue depth, and database time per request. Tools like Telescope in staging and an APM in production tell you where the milliseconds actually go.

Alert on Symptoms

Alert on user-facing symptoms such as error rate and p95 latency, not on causes like CPU. Causes change; the symptom you care about is whether the app is fast and working.

Closing Thoughts

Scaling is less about clever tricks and more about removing the obvious bottlenecks in the right order: database, cache, queues, observability. Do those well and Laravel will comfortably take you a very long way before you need anything exotic.