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
- Primary receives a write (INSERT, UPDATE, DELETE)
- Primary records the change in its replication log
- Replicas continuously poll or stream from this log
- Each replica applies log entries in order to stay synchronized
- 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.