stats — Server Metrics & Slabs
Inspect memory, hit rate, evictions and per-slab allocation.
Code
stats
# STAT pid 1234
# STAT uptime 3600
# STAT curr_connections 12
# STAT total_connections 123
# STAT get_hits 9821
# STAT get_misses 179
# STAT curr_items 1024
# STAT bytes 5242880
# STAT evictions 3
# STAT limit_maxbytes 67108864
# END
# Hit rate = get_hits / (get_hits + get_misses)
stats items
# STAT items:1:number 100 <- slab 1 has 100 items
# STAT items:5:number 50 <- slab 5 has 50 items
stats slabs
# STAT 1:chunk_size 96
# STAT 1:total_chunks 10922
# STAT 5:chunk_size 232
stats sizes # deprecated; may be slow
stats cachedump 5 100 # dump up to 100 keys from slab 5 (debug only)
stats reset # reset counters (NOT data)Explanation
stats gives a snapshot of the server: get_hits/get_misses for hit rate, evictions for memory pressure, curr_items/bytes for usage, limit_maxbytes for the cap. stats items breaks down items per slab class (Memcached uses fixed-size chunk allocator: each class serves a size bucket). stats slabs shows chunk sizes and free chunks per class. Use cachedump to enumerate keys in a slab for debugging (it's not exhaustive and is slow — don't use it in production hot paths). Watch evictions and bytes — if evictions is rising, you're over capacity and need more memory or shorter TTLs.
More Memcached Snippets
set / get — Basic Key-Value
Store and retrieve values by key with expiration and flags.
add / replace / append / prepend
Conditional writes and in-place string concatenation.
cas / gets — Compare-And-Swap
Optimistic locking: update a key only if it hasn't changed since you read it.
flush_all — Invalidate Everything
Logically invalidate all keys instantly (lazy deletion).
Expiration, Eviction & TTL Strategy
Choose TTLs wisely and understand how Memcached evicts under pressure.
Text vs Binary Protocol & Consistent Hashing
Pick the right protocol and shard across servers without reshuffling.