<strong>Database replication</strong> is copying data from one database (primary) to one or more databases (replicas) to improve availability, performance, and disaster recovery.
The Library Branches Analogy
Instead of everyone going to the main library, you have branches with copies of books. Closer, faster access. If main library burns down, branches still have the books.
Main Library (Primary)
Original books
Branch Libraries (Replicas)
Copies of books
Readers (Users)
Access nearest branch
Writes
Sync Data
Reads
Write to primary
INSERT INTO users VALUES (...)
Primary logs change
Binary log records the write
Replicas pull changes
Replicas read binary log
Replicas apply changes
Execute same INSERT on replicas
Read from replicas
Distribute read queries across replicas
Wrong
"Replication means instant consistency"
Correct
<strong>Replication has lag</strong>. Replicas are eventually consistent, not immediately. Writes to primary take milliseconds to propagate to replicas.
Social media app:
1 primary database (handles all writes)
5 read replicas (handle all reads)
User posts photo → write to primary
Users view feed → read from nearest replica
If primary fails → promote replica to primary