How do you set up and monitor replication, and what do you do when replica lag grows?
What the interviewer wants to hear
- Streaming or binlog replication from primary to replicas
- Measure lag in bytes and time
- Use pg_stat_replication or SHOW REPLICA STATUS
- Common causes: heavy writes, slow replica disk, long queries on replica, network
- Alert on thresholds and route reads carefully
Model answer
In PostgreSQL I use physical streaming replication, where replicas receive WAL from the primary; in MySQL, binlog replication, ideally with GTIDs. For monitoring I track lag in both bytes and time. In PostgreSQL pg_stat_replication shows write_lag, flush_lag and replay_lag; in MySQL SHOW REPLICA STATUS shows Seconds_Behind_Source, though a heartbeat table is more reliable. I export these to Prometheus and alert on thresholds. When lag grows I look for the cause: a big batch job or index build on the primary, a slow disk or weaker instance on the replica, long-running queries on the replica conflicting with replay, or network limits. Fixes include breaking big jobs into batches, upgrading replica hardware, enabling parallel apply in MySQL, and making the app stop reading from replicas that lag too much.
Can you answer it under time pressure?
Take a timed medium round and get scored on every answer.
More medium Database Administrator questions
- A query that ran in 50 ms yesterday now takes 20 seconds. Nothing in the code changed. How do you investigate?
- Explain the SQL isolation levels and the anomalies each one allows.
- How would you add a column and an index to a 500 million row table without downtime?
- A busy PostgreSQL table keeps getting bloated. How do you tune autovacuum?
- Walk me through how you read an EXPLAIN ANALYZE plan to find why a query is slow.
- How would you tune the main PostgreSQL memory settings on a new server?
- Which metrics and alerts would you set up for a production PostgreSQL or MySQL database?
- What are the main operational differences between running MySQL (InnoDB) and PostgreSQL?
- Developers send you schema changes and new queries to review. What do you look for?