Course contentsShow
← System Design
Lesson 206 of 1,9196. Database Scaling: Read ReplicasFree lesson

Replication Logs and Mechanisms

How databases use write-ahead logs, binlogs, or similar mechanisms to propagate changes from primary to replicas.

Replication Logs and Mechanisms

What you'll learn: How databases actually propagate changes from the primary server to replicas using internal logs.

The Core Idea

When you write data to a primary database, that change needs to reach all replica servers. But how does the primary tell replicas what changed? It uses replication logs—special files that record every data modification in a sequential, ordered format.

Think of it like a chef (primary) calling out every order modification to line cooks (replicas) through a ticket system. The tickets are the log entries, and they must be processed in exact order.

Common Log Mechanisms

Write-Ahead Log (WAL):
Before the database writes data to disk, it first writes the change to a log file. Replicas read this log and replay the same changes. PostgreSQL uses WAL extensively.

Binary Log (binlog):
MySQL's approach—records all data-modifying statements or row changes in binary format. Replicas connect, request the binlog stream, and apply changes locally.

Oplog (Operations Log):
MongoDB's version—a capped collection storing every write operation. Replicas tail this log and replay operations in sequence.

How It Works

  1. Primary receives a write (INSERT, UPDATE, DELETE)
  2. Primary records the change in its replication log
  3. Replicas continuously poll or stream from this log
  4. Each replica applies log entries in order to stay synchronized
  5. Replicas track their position in the log (like a bookmark)

The log ensures ordering and durability—if a replica crashes, it can resume from its last known position without missing changes.

Key Takeaway:
Replication logs are the ordered record of all database changes that replicas read and replay to stay synchronized with the primary—acting as the single source of truth for propagating writes.