Serksa
All Concepts
Performance & Scaling

Database Replication

1

What is it?

<strong>Database replication</strong> is copying data from one database (primary) to one or more databases (replicas) to improve availability, performance, and disaster recovery.

2

Think of it like...

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

3

Visual Flow

💾Primary DB

Writes

🔄Replication

Sync Data

📊Replica DBs

Reads

4

Where you see it

1

Write to primary

INSERT INTO users VALUES (...)

2

Primary logs change

Binary log records the write

3

Replicas pull changes

Replicas read binary log

4

Replicas apply changes

Execute same INSERT on replicas

5

Read from replicas

Distribute read queries across replicas

5

Common Mistake

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.

💡 Real-World Example

Social media app:

1

1 primary database (handles all writes)

2

5 read replicas (handle all reads)

3

User posts photo → write to primary

4

Users view feed → read from nearest replica

5

If primary fails → promote replica to primary