Course contentsShow
← Databases & SQL
Lesson 1534 of 1,95314. High Availability and DurabilityFree lesson

Single-Leader Replication Patterns

Deploy a primary-replica architecture for read scaling and high availability.

In this lesson, you will learn how to architect a primary-replica system to distribute read traffic and ensure your database survives node failures.

The Single-Leader Architecture

In a single-leader replication pattern, one node is designated as the primary (or leader), and all other nodes are replicas (or followers). The system enforces a strict rule: all write requests must flow through the primary. The primary processes the write to its local storage and then propagates the data changes to all replicas, usually via an asynchronous stream of changes based on the Write-Ahead Log (WAL).

Scaling Reads and Availability

This pattern addresses two major constraints of database management:

  1. Read Scaling: Since the primary is often I/O bound by writes, you can offload heavy SELECT queries to the replicas. Because these replicas are essentially mirrors of the primary’s state, they are perfect for read-only workloads, allowing you to scale horizontally by adding more replicas.
  2. High Availability: If the primary node experiences a hardware failure, the system remains operational by promoting one of the existing replicas to become the new primary. This transition is managed by a failover process, which ensures the application can continue writing data even when the original leader is offline.

Conceptual Workflow

When an application performs a write, the primary commits it locally and broadcasts the change:

-- Client sends write to Primary
UPDATE users SET balance = balance - 50 WHERE id = 123;

-- Primary updates its WAL and notifies replicas
-- Replicas apply the change to their own local state:
-- Replica 1: Applies change (asynchronous)
-- Replica 2: Applies change (asynchronous)

Because this process is typically asynchronous, there is a risk of replication lag. If a client writes to the primary and immediately queries a replica, they might read stale data. However, for many distributed applications, this trade-off is acceptable in exchange for the massive performance gains and fault tolerance provided by the replica set.

Key Takeaway: Single-leader replication enables horizontal read scaling and provides a clear path for failover by funneling all writes to a single source of truth while distributing read loads across synchronized followers.