Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 1 of 6 · Concept · 20 min · revision 1

Leaders, read replicas, sync versus async replication and failover

See how a single leader ships its log to followers, what read replicas can and cannot scale, what synchronous and asynchronous commits really wait for in PostgreSQL, and what a safe failover has to do.

Contentsand lesson path

On this page

  1. Why it matters
  2. One leader, many followers
  3. When does the leader say "committed"?
  4. Failover: promoting a follower
  5. Check your understanding
  6. Apply it
  7. Review prompt

Lessons of Database Replication & Sharding

  1. Leaders, read replicas, sync versus async replication and failover Concept · 20 min
  2. Multi-leader and leaderless quorums Concept · 20 min
  3. Observing replication lag with a read replica Lab · 25 min
  4. Shard keys, hot spots, index tables and materialized views Concept · 30 min
  5. Sharding a social graph table and planning a rebalance Worked example · 30 min
  6. Replication and sharding check Knowledge check · 8 questions

What you will learn

  • Compare synchronous and asynchronous replication and single-leader (read replicas), multi-leader and leaderless topologies by write path, failover behavior and conflict handling.

01. Why it matters

A single database server is easy to reason about: one copy of the data, one place to write and one place to read. It is also one machine that can fail, one CPU and disk budget for every query, and one location that is far away from some users.

Replication keeps a copy of the same data on several servers, called replicas. It buys three things: reads spread over more machines, a standby that can take over when a server dies, and copies that sit closer to distant users.

The price is that the copies are never updated at exactly the same moment. Every replication design answers two questions, and you should be able to answer both for any system you build. When the database says "committed", how many copies hold the write? When a client reads from a copy, how old can that copy be? This lesson covers the most common design, one leader with followers. Lesson 02 covers designs that accept writes in more than one place.

02. One leader, many followers

In single-leader replication (also called primary–replica or leader–follower), one server, the leader or primary, accepts every write. It applies the write and records it in its log. PostgreSQL calls this log the write-ahead log (WAL); MySQL calls it the binary log. The other servers, the followers (also called replicas or standbys), receive the log and replay it in the same order. They pass through the same states as the leader, a little later.

Diagram: writes go to the leader, reads can go anywhere
  writes ──► LEADER ──── log (WAL) ────┬────► FOLLOWER 1 ◄── reads
  reads  ──►   │                        └────► FOLLOWER 2 ◄── reads
               └── applies the write first, followers replay it later

Clients can read from any follower, which is why followers are often called read replicas. A read-heavy service, for example a product catalogue that serves 50 reads for every write, can add replicas until each one carries a comfortable share of the reads. Replicas can also run reports and backups without slowing the leader, and a follower in another region serves nearby users with lower latency.

Replicas do not add write capacity. Every follower must replay every write that the leader accepts. If the leader's disks are saturated by INSERTs and UPDATEs, they stay saturated however many replicas you add, and each replica needs roughly the same disk throughput as the leader just to keep up. When the write rate or the data size outgrows one leader, the next tool is sharding, which lesson 04 covers. A follower is also not a backup: an accidental DELETE on the leader is replayed on every follower within seconds.

03. When does the leader say "committed"?

With asynchronous replication, the leader confirms a commit once the write is safe on its own disk, and it sends the log to followers in the background. Writes are fast, and a slow or broken follower does not slow the leader down. The gap between the leader and a follower is the replication lag. The PostgreSQL documentation says the delay is typically under one second when the standby can keep up with the load. It can grow far larger when a follower is overloaded, the network is slow or a long query on the follower holds replay back.

If the leader dies, writes that had not reached the follower that takes over are lost, even though the clients were told they succeeded. You can estimate that exposure. Suppose a leader commits 1,500 transactions per second, and its lag at the moment of failure is somewhere between 0.2 s and 2 s. Then between 1,500 × 0.2 = 300 and 1,500 × 2 = 3,000 acknowledged transactions may be missing on the new leader. Whether that is acceptable is a business decision. It is written down as the recovery point objective (RPO): the most data, measured in time, that the service may lose.

With synchronous replication, the leader waits for one or more followers to confirm before it reports success. In PostgreSQL, synchronous_standby_names lists the standbys that commits wait for, and synchronous_commit sets how far each of them must get:

PostgreSQL 17: what a commit waits for on the synchronous standby
synchronous_commit   the commit returns once the standby has...
remote_write         received the commit record and written it to its file system
on (the default)     received it and flushed it to durable storage
remote_apply         flushed it and replayed it, so queries there can see it

Two consequences surprise many teams. First, on protects the write if the leader dies, but a read on that standby straight after the commit can still miss the row, because the standby may not have replayed it yet. Only remote_apply makes the change visible there before the client hears "committed", and it adds the replay time to every commit.

Second, every synchronous commit waits at least one network round trip plus the standby's write, flush or replay, and it waits for as long as a synchronous standby is missing. The PostgreSQL documentation warns that such commits "may never be completed" if a synchronous standby crashes. It recommends naming more potential standbys than you need: FIRST 1 (s1, s2) waits for the highest-priority standby that is connected, and ANY 2 (s1, s2, s3) waits for any two of the three, which is a quorum.

Products differ here, so read the documentation of yours. MySQL's semisynchronous replication waits for at least one replica to receive and log each transaction. If no replica answers within a timeout, the source reverts to asynchronous replication until a replica catches up. That keeps writes flowing, but it quietly weakens the durability you planned for, so alert on the mode change. A common middle ground in both products is one synchronous follower nearby, in another availability zone, plus asynchronous followers further away.

04. Failover: promoting a follower

When the leader fails, a follower must become the new leader. This failover has four parts, and each can go wrong.

  1. Detect the failure. A remote observer cannot reliably tell a crashed leader from a slow one or a broken network link, so systems use timeouts. A short timeout fails over during a harmless pause; a long one leaves the service without writes for longer. PostgreSQL itself does not detect failures. Tools such as Patroni, or a managed database service, do it, usually with a consensus store such as etcd so that only one decision wins (see Consensus and distributed transactions).
  2. Choose the follower with the most recent data, to lose as few acknowledged writes as possible. With a synchronous standby, that is the one that confirmed every commit.
  3. Promote it (pg_promote() in PostgreSQL) and redirect clients: update a DNS name, a proxy or the service registry, and make applications reconnect.
  4. Fence the old leader. If it comes back still believing it is the leader, two servers accept writes and the data splits in two, which is called split brain. Fencing makes sure the old leader cannot write any more: cut its power or network, revoke its access to storage, or make it reject writes once it learns of a newer leader. The PostgreSQL documentation calls this STONITH, "shoot the other node in the head".

After an asynchronous failover, writes that only the old leader had are a problem. They are usually discarded, because the new leader may already have reused the same primary key values. If other systems saw those writes (a cache, a search index, an email that was already sent), they now disagree with the database. Rebuild the old leader as a follower of the new one rather than letting it rejoin as it was; PostgreSQL's pg_rewind can speed that up.

Finally, decide what failover is allowed to cost. Automatic failover shortens outages, which improves the recovery time objective (RTO), but it can promote a follower during a brief network blip. Some teams page a human instead for databases where a wrong promotion costs more than a few minutes of downtime.

05. Check your understanding

Practice check · 3 questions

Leaders, followers and commits

Three questions on synchronous commits and what replicas can scale.

Q1. A PostgreSQL primary has `synchronous_standby_names = 'standby_a'`, the default `synchronous_commit = on`, and no other standbys. standby_a loses power. What happens to the next transaction that commits on the primary?
Q2. A primary waits for one synchronous standby with `synchronous_commit = on`. Right after COMMIT returns, the same user's next request reads the new row from that standby. Can the read miss the row?
Q3. A primary's disks are saturated by a heavy stream of INSERTs and UPDATEs, while reads are light. A teammate proposes adding three asynchronous read replicas. What does that do to the write bottleneck?
Practice only: not graded, not saved, not evidence.

06. Apply it

Write a half-page replication decision for a database you know, as the first part of this skill's practice task:

  • How many followers, where they run, and which reads may use them.
  • Synchronous or asynchronous, and the RPO this gives. Estimate the worst case with your peak commit rate and the largest lag you have seen.
  • Who detects a leader failure, who decides to promote, and how the old leader is fenced.
  • What happens to commits if the synchronous standby is lost.

In lesson 03 you will build a primary and a streaming replica with PostgreSQL 17 and watch lag happen. As an extra step after that lab, set synchronous_standby_names on the primary, pause replay on the replica, and compare a commit under on, which returns at once, with one under remote_apply, which waits.

07. Review prompt

Without looking back, explain: why read replicas do not add write capacity; how to estimate the writes at risk in an asynchronous failover; what remote_write, on and remote_apply each wait for; what happens to commits when the only synchronous standby is down; and the four parts of a safe failover, including why the old leader must be fenced.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check