Course contentsShow
← Databases & SQL
Lesson 1276 of 1,95311. NoSQL and Document ModelsFree lesson

Scaling Read Replicas

Distribute read traffic to maintain performance during load spikes.

In this lesson, you will learn how to distribute read traffic across multiple database instances to maintain system performance during sudden load spikes.

The Bottleneck Problem

In a standard database setup, all queries—both writes and reads—target a single "primary" node. When your application experiences a traffic surge, such as a major sale or a viral post, the primary node can become overwhelmed. This leads to increased latency, connection timeouts, and a degraded user experience. While you can optimize your queries or scale vertically (adding more CPU/RAM), there is a physical limit to how much a single machine can handle.

Distributing the Load

To scale horizontally, we introduce Read Replicas. These are secondary instances that contain a near-real-time copy of your primary database. By offloading SELECT operations or document retrieval requests to these replicas, the primary node is left with more resources to process critical writes and updates.

Conceptual Traffic Routing

Your application layer determines where to send a request based on its intent:

-- Application logic pseudo-code
if (query_type == 'WRITE') {
    execute_on(PRIMARY_NODE);
} else {
    // Distribute read requests across a pool of replicas
    target = load_balancer.get_next_available_replica();
    execute_on(target);
}

By adding more replicas, you can theoretically scale your read capacity almost linearly. If your document store is being hammered by millions of users reading product descriptions or social feeds, you can simply spin up three or four additional replicas to share that burden.

Managing the Trade-off

It is important to remember that replicas are not instantly consistent. Because data must be replicated from the primary node to the secondary nodes, there is a tiny amount of "replication lag." If a user updates their profile and immediately navigates to a page served by a read replica, they might see their old data for a few milliseconds. When scaling read replicas, you must balance the need for high availability and performance against the necessity of eventual consistency, ensuring your application handles that minor lag gracefully.

Key Takeaway: Scaling read replicas allows you to offload non-critical queries from your primary node, effectively absorbing traffic spikes by distributing the read volume across a pool of secondary database instances.