100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

How Does Load Balancing Across Database Replicas Work?

Learn how round robin, least-connections, and health checks distribute read traffic across database replicas safely.

mediumQ173 of 228 in Database Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

Load balancing across database replicas is the practice of distributing read queries across multiple replica servers using a strategy such as round robin, least-connections, or weighted routing, so no single replica becomes a bottleneck while the primary continues to absorb all writes.

A load balancer (either a dedicated proxy like HAProxy/ProxySQL or driver-level logic in the application) sits in front of the replica pool and picks a target replica for each incoming read using a chosen algorithm: round robin cycles through replicas evenly, least-connections sends traffic to whichever replica currently has the fewest active queries, and weighted routing sends more traffic to larger or closer replicas. Health checks continuously probe each replica’s availability and replication lag, automatically pulling an unhealthy or too-far-behind replica out of rotation so queries never land on a broken or stale server, and adding it back once it recovers.

  • Prevents any single replica from becoming an overloaded hotspot
  • Improves overall read throughput as more replicas are added
  • Health checks automatically avoid failed or lagging replicas
  • Weighted routing lets larger or closer replicas absorb proportionally more traffic

AI Mentor Explanation

A cricket stadium with several ticket-scanning gates uses a steward who directs each arriving fan to the gate with the shortest queue, rather than letting everyone crowd one entrance. If a gate’s scanner breaks, the steward stops sending fans there until it is fixed. Load balancing across database replicas works identically: incoming read queries are directed to whichever replica has the least load, and unhealthy replicas are temporarily removed from rotation.

Step-by-Step Explanation

  1. Step 1

    Deploy a load balancer or proxy

    Place HAProxy, ProxySQL, or driver-level logic in front of the replica pool to route reads.

  2. Step 2

    Choose a balancing algorithm

    Select round robin, least-connections, or weighted routing based on replica capacity and location.

  3. Step 3

    Run continuous health checks

    Probe each replica’s availability and replication lag on a regular interval.

  4. Step 4

    Adjust rotation dynamically

    Remove unhealthy or lagging replicas from the pool automatically, and re-add them once they recover.

What Interviewer Expects

  • Knowledge of common load-balancing algorithms: round robin, least-connections, weighted
  • Understanding of the role of health checks and replication lag monitoring
  • Awareness of proxy tools like HAProxy or ProxySQL versus driver-level balancing
  • Ability to explain why writes bypass the load balancer and go straight to the primary

Common Mistakes

  • Confusing replica load balancing with sharding
  • Forgetting that writes must not be load-balanced across replicas
  • Ignoring replication lag when deciding replica health
  • Assuming round robin is always sufficient regardless of replica capacity differences

Best Answer (HR Friendly)

Load balancing across database replicas means spreading read queries evenly across multiple replica servers so no single one gets overwhelmed, using strategies like round robin or sending traffic to whichever replica is least busy. Health checks constantly monitor each replica so a failed or badly lagging one gets removed from rotation automatically until it recovers.

Code Example

Conceptual weighted replica load balancing
-- Pseudocode: proxy layer picks a replica by current load and weight
function pickReplica(replicas) {
  const healthy = replicas.filter(r => r.isHealthy && r.lagMs < 1000);
  return healthy.sort((a, b) => a.activeConnections - b.activeConnections)[0];
}

-- The chosen replica then serves the actual read:
SELECT * FROM Products WHERE category = 'electronics';
-- executed against the replica with the fewest active connections

Follow-up Questions

  • What is the difference between round robin and least-connections balancing?
  • How would you weight replicas differently based on hardware capacity?
  • What health signals should trigger removing a replica from rotation?
  • How does replica load balancing interact with read-your-writes consistency?

MCQ Practice

1. Which load-balancing strategy sends traffic to the replica with the fewest active queries?

Least-connections routing directs new queries to whichever replica currently has the smallest number of active connections.

2. What should trigger a replica being temporarily removed from the load-balancing pool?

Health checks detect unavailability or unacceptable lag, and the balancer excludes that replica until it recovers.

3. Why do write queries bypass replica load balancing?

Replicas are read-only copies kept in sync via replication, so writes must always go to the primary server.

Flash Cards

What does load balancing across replicas do?Distributes read queries across multiple replica servers to prevent any single one from being overloaded.

Name two load-balancing algorithms.Round robin and least-connections are common strategies.

Why are health checks needed?To detect and exclude unhealthy or lagging replicas from serving reads automatically.

Do load balancers route writes to replicas?No, writes always go to the primary; only reads are load-balanced across replicas.

1 / 4

Continue Learning