Read replicas are a well-known scaling pattern. Set up a few followers of the primary, route reads to them, and the primary handles only writes. The infrastructure side is cleanly documented for every major database. The routing side — deciding which queries go where, and what happens when a replica is briefly behind — is where things get subtle.
The failure mode that bites teams is not the infrastructure. It is the bug where a user creates a record, the application immediately reads it back from a replica that hasn't caught up, and the user sees the record missing or stale. The fix is not "add more replicas." It is to route reads with intent: knowing which queries can tolerate lag and which absolutely cannot.
This guide covers the routing strategies that work in production and the cases where each fails.
What "Replication Lag" Actually Means
A replica lags the primary by some amount of time — the gap between when a write commits on the primary and when it is visible on the replica. Typical lag is milliseconds. Under load, during long transactions, or when network is congested, lag grows to seconds or minutes.
The lag is not uniform across replicas. Replica A might be 200ms behind; replica B might be 4 seconds behind. Some database systems (Postgres physical replication, MySQL row-based replication) replay changes in order, so lag is a single number per replica. Others (logical replication with parallelism) have per-table or per-key lag, which is even harder to reason about.
Reading from a lagged replica is fine for most queries. "What were sales last quarter" tolerates 30 seconds of lag. "What is the user's current balance after they just deposited" does not.
Strategies That Work
Four patterns appear repeatedly in production:
1. Default-write-to-primary
The simplest strategy. The application defaults to using the primary for everything. Explicitly mark specific read queries as "can hit a replica." Common in Laravel via the connection setting:
DB::connection('mysql_read')->select(...);
// or scope on a model
User::on('mysql_read')->where(...)->get();
Works well when most code defaults to correctness and only the high-volume, lag-tolerant queries are explicitly tagged. Fails when developers forget to tag them — the primary continues to take the read load.
2. Default-to-replica, primary-for-known-paths
The inverse. All reads go to a replica by default. Specific paths that require read-your-own-write consistency are tagged to use the primary.
Laravel's default with read/write array config:
'mysql' => [
'read' => ['host' => ['replica1.db', 'replica2.db']],
'write' => ['host' => 'primary.db'],
'sticky' => true,
],
With sticky: true, any transaction that performs a write reads from the primary for the remainder of the request. This catches the most common case (create-then-read in the same request).
Works well for high-traffic read-heavy applications. Fails when developers run a read outside a transaction expecting recent writes to be visible.
3. Per-transaction routing
More sophisticated. A transaction is opened against a specific connection (primary or replica). Reads inside the transaction always hit that connection. Code can explicitly request a primary or replica transaction:
DB::transaction(function () {
// explicitly primary — sees the write immediately
Order::create([...]);
$latest = Order::latest()->first();
}, connection: 'primary');
This forces developers to think about consistency requirements. The cost is verbosity and the discipline to use it correctly.
4. Bounded staleness
The replica routing decision considers replication lag. A query that can tolerate up to 5 seconds of lag is routed to the freshest replica that meets that bound. If no replica is fresh enough, the query falls back to the primary.
selectReplica(maxLagSeconds: 5)
This pattern requires lag observability per replica and a routing layer that can read it. ProxySQL and pgbouncer can be configured for this; some application frameworks have plugins.
The cost is operational complexity. The benefit is that critical queries get correctness, lag-tolerant queries get scale, and the routing decision is automated.
Where Routing Goes Wrong
A few patterns that appear in every production system that uses replicas.
Read-your-own-write within a single page load. A user POSTs an update, the response is computed, and an embedded API call later in the response reads from a replica. The replica hasn't caught up. The user sees stale data even though they just made the change.
Fix: sticky reads (same-request, same-connection) or session-scoped routing.
Reads in long-running background jobs. A job processes records over an hour. Each read goes to a different replica with different lag characteristics. Subtle inconsistencies appear in the job's outputs.
Fix: pin the job's connection to a specific replica or to the primary for the duration.
Webhooks that fire from a primary write. A webhook fires when an order is created on the primary. The webhook handler receives the order ID. It reads the order from a replica. The replica hasn't caught up. The handler reports the order doesn't exist.
Fix: pass enough data in the webhook payload that the handler does not need to read back from the database, or always read from the primary when called from a webhook handler.
Counts and aggregations. A count query against a replica returns a number that is briefly inconsistent with the canonical state. A dashboard shows N orders; a moment later it shows N-1 because the replica caught up to a recent delete.
Fix: accept the briefly-inconsistent state, or route count queries to the primary when they are user-visible.
Connection Pooling and Replicas
A practical detail that catches teams: connection pools to replicas behave differently from pools to the primary.
- Per-replica pools — one pool per replica. Each pool sizes independently. Routing decisions pick which pool. Most common.
- Shared replica pool — one pool over all replicas. Round-robin or random selection per checkout. Simpler but harder to drain a specific replica.
- Aware proxy in front of pools — ProxySQL or pgbouncer load-balances and routes. The application sees one logical pool.
The failure mode is exhausting one pool while others are idle. Per-replica pools without health-checking pile requests on a degraded replica. Health-aware routing — even a basic "skip replicas that are >10s behind" — prevents the failure.
Monitoring
Replica routing is impossible to operate without per-replica visibility. Metrics that earn their keep:
- Replication lag per replica, in seconds.
- Query rate per replica.
- Connection pool saturation per replica.
- Error rate per replica.
- Routing decision distribution (% going to primary vs replicas).
The routing decision distribution is the underrated one. "95% of reads went to replicas" is the goal. If you find 60% going to the primary, the routing logic isn't catching enough cases.
When Not To Bother
Read replicas are not always worth the complexity. Skip them when:
- The primary is not the bottleneck. CPU under 50% and query latency healthy — the database isn't your problem.
- Read traffic is dominated by analytics and OLAP. A column-store or warehouse is a better tool.
- The application cannot tolerate any replication lag (financial transactions, real-time pricing). Caching or a different database architecture serves better.
- The team does not have the operational bandwidth to add per-replica observability. Without that, debugging is a nightmare.
For most other workloads — a healthy mix of reads and writes, primarily transactional, growing traffic — replica routing is one of the highest-leverage scaling moves available.
A Practical Starting Setup
For a service with one Postgres or MySQL primary and growing read load:
- Add one or two replicas. Use the database's native replication (streaming, GTID-based).
- Configure the application's connection layer for read/write split with sticky reads.
- Add per-replica lag and connection pool metrics to your observability stack.
- Audit explicit
DB::transaction()blocks — ensure writes happen on the primary connection. - Watch for a quarter. Tighten routing for the specific code paths that have lag-sensitivity bugs.
This is a one-sprint setup that buys substantial read capacity without a database rearchitecture. The complexity comes only when the application grows beyond the simple split.
Figuring out how to route reads to replicas without breaking the user experience? We help teams design read/write split patterns that match their consistency needs. scopeforged.com