Databases intermediate LIVE DEMO
πŸ›’οΈ

Database Replication WAL and Failover Visualization

Step through primary-standby database replication, WAL streaming, replica lag, synchronous commit, failover, split-brain risk, and recovery.

DatabaseReplicationDistributed SystemsWALFailoverVisualization
Interactive Demo

Continue with the guided workspace

Use the paired workspace to connect this guide's mental model with live component state, visible signals, and the outcome each step or scenario is meant to prove.

Database replication is not just a topology diagram. The important lesson is the order of events: a primary appends WAL, replicas receive and replay it, lag changes read freshness and recovery point, and failover must preserve one writable history.

To explore these dynamics dynamically, you can also run the Database Replication simulator sandbox to experiment with async vs. sync mode, replication lag, primary crash, manual promotion, and split-brain scenarios under variable traffic loads.

Mental Model

At its core, database replication is the process of synchronizing state from a single authoritative node (the Primary) to one or more follower nodes (Standbys). Writes must flow through the primary to establish a global order, while reads can be distributed to followers. The primary tracks changes in a Write-Ahead Log (WAL). Replication protocols differ in how they ensure consistency versus performance: synchronous mode prioritizes correctness (waiting for confirmation), whereas asynchronous mode prioritizes write speed (replicating in the background).

Components and Why They Exist

Client App

This component exists to initiate transaction reads and writes toward our database. It routes write operations to the primary node and read queries through the load-balancing read router. In the demo, observe visual query dots traveling across the network: blue for writes, green for reads.

Primary Database

This component exists to accept write operations, establish transactional ordering, and stream logs to replicas. It processes incoming write transactions, appends them locally to the Write-Ahead Log (WAL), and broadcasts log entries to standbys. In the demo, observe the primary database node’s local WAL state transitions from queued to committed.

Sync Standby

This component exists to guarantee high-availability durability without risking data loss if the primary crashes. It receives WAL updates, flushes them to disk, and acknowledges receipt to the primary before the client write finishes committing. In the demo, watch the primary wait for an acknowledgment (ACK) from this node during synchronous commit steps.

Async Standby

This component exists to scale read throughput and provide disaster backup options without increasing client write latencies. It receives replication logs in the background and replays them to catch up with the primary at its own pace. In the demo, watch the replication lag indicator showing the LSN (Log Sequence Number) difference between this node and the primary.

Read Router

This component exists to redirect read traffic away from the primary database, preventing CPU bottlenecks and resource contention. It receives read queries, measures replica lag, and routes client connections to standbys that are reasonably up to date. In the demo, observe read queries (green particles) being routed to standby nodes.

Edge Cases and Misconceptions

  • Synchronous is not β€œinstantaneous”: Synchronous replication does not mean replicas apply the WAL at the exact same instant as the primary. It only guarantees the WAL is written to the standby’s disk before the primary returns success. The standby may still take time to replay those changes into its queryable engine.
  • Stale reads on followers: Replicating asynchronously creates a data-safety window where reads routed to standbys can return stale data. Applications must be design-tolerant of eventual consistency.
  • Split-brain risk: If a network partition occurs and the primary is isolated, replicas might elect a new primary. If the old primary is not fenced, it will continue accepting writes, diverging the dataset.

Common Questions

What is the difference between synchronous and asynchronous replication?

Synchronous replication guarantees that data is written to both the primary and at least one standby replica before a write is confirmed to the client, providing zero data loss but higher write latency. Asynchronous replication allows the primary to acknowledge writes immediately after local commit while streaming the WAL background, yielding lower latency but introducing a risk of data loss if the primary crashes before standbys catch up.

How does replication lag cause stale reads in distributed databases?

Replication lag is the delay between when a write is committed on the primary and when it is replayed on a replica. If reads are routed to an async standby, a client reading right after a write might hit a replica that has not yet replayed that WAL entry, resulting in reading stale (older) data.

What is split-brain and how does fencing prevent it?

Split-brain is a catastrophic state where a network partition or false failure detection causes a standby to promote itself to primary while the old primary is still active and accepting writes. This results in divergent write timelines. Fencing (such as STONITH - Shoot The Other Node In The Head) prevents this by forcefully disabling or isolating the old primary before promoting the standby.

Why is a Write-Ahead Log (WAL) necessary for replication?

The WAL is an append-only sequence of transactional changes written to disk before any actual data pages are modified. Replicas use this log stream to replay operations in the exact same logical order, ensuring state consistency across nodes.

How does synchronous commit impact write latency?

Synchronous commit forces the primary to block the client's connection until the WAL payload is sent, received, and flushed by the sync replica. This introduces network round-trip time (RTT) and disk flush time on the replica into every single write cycle.

What is STONITH and why is it used?

STONITH stands for 'Shoot The Other Node In The Head.' It is a fencing technique where the clustering system uses power management or network switch controls to physically power down or isolate an unresponsive primary node to ensure it cannot write to disk or accept clients.

Can a database have multiple sync standbys?

Yes, clusters can configure multiple sync standbys. Advanced setups (like PostgreSQL's synchronous_commit) can require acknowledgments from a quorum (e.g., any 2 out of 3 replicas) to commit a write.