Memcached - Stats Slabs
Memcached - Deep Dive with stats slabs
Section titled “Memcached - Deep Dive with stats slabs”The stats slabs command provides a low-level view of Memcached’s memory allocation. It displays detailed statistics for each slab class, such as memory usage, chunk information, and command counters. This is essential for advanced performance tuning and debugging memory-related issues.
Syntax
Section titled “Syntax”This command is run directly on the Memcached server.
stats slabsExample Output
Section titled “Example Output”stats slabsSTAT 1:chunk_size 96STAT 1:chunks_per_page 10922STAT 1:total_pages 1STAT 1:total_chunks 10922STAT 1:used_chunks 1STAT 1:free_chunks 10921STAT 1:free_chunks_end 0STAT 1:mem_requested 71STAT 1:get_hits 0STAT 1:cmd_set 1STAT 1:delete_hits 0STAT 1:incr_hits 0STAT 1:decr_hits 0STAT 1:cas_hits 0STAT 1:cas_badval 0STAT 1:touch_hits 0STAT active_slabs 1STAT total_malloced 1048512ENDUnderstanding the Slab Statistics
Section titled “Understanding the Slab Statistics”The output is divided into per-slab stats (prefixed with slab_id:) and global stats.
Per-Slab Metrics (e.g., for Slab Class 1)
Section titled “Per-Slab Metrics (e.g., for Slab Class 1)”chunk_size: The size in bytes of each chunk in this slab class.chunks_per_page: The number of chunks that fit into a single memory page (typically 1MB).total_pages: The total number of memory pages allocated to this slab class.used_chunksvs.free_chunks: This ratio shows the slab’s utilization. Iffree_chunksis consistently low, this slab is under pressure.mem_requested: The total bytes of user data stored in this slab, not including metadata overhead.get_hits,cmd_set,delete_hits, etc.: Command counters specific to this slab. These are useful for identifying which item sizes are most frequently accessed (i.e., ‘hot’ slabs).
Global Metrics
Section titled “Global Metrics”active_slabs: The number of slab classes that have been initialized and contain at least one item.total_malloced: The total amount of memory in bytes that the Memcached process has allocated from the operating system.
Performance Tuning with Slab Stats
Section titled “Performance Tuning with Slab Stats”By combining information from stats slabs and stats items, you can get a complete picture of cache performance.
Consider this scenario: You observe from stats slabs that slab class 7 has very high get_hits. You then check stats items and see that items:7:evicted is rapidly increasing. This is a clear bottleneck. It tells you that your most frequently accessed items are being evicted due to memory pressure in that specific slab class. The solution is to increase the total memory available to Memcached, which will allow it to allocate more pages to the highly-demanded slab class 7.