# Reads Are Slow and Someone Says 'Just Shard It': Why Read Replicas Come First

Our dashboard pages were getting slower every week. Database CPU sat at 85% for most of the working day, and the p95 latency for our reporting pages had doubled in two months.

In the design meeting, a senior developer said what many teams say at this point: "We've outgrown one database. Let's shard it."

It sounded like the grown-up answer. It was also the wrong first step. This post walks through what we did instead, why read replicas usually come before sharding, and how to answer this question in an interview without falling into the trap.

## First, look at what the database is actually doing

Before anyone draws a sharding diagram, answer one question: **is the load coming from reads or from writes?**

On PostgreSQL, `pg_stat_statements` tells you which queries take the most total time. On MySQL, the slow query log and Performance Schema do the same. On AWS RDS or Aurora, Performance Insights shows the top SQL and wait events.

```sql
-- PostgreSQL: top 10 queries by total time
SELECT calls,
       round(total_exec_time::numeric, 0) AS total_ms,
       round(mean_exec_time::numeric, 1)  AS mean_ms,
       left(query, 80)                    AS query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;
```

When we ran this, the picture was clear. About 90% of the time was spent on reads: reporting queries, product pages, and search. Writes were small and steady. Our problem was not "too much data for one machine". It was "too many reads hitting one machine".

## Why sharding was the wrong first move

Sharding splits your data across several databases, usually by a key such as customer ID. It is a powerful tool, but it is expensive:

- **You must choose a shard key**, and a bad choice creates hot shards that are just as overloaded as before.
- **Cross-shard queries get hard.** Joins, reports across all customers, and global unique constraints now need extra work.
- **Transactions across shards** need careful design, or you give them up.
- **Migrations, backups and schema changes** now happen on many databases, not one.
- **It is hard to undo.** Once data is split and the application depends on the routing, going back is a project of its own.

For a read-heavy workload, all of that pain buys you very little, because the cheap fixes have not been tried yet.

## What we did instead (in two weeks)

### 1. Fixed the worst queries

Three of the top ten queries were missing an index. One report was doing a full table scan on every page load. Adding the right indexes cut database CPU by about 15 points on its own. This is always the cheapest win.

### 2. Cached the hottest pages

Product pages changed a few times a day but were read thousands of times an hour. We put them in Redis with a short TTL and cleared the key on update. That removed a big chunk of repeated reads.

### 3. Added two read replicas

A read replica is a copy of the primary database that receives changes through replication and serves read-only queries. On RDS or Aurora this is a few clicks or a few lines of Terraform:

```hcl
resource "aws_db_instance" "reporting_replica" {
  identifier          = "app-db-replica-1"
  replicate_source_db = aws_db_instance.primary.identifier
  instance_class      = "db.r6g.xlarge"
  publicly_accessible = false
}
```

We sent reporting, search and dashboard queries to the replicas.

### 4. Kept the right reads on the primary

This is the part teams forget. Replication is usually **asynchronous**, so a replica can be a little behind. If a user places an order and the next page reads from a replica, they may not see their own order.

So we kept two kinds of traffic on the primary:

- All writes.
- Reads that must see the latest data, such as "show my order right after I place it" (read-your-own-writes).

In the application, this was a simple rule in our data access layer: a read-only connection pool for replicas, and the primary pool for writes and for reads inside a request that just wrote data.

### 5. Watched replica lag

We added an alert when replica lag went above a few seconds. On PostgreSQL you can check it on the replica:

```sql
SELECT now() - pg_last_xact_replay_timestamp() AS replica_lag;
```

On RDS, the `ReplicaLag` CloudWatch metric does the job. If lag grows, the application can fall back to the primary for sensitive reads until the replica catches up.

## The result

Primary CPU dropped from about 85% to around 35%. Reporting pages went back to their old speed. And sharding moved off the roadmap for at least another year, which saved months of engineering work and a lot of risk.

## When sharding really is the answer

Read replicas do not fix everything. Sharding (or a database built for horizontal scale) starts to make sense when:

- **Writes** are too much for the biggest primary you can reasonably run.
- **Data size** no longer fits well on one node, even after archiving old data.
- **Tenants need isolation**, for example large customers that must be on separate databases.

Even then, consider the steps in between: partitioning big tables, archiving cold data, vertical scaling, or moving one heavy feature to its own database.

## The interview angle

When an interviewer asks, "Your database reads are slow. What do you do?", the trap answer is "Shard it." A strong answer sounds like this:

1. "First I'd check whether the load is reads or writes, using pg_stat_statements or Performance Insights."
2. "I'd fix the slowest queries and missing indexes."
3. "I'd add caching for hot, rarely changing data."
4. "Then read replicas for read-heavy traffic, keeping writes and read-your-own-writes on the primary."
5. "I'd monitor replica lag and fall back to the primary when it grows."
6. "I'd shard only when writes or data size are too much for one primary, because sharding is hard to undo."

Interviewers are listening for the order of your steps. Jumping straight to sharding tells them you reach for the most complex tool first.

## Key takeaways

- Measure first: are you read-bound or write-bound?
- Indexes and caching are the cheapest wins.
- Read replicas scale reads with little application change.
- Replication lag is real; keep read-your-own-writes on the primary.
- Sharding is a one-way door. Use it when writes or data size demand it, not as the first fix.

In 14+ years, I have seen far more teams hurt by sharding too early than by sharding too late.

---

> **Let's connect: Free 15-min Cloud & DevOps Interview Guidance Call 👆 link in bio**
> ([topmate.io/cloudtechbydevang](https://topmate.io/cloudtechbydevang/2358399))
