← All Exercises

Measure Replica Lag from Sequence Gaps

intermediatejavascript~25min

Background

Replica lag causes read-after-write failures. Expressing lag in seconds against a threshold makes routing decisions objective.

Problem

Convert primary/replica sequence gaps into seconds using write throughput and report samples whose lag exceeds the threshold.

Input

JSON object: { samples: [{ primarySeq, replicaSeq }], throughputPerSecond, thresholdSeconds }

Output

JSON object: { maxLagSeconds, lagging: number[] }

Sample tests

Sample 1

Input: {"samples":[{"primarySeq":1000,"replicaSeq":1000},{"primarySeq":1200,"replicaSeq":1100}],"throughputPerSecond":100,"thresholdSeconds":2}
Expected: {"maxLagSeconds":1,"lagging":[]}

Sample 2

Input: {"samples":[{"primarySeq":1500,"replicaSeq":1000}],"throughputPerSecond":100,"thresholdSeconds":2}
Expected: {"maxLagSeconds":5,"lagging":[0]}

Your solution

Learning objective

Quantify replication lag in the unit that routing decisions use.