Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 37 ยท Performance and Sizing

Hit Ratio and Metrics

Read Memcached's stats output, compute hit ratio, eviction rate and memory use, and learn which numbers to alert on. Includes a runnable stats parser and a hit-ratio simulation.

A cache you cannot measure is a cache you cannot trust. The first question about any cache is: how often does it have the answer? That is the hit ratio. A second, quieter question is: is the cache throwing away things it should keep? That is answered by the eviction counters. Memcached reports both through the stats command, which you can run over a plain TCP connection (echo stats | nc localhost 11211) or through any client library.

This lesson teaches you which fields matter, how to turn raw counters into rates, and what a healthy dashboard looks like. The server cannot run in the live editors, so the examples parse a pasted sample of real stats output and simulate workloads in Python.

The core counters

stats returns lines of STAT name value. Most are cumulative counters since the server started. To get a rate you must take two samples and divide the difference by the elapsed time.

Stat Meaning
cmd_get Total get commands (counted per key, so a multi-get of 10 keys adds 10)
get_hits Gets that found the key
get_misses Gets that did not find the key
get_expired Misses caused by an expired item (subset of misses)
get_flushed Misses caused by a flush_all
cmd_set Total set commands
evictions Items removed to make space before their TTL ended
reclaimed Expired items whose memory was reused (a normal event)
bytes Bytes currently used by items
limit_maxbytes The configured memory limit (-m)
curr_items, total_items Items stored now, and ever
curr_connections Open client connections
listen_disabled_num Times the server stopped accepting connections because it hit the connection limit

The hit ratio is get_hits / (get_hits + get_misses).

Python Parse stats and compute ratios

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reading the numbers

A high hit ratio is good, but it needs interpretation:

  • Why a miss happens matters. get_expired misses are normal (the TTL ended). Misses where the key was never stored are cold misses (first request ever). Misses of keys that were stored and then evicted mean memory is too small: the cache is losing data it wanted to keep.
  • Evictions per second is the number to watch alongside hit ratio. A rising eviction rate with a falling hit ratio is a clear signal to add memory or reduce item sizes. A cache at 100% memory with zero evictions is fine: expired items are being reclaimed.
  • Hit ratio by key family. A global 90% can hide a 20% on the product pages that matter most. Add a metric in your application (for example a counter per key prefix: hit and miss) so you can see which namespaces cause database load.
  • Compare with the miss cost. A hit ratio of 90% means 10% of reads reach the database. At 10,000 reads per second that is 1,000 database queries per second. Going from 90% to 95% halves the database load; going from 98% to 99% does the same. Higher ratios matter more than they look.

How hit ratio depends on cache size

Real traffic is skewed: a small set of keys gets most of the requests. That is why a modest cache works well, and why returns diminish as it grows. The simulation below uses an LRU cache and Zipf-distributed requests over 10,000 keys.

Python Hit ratio versus cache size

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You will see a steep climb at first, then flattening. Caching just a few percent of the keys already serves the majority of requests, and doubling an already large cache adds little. Plot your own hit ratio against memory before buying more RAM; the knee of the curve is the sensible size. Sizing is covered next in Slab Allocator and Memory Sizing.

What to monitor and alert on

  1. Hit ratio per cluster and per key family: alert on a sustained drop of several points.
  2. Evictions per second: alert on sudden growth, which hints at a memory shortage or a flood of new large items.
  3. Memory used versus limit and curr_items.
  4. Latency measured in the client (p50, p99, p99.9). Tail latency is what users feel.
  5. Connections against the limit, and listen_disabled_num should stay at 0.
  6. Network throughput per server; large values saturate NICs before CPUs.
  7. Restarts: uptime going backwards means a restart, which means a cold cache.

For fast diagnosis, stats items and stats slabs show per-slab-class detail (a topic of the next lesson), and stats settings shows the server's configuration. A Prometheus exporter or your cloud provider's agent can collect these and draw dashboards.

Measure from the application side too. The server only knows its own counters; it cannot tell you that a client timed out, retried or skipped a dead server. A client-side counter of hit, miss, error and timeout per request is the metric your users actually experience.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How is hit ratio calculated?

    Hit ratio is the share of gets that found their key.
  2. 2. What does a rising evictions counter together with a falling hit ratio suggest?

    Items are being removed before they expire, so useful data is being thrown away.
  3. 3. Why take two samples of cmd_get to measure traffic?

    Counters only grow, so you subtract an earlier sample to get a per-second rate.
  4. 4. Why track hit ratio per key family and not just overall?

    Averages hide weak spots, and one unfriendly namespace can generate most of your database queries.

Summary

  • stats returns cumulative counters; rates need two samples.
  • Hit ratio = hits / (hits + misses), but evictions tell you whether the cache is too small.
  • Returns diminish as the cache grows because traffic is skewed; find the knee.
  • Alert on hit ratio, evictions, memory, tail latency, connections and restarts.
  • Add client-side metrics per key family for the view your application has.

Next: Slab Allocator and Memory Sizing