Scrivio Interview library
InternalsFoundations7 min read · updated 13 November 2026

A PostgreSQL or MySQL primary takes a steady stream of writes, and the team needs a new read replica without stopping the primary or locking tables.

How is a new follower built from a consistent snapshot plus the replication log, and how does it know where in the log to start?

We take a consistent physical copy of the primary while writes continue, with pg_basebackup for PostgreSQL or the clone plugin or Percona XtraBackup for MySQL. That copy is tied to a log position: the WAL location where the backup started, or the GTID set or binlog coordinates recorded with the MySQL copy. The follower starts from the copy, asks the primary for the log from that position, replays everything that happened during and after the copy, and then keeps streaming; the primary must retain the log from that point, for example through a replication slot.

Why this question is asked

Adding a read replica sounds like an operations chore, but the way it works explains a lot about replication in general: why a backup taken while data changes can still be consistent, why replicas sometimes cannot catch up, and why the primary's disk sometimes fills up. Interviewers ask it to check that we understand the link between a data copy and a log position. In this answer, we will recap what a replication log is, walk through building a PostgreSQL follower and a MySQL follower step by step, see how the follower knows where to start, and look at what the primary must keep for this to work.

1A Quick Recap: Snapshot Plus Log

Both PostgreSQL and MySQL record every change in a log before or as they apply it. PostgreSQL has the write-ahead log (WAL), a sequence of records addressed by a log sequence number (LSN) such as 4A/C3000028. MySQL has the binary log (binlog), where each transaction also carries a global transaction id (GTID) when GTIDs are enabled.

A follower is built from two parts. The snapshot is a copy of the data files as of some point. The log is every change after that point. Replaying the log on top of the snapshot gives exactly the primary's current data, and continuing to replay keeps it current. The whole trick is that the snapshot and the log must agree on where one ends and the other begins.

2Building a PostgreSQL Follower With pg_basebackup

pg_basebackup connects to the primary over the replication protocol and copies the data directory while the primary keeps running. Files change during the copy, so the copy on its own is not consistent. That is fine, because PostgreSQL records the WAL position at which the backup started, and replaying WAL from that position repairs every torn or outdated page.

Java
# on the new replica host (PostgreSQL 17 or 18)
pg_basebackup -h primary.db -U replicator -D /var/lib/postgresql/18/main \
-X stream -C -S replica3 -R -P
# -X stream : also stream the WAL generated during the copy
# -C -S : create replication slot "replica3" on the primary first
# -R : write standby.signal and primary_conninfo for us
grep 'START WAL' /var/lib/postgresql/18/main/backup_label
# START WAL LOCATION: 4A/C3000028 (file 000000010000004A000000C3)
pg_ctl -D /var/lib/postgresql/18/main start # replays from 4A/C3000028, then streams
Step throughA new PostgreSQL follower joins
New replicaPrimary
BASE_BACKUP (create slot replica3)
start LSN 4A/C3000028 + data files
WAL written during the copy
replay WAL until consistent
START_REPLICATION from last replayed LSN
continuous WAL stream
feedback: received and replayed LSN

7 messages in order. Play them, or step through them one at a time.

The slot keeps the WAL from the start LSN on the primary until the replica confirms it.

Notice the backup_label file: it is how the follower knows where to start. On startup, PostgreSQL sees standby.signal, reads the start location from backup_label, replays the WAL that was streamed alongside the copy, connects to the primary with primary_conninfo, and asks for WAL from the point it has reached. It is not usable for queries until it has replayed past the point where the backup ended, which is when the copy becomes consistent. After that it serves reads while continuing to stream.

3Building a MySQL Follower: Clone Plugin or XtraBackup

MySQL has two common physical methods. The clone plugin (MySQL 8.0.17 and later) copies the InnoDB data from a donor over the network with a single statement, and the recipient restarts with the data and with the GTID set of the copy already recorded. Percona XtraBackup copies the data files while it reads the InnoDB redo log, then prepares the copy so it is consistent, and writes the binlog coordinates and GTID set into a file next to the backup.

SQL
-- on the new replica, clone plugin
CLONE INSTANCE FROM 'clone_user'@'primary.db':3306 IDENTIFIED BY '***';
SELECT @@GLOBAL.gtid_executed; -- 3e11fa47-...:1-9120344, the set contained in the copy
-- with XtraBackup instead: xtrabackup_binlog_info holds the same GTID set;
-- after restoring the files: SET GLOBAL gtid_purged = '3e11fa47-...:1-9120344';
CHANGE REPLICATION SOURCE TO
SOURCE_HOST = 'primary.db', SOURCE_USER = 'repl', SOURCE_PASSWORD = '***',
SOURCE_AUTO_POSITION = 1; -- ask for every GTID not in gtid_executed
START REPLICA;

With GTIDs, the follower does not need to name a file and offset. SOURCE_AUTO_POSITION = 1 tells the source which transactions the replica already has, and the source sends everything else. Without GTIDs, we use the binlog file name and position recorded with the backup instead, through SOURCE_LOG_FILE and SOURCE_LOG_POS. Either way, the position comes from the copy, which is the key idea.

4The Primary Must Keep the Log From That Position

There is a catch. Between the start of the copy and the moment the follower starts streaming, perhaps hours for a large database, the primary keeps writing and keeps recycling old log files. If it removes WAL or binlogs the follower still needs, the follower cannot continue, and the copy must be redone.

PostgreSQL offers two protections. A replication slot, created above with -C -S, makes the primary keep all WAL from the slot's position until the replica confirms it. wal_keep_size keeps a fixed amount of extra WAL, which only helps if the copy is fast. MySQL keeps binlogs for binlog_expire_logs_seconds, 30 days by default, so the window is usually wide, but a busy server with a shorter setting can purge what a slow clone needs. The slot's downside is that a replica which never returns makes the primary keep WAL forever, so slots need monitoring.

5Why Not a Logical Dump?

A logical dump, such as pg_dump or mysqldump, writes the data out as SQL or a compact archive. mysqldump can record the matching binlog position with --source-data (formerly --master-data), so in principle it can seed a replica too. The problem is speed: restoring means re-inserting every row and rebuilding every index, which for a 2 TB database can take a day or more, all while the primary must retain the log. A physical copy moves files at disk and network speed and needs no index rebuild, so it is the normal choice. Logical dumps remain useful for small databases, for moving between major versions, or for copying a subset of tables.

6Follow-up questions

Can the replica serve reads before it has caught up?

Only after it reaches a consistent state, which is the WAL position where the backup ended. From then on, a PostgreSQL hot standby accepts read-only queries while it continues to replay, so it may be behind the primary. Applications that need fresh data should check replication lag before sending reads to a new replica.

Does the backup lock tables on the primary?

No. pg_basebackup takes no table locks; it asks the primary to begin a backup and copies files while writes continue. XtraBackup copies InnoDB without blocking writes, though it may briefly lock for non-InnoDB tables, and the clone plugin may block concurrent DDL on the donor while it runs, depending on the clone_block_ddl setting. Check your version for the exact behaviour.

What happens if the copy takes longer than the log retention?

The follower asks for a WAL segment or binlog that is already gone and stops with an error. That is why we create the slot before the copy starts, or raise retention for the duration of the build.

7Conclusion

In this answer, we saw that a new follower is built from a snapshot and a log. A physical copy, from pg_basebackup, the clone plugin or XtraBackup, is taken while the primary keeps writing, and it records the log position it belongs to: the start WAL location in backup_label, or the GTID set or binlog coordinates. The follower replays from that position and then streams, and the primary must keep the log from there, ideally through a replication slot. Logical dumps can do the same but restore far too slowly for large databases.

What to say in the interviewAbout 90 seconds aloud

Open with

A follower is a snapshot plus the log after it. In PostgreSQL I would run pg_basebackup against the primary with WAL streaming, a replication slot created up front, and the -R option that writes standby.signal and primary_conninfo.

Then, if they want more

The copy is taken while writes continue, so the files themselves are not consistent, but the backup records the WAL position where it started in the backup_label file.

When the replica starts, it replays WAL from that position, which repairs any page that changed during the copy, becomes consistent once it passes the backup's end point, and then connects to the primary and streams from wherever it has got to. In MySQL I would use the clone plugin or XtraBackup.

The clone records the GTID set of the copy in gtid_executed, and XtraBackup writes it to a file I load into gtid_purged, then CHANGE REPLICATION SOURCE TO with SOURCE_AUTO_POSITION fetches every transaction the replica does not have. Without GTIDs I would use the binlog file and position recorded with the backup.

The important operational detail is retention: the primary must keep the log from the copy's position until the follower has it, so in PostgreSQL I create the slot before copying, and in MySQL I check binlog_expire_logs_seconds against how long the copy will take.

I would avoid a logical dump for a big database, because re-inserting rows and rebuilding indexes can take a day.

About this answer

Last updated
13 November 2026
Versions it names
MySQL 8.0, PostgreSQL 17
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How is a new follower built from a consistent snapshot plus the replication log, and how...": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/e9aeadd717/