Replicación de Base de Datos

Líder–seguidor · Réplicas de lectura

Escrito por Darjus Kazakevic · revisado el

Copies of the data on several nodes: more reads, faster failover, and a lag.

Replication keeps the same data on several machines. In the usual setup, one leader (also called primary) accepts writes and streams its change log to followers (replicas), which serve reads. Postgres streaming replication, MySQL binlog replication and MongoDB replica sets all work this way.

Replicas give you more read capacity, a standby to promote when the leader dies, and a copy in another region. The cost is replication lag. With asynchronous copying, a follower is usually milliseconds behind and sometimes seconds. Reading your own write is something you must build.

En esencia — One leader takes writes and followers copy its log to serve reads. Replicas scale reads and survive failures, but async lag means some reads are stale.

Cuándo recurrir a él

Recurre a él cuando

  • Reads outgrow one database server while writes still fit on it
  • A standby must take over when a node or a zone fails
  • Users in another region need low-latency reads

Reconsidéralo cuando

  • Writes are the bottleneck: every replica still applies every write
  • Most reads must see the latest write, so replicas would sit idle
  • As your only backup: a replica copies mistakes too

Conceptos clave

Leader–follower, multi-leader, leaderless
Leader–follower: one node takes writes (Postgres, MySQL). Multi-leader: several nodes take writes and must resolve conflicts. Leaderless: clients write to several nodes and read from several (Cassandra, Amazon's Dynamo).
Synchronous vs asynchronous
Synchronous: the leader waits for a follower to confirm before it acknowledges the write, so failover loses nothing. Asynchronous: it does not wait, so writes are faster but the newest ones can be lost.
Replication lag
How far a follower is behind the leader. Usually under 100 ms within one region. Seconds under heavy writes or long-running queries on the replica. More across regions.
Read-your-writes (Read-after-write consistency)
The promise that a user sees their own changes. Replication does not give it. Read from the leader for a few seconds after a write, or wait until the replica has reached the write's log position.
Failover
Promoting a follower when the leader fails, which is a leader election. Patroni or Amazon RDS detects the failure and switches, typically in 30–120 seconds. The risks: lost async writes, and split brain (two nodes that both act as leader).

Reglas de diseño

Build read-your-writes yourself
After a user writes, send that user's reads to the leader for a few seconds, or wait until the replica reaches the write's position. Send reads that can be a little stale to replicas.
Alert on lag, not only on replica health
A replica 30 seconds behind is up and wrong. Measure lag in seconds, alert on it, and take a replica out of the read pool when it passes your budget.
Default to async; make sync a deliberate choice
Synchronous replication loses no acknowledged write on failover, but every write waits for a follower. If you need it, use one sync follower in the same region and keep the rest async.
Keep backups as well as replicas
A replica copies a mistaken DELETE within milliseconds. Keep point-in-time backups (a base backup plus archived logs), and test restoring them.

Errores habituales

  • A user saves a form, reloads, and sees the old value because the read went to a lagging replica.
  • Picking a random replica per request, so a new comment appears, disappears, then appears again.
  • A long report on a replica that holds back replay, so lag grows to minutes.
  • Treating the replica as the backup, then finding that a dropped table was replicated too.