Skip to content

Alert on Redis replication lag

Redis slave is behind master — read-after-write returns stale data. No native alert, you need an external one.

Recipe

bash
#!/usr/bin/env bash
# Run on the standby Redis host. Exposes `200 ok` / `500 lag` over HTTP
# so an external monitor can poll it.

LAG_LIMIT=${1:-10}    # seconds
HOST=${REDIS_HOST:-127.0.0.1}
PORT=${REDIS_PORT:-6379}

LAG=$(redis-cli -h "$HOST" -p "$PORT" INFO replication \
       | awk -F: '/master_last_io_seconds_ago/ {gsub(/\r/,""); print $2}')

if [ -z "$LAG" ] || [ "$LAG" -gt "$LAG_LIMIT" ]; then
  printf 'HTTP/1.1 500 OK\r\nContent-Type: text/plain\r\n\r\nlag=%s\n' "$LAG"
else
  printf 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nok lag=%s\n' "$LAG"
fi

Same thing in Enterno.io

Expose the script as /redis-replication-status via nginx + fcgiwrap (or a systemd socket) and point an Enterno HTTP monitor at it. Alert rule "not 200" is enough.

Set up HTTP monitor → ← All recipes

Related recipes