Database Replication Explained: Binlog, WAL and Replicas

Replicas stay in sync by replaying a log of changes. Statement vs row-based logs, the MySQL binlog, PostgreSQL WAL streaming and replica lag.

In this chapter

The replication log: changes other servers can replay

More on Logs, versions and the buffer pool

Keep learning

Overview

chapters · logs, versions and the buffer pool The replication log: changes other servers can replay The redo log lets one server repair itself after a crash. A replication log lets other servers, and future you, replay what happened. Same changes, different reader.

1. Physical log versus logical log

One change, UPDATE items SET price to 12 for id 7, written three ways. Physical: page 812, offset 96, bytes 0A to 0C, readable only by the same engine version with the same page layout. Logical row: table items, id 7, price 10 to 12, readable by any server that knows the table. Logical statement: the SQL text itself, readable anywhere but only safe if deterministic. page 812 · offset 96 bytes 0A → 0C logical: row items id=7 price 10 → 12 logical: statement the SQL text above reader: same engine, same version, same pages reader: any server that knows the table reader: anyone, but only safe if deterministic ← more exact, less portable more portable, more fragile → Physical versus logical Physical (page-level) Logical (row or statement) Continuously, before each page change Per transaction, at commit Can repair a torn or half-written page ✓ yes Replica may differ in version or layout Rolled-back work Logged too Never written Kept for Until the next checkpoint (plus archive) Days, for replicas and restores

2. Statement format versus row format

A statement log is tiny but is re-executed, so it must give the same answer everywhere: random values or LIMIT without ORDER BY break that. A row log ships exact before and after images: safe and readable by change-data-capture tools, but one million-row UPDATE writes a million images. Mixed logs statements and switches to rows when a statement is unsafe.

3. Replicas and point-in-time recovery

log: t1 t2 t3 t4 … replica 1: applied to t4 replica 2: applied to t2 (lag) backup 02:00 replay log 02:00 → 14:06 state at 14:06 Point-in-time recovery: stop just before the mistake, or skip it by its transaction id. One log, two uses: keep copies in step, and travel back to any moment after a backup.

4. Two logs must agree: two-phase commit

If the storage log and the replication log are separate files, a crash between their writes can leave them on different sides of a transaction. The fix is a fixed order: prepare in the storage log, write the replication log, then commit. On restart, a PREPARED transaction commits only if the replication log has it. It only holds if both logs are flushed at commit. That is two disk syncs per commit, softened by group commit (commit cost).

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.