DEV Community

Sreekar Reddy
Sreekar Reddy

Posted on β€’ Originally published at sreekarreddy.com

πŸ“‹ Database Replication Explained Like You're 5

Copying data to multiple servers

Day 122 of 149

πŸ‘‰ Full deep-dive with code examples


The Backup Copy Analogy

Important documents:

  • One copy at home
  • One copy at the bank
  • One copy at your lawyer's

If your house burns down, you still have copies!

Database Replication keeps copies of your database in multiple places!


Why Replicate?

Safety:

  • If one server fails, another replica may still have the data (and can take over, depending on your setup)
  • Can reduce data-loss risk, but replication is not a full backup strategy by itself

Speed:

  • Users far away can read from a nearby replica
  • Can reduce read latency for globally distributed users

Availability:

  • One server down β†’ Others handle requests
  • Less downtime for users (depending on failover and client behavior)

Types of Replication

Primary-Replica:

Primary (writes) ──→ Replica 1 (reads)
                └──→ Replica 2 (reads)
Enter fullscreen mode Exit fullscreen mode
  • One leader accepts writes
  • Replicas get copies, handle reads

Multi-Primary:

  • Multiple servers accept writes
  • More complex, handles conflicts

The Trade-offs

Consistency:

  • Replicas might be slightly behind
  • User writes, then reads from replica = might see old data

Complexity:

  • Managing multiple servers
  • Handling conflicts if two writes happen

Common Uses

  • Global apps β†’ Replicas in each region
  • High-traffic sites β†’ Spread read load
  • Disaster recovery β†’ Backup in different location

In One Sentence

Database Replication copies your data to multiple servers for safety, speed, and availability when one server isn't enough.


πŸ”— Enjoying these? Follow for daily ELI5 explanations!

Making complex tech concepts simple, one day at a time.

Top comments (0)