How to Mock Replica Lag Monitoring in Python

Simulates database replica lag with a mock monitor class that generates realistic lag metrics and health statuses.

Easy Python 3.9+ Aug 9, 2026 Database scaling & optimization 12 views 0 copies

Python code

40 lines
Python 3.9+
import time
import random
from datetime import datetime, timedelta

class MockReplicaLagMonitor:
    def __init__(self, replicas=3, base_lag=0.5, jitter=0.2):
        self.replicas = [f"replica-{i}" for i in range(replicas)]
        self.base_lag = base_lag
        self.jitter = jitter
        self.last_write = datetime.now()

    def simulate_write(self):
        self.last_write = datetime.now()
        return f"Write applied at {self.last_write.isoformat()}"

    def check_lag(self):
        results = {}
        for replica in self.replicas:
            lag = max(0.0, self.base_lag + random.uniform(-self.jitter, self.jitter))
            replica_time = self.last_write - timedelta(seconds=lag)
            results[replica] = {
                "lag_seconds": round(lag, 3),
                "replica_last_sync": replica_time.isoformat(),
                "status": "healthy" if lag < 1.0 else "warning" if lag < 2.0 else "critical"
            }
        return results

    def monitor_loop(self, iterations=3, interval=1):
        print(self.simulate_write())
        for i in range(iterations):
            time.sleep(interval)
            statuses = self.check_lag()
            print(f"\nCheck {i + 1}:")
            for replica, info in statuses.items():
                print(f"  {replica}: lag={info['lag_seconds']}s, "
                      f"status={info['status']}, sync={info['replica_last_sync'][:19]}")

if __name__ == "__main__":
    monitor = MockReplicaLagMonitor(replicas=3, base_lag=0.7, jitter=0.5)
    monitor.monitor_loop(iterations=3, interval=1)

Output

stdout
Write applied at 2024-01-01T12:00:00.000000

Check 1:
  replica-0: lag=0.712s, status=healthy, sync=2024-01-01T11:59:59
  replica-1: lag=0.943s, status=healthy, sync=2024-01-01T11:59:59
  replica-2: lag=0.456s, status=healthy, sync=2024-01-01T11:59:59

Check 2:
  replica-0: lag=1.234s, status=warning, sync=2024-01-01T11:59:58
  replica-1: lag=0.567s, status=healthy, sync=2024-01-01T11:59:59
  replica-2: lag=1.891s, status=warning, sync=2024-01-01T11:59:58

Check 3:
  replica-0: lag=0.345s, status=healthy, sync=2024-01-01T11:59:59
  replica-1: lag=2.145s, status=critical, sync=2024-01-01T11:59:57
  replica-2: lag=0.789s, status=healthy, sync=2024-01-01T11:59:59

How it works

The MockReplicaLagMonitor class simulates the behavior of a real replica lag monitoring system by tracking the timestamp of the last write and computing per-replica lag as a random value around a configured base. random.uniform adds realistic jitter to the lag, while timedelta calculates each replica's last sync time by subtracting the lag from the primary write timestamp. The health classification uses simple thresholds to categorize lag as healthy, warning, or critical, mimicking production alerting logic. The monitor_loop method demonstrates a typical polling pattern with time.sleep between checks, which mirrors how real monitoring agents sample metrics on an interval.

Common mistakes

  • Using >= instead of < for status thresholds can misclassify lag values
  • Forgetting to bound lag with `max(0.0, ...)` can produce negative sync times
  • Assuming `isoformat()` truncation works when slicing wrong index ranges

Variations

  1. Use a list of datetimes instead of a single `last_write` timestamp to simulate per-replica write offsets
  2. Add a `get_metrics()` method that returns a flattened dict for direct export to monitoring systems

Real-world use cases

  • Unit testing alerting rules before deploying them to production monitoring stacks like Prometheus or Datadog.
  • Load testing dashboards and notification pipelines with synthetic replica lag data.
  • Simulating replica failover scenarios in integration tests for database failover automation scripts.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Database scaling & optimization

Related tutorials and quizzes for this topic.