Measuring Replication Lag
What you'll learn: How to actually measure the delay between when data is written to the primary and when it appears on replicas.
The Core Problem
You know replication lag exists—but how do you quantify it? Without measurement, you can't tell if your lag is 10 milliseconds or 10 seconds. You need concrete numbers to trigger alerts, decide when to promote replicas, or determine if reads are safe.
Two Main Measurement Techniques
1. Sequence Numbers (Position-Based)
Every change in the replication log gets a unique sequence number (like a counter). The primary tracks which position it's at, and each replica reports its current position. The difference tells you how far behind each replica is.
Analogy: Think of a book being copied chapter by chapter. The author finishes Chapter 50, but your copy only has up to Chapter 47. You're 3 chapters behind—that's measurable lag.
Primary position: 1,245,890
Replica position: 1,245,102
Lag: 788 operations behind
2. Timestamps (Time-Based)
The primary attaches a timestamp to each write operation. When the replica applies that operation, it compares the timestamp to current time. This tells you lag in actual seconds or milliseconds.
Write timestamp: 14:32:15.200
Replica applied at: 14:32:17.450
Lag: 2.25 seconds
Why Both Matter
Sequence numbers show how much work is pending. Timestamps show how old the data is. A replica 1000 operations behind might only be 50ms late (fast writes), or 5 seconds late (slow writes). Use both for complete visibility.
Key Takeaway: Measure replication lag using sequence number differences (operations behind) and timestamp comparisons (age of data) to monitor replica health and make informed consistency decisions.