Skip to content

Memcached — alert when eviction rate is too high

Memcached fills up and starts evicting keys under load; the app cache-misses and hammers the DB. Want an evictions/min threshold.

Recipe

bash
#!/usr/bin/env bash
# Reads "stats" via netcat; computes delta between two probes 60s apart.
HOST="${MC_HOST:-127.0.0.1}"
PORT="${MC_PORT:-11211}"
THRESHOLD="${EV_THRESHOLD:-1000}"
STATE=/var/tmp/mc-evictions.last

CUR=$( (echo stats; sleep 0.2; echo quit) | nc -q1 "$HOST" "$PORT" | awk '/^STAT evictions/ {print $3}')
NOW=$(date +%s)
[ -z "$CUR" ] && { echo "no-data"; exit 1; }

if [ -f "$STATE" ]; then
  read -r LAST_VAL LAST_TS < "$STATE"
  DT=$(( NOW - LAST_TS ))
  [ "$DT" -gt 0 ] && {
    DELTA=$(( CUR - LAST_VAL ))
    PER_MIN=$(( DELTA * 60 / DT ))
    [ "$PER_MIN" -ge "$THRESHOLD" ] && echo "high $PER_MIN" || echo "ok $PER_MIN"
  }
fi
echo "$CUR $NOW" > "$STATE"

Same thing in Enterno.io

Expose the endpoint + Enterno HTTP monitor with "ok" rule. Pair with monitor-history dashboard to spot when eviction-rate spiked alongside a deploy.

Set up HTTP monitor → ← All recipes

Related recipes

long_query_time = 1, slow_query_log enabled. You need to know when the slow-query rate per minute suddenly jumps (a deploy broke an index, ORM went N+1).

The CDN cache_status header (cf-cache-status or x-cache) suddenly returns MISS on more than 30% of requests — origin load + bandwidth bills both spike.