Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Memcached Basics

Stats and Multi-Get

Read the Memcached stats output (get_hits, evictions, curr_items, bytes) to judge cache health, and use multi-get to fetch many keys in one round trip instead of many.

You cannot tune what you cannot see. Memcached exposes a stats command that reports counters about hits, misses, memory and connections, and these numbers answer most questions about cache health. The second half of this lesson covers multi-get, the feature that turns a hundred slow round trips into one.

The stats command

Send stats and the server replies with a long list of STAT name value lines, ending in END. A trimmed example:

TEXT
STAT pid 4121
STAT uptime 86400
STAT curr_connections 38
STAT cmd_get 1048210
STAT cmd_set 71200
STAT get_hits 986120
STAT get_misses 62090
STAT evictions 0
STAT reclaimed 5230
STAT bytes 41822310
STAT limit_maxbytes 134217728
STAT curr_items 80411
STAT total_items 905120
STAT threads 4
END
Counter Meaning What to watch
uptime Seconds since start A small number means a recent restart: expect a cold cache
curr_connections Open client connections Close to the -c limit is a problem
cmd_get / cmd_set Total get / set commands (cmd_get counts every key of a multi-get) The read-to-write ratio
get_hits / get_misses Lookups that found or did not find the key Hit ratio, see the earlier lesson
evictions Live items thrown out to make room Should be near zero; a rising value means memory pressure
reclaimed Expired items whose space was reused Healthy: expiry is working
bytes / limit_maxbytes Memory used / the -m budget Close together means full
curr_items Items stored now (may include not-yet-reclaimed expired items) Growth over time
total_items Items stored since start Write volume

Other sub-commands go deeper: stats items (per slab class counts and ages), stats slabs (chunk sizes and pages), stats sizes and stats settings. You will use these in Performance and Sizing.

Turning raw stats into insight

The code below parses a stats response like the one above and prints a health report. This is the kind of script you would run from a cron job or a monitoring agent.

Python A small stats health report

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.

Here the server is 99% full and evicting, a sign that it needs more memory or smaller values.

Stats from MiniMemcached

The stand-in keeps the same important counters. Give it a small max_items limit and watch evictions appear.

Python Evictions appear when memory is full

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.

Multi-get: fewer round trips

Every network round trip costs time, even on a fast LAN (roughly 0.2 to 1 ms). If a page needs 50 cached fragments and you fetch them one by one, that is 50 round trips in series, perhaps 25 ms before you have done any work. Memcached lets you ask for many keys at once:

TEXT
get user:1 user:2 user:3 user:4
VALUE user:1 0 5
alice
VALUE user:3 0 3
bob
END

The reply contains only the hits; missing keys are simply absent. Your code compares what came back with what it asked for to find the misses, then loads those from the database, ideally also in one batch query, and stores them.

Python get_multi: one trip for many keys

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.

This pattern, multi-get then one batch load for the misses then store, avoids the N+1 problem for caches: instead of one lookup per item you do one cache trip plus at most one database query.

Why multi-get matters even more with many servers

With a pool of servers, a smart client splits your key list by server and sends one multi-get to each server in parallel. The total time is that of the slowest server, not the sum. We build this in the distributed caching chapter.

The estimate below shows how big the gap is.

Python 50 lookups: serial vs batched

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.

(Real batches also take a little longer to transfer and process, so the true speed-up is smaller, but still large.)

Limits and tips

  • Batch sizes of tens to a few hundred keys are typical. Extremely large multi-gets create big responses and delay other clients on the connection.
  • A multi-get is not atomic: other clients may change keys while it runs.
  • Count hits and misses per key. cmd_get counts each key of a multi-get separately, so ratios stay meaningful.
  • Order is not guaranteed; always look results up by key.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which counters give you the hit ratio?

    hit ratio = get_hits / (get_hits + get_misses).
  2. 2. A rising evictions counter with bytes close to limit_maxbytes suggests:

    Live items are being removed to make room, so memory is too small for the data, or the items are too large.
  3. 3. What does a multi-get reply contain for keys that are not cached?

    Missing keys are simply absent from the reply. Your code compares the keys it asked for with those returned.
  4. 4. Why is fetching 50 keys with one multi-get faster than 50 separate gets?

    The cost is dominated by round trips, not by the work inside the server.

Summary

  • stats reports counters; the key ones are get_hits, get_misses, evictions, bytes, limit_maxbytes and curr_items.
  • Compute rates from snapshots, and alert on evictions and a falling hit ratio.
  • Multi-get fetches many keys in one round trip and returns only the hits.
  • Load the misses with one batch query, then store them: no N+1 pattern.
  • Multi-get is not atomic and should be batched to a sensible size.

Next: The Cache-Aside Pattern