Memcached slab LRU eviction: missing global eviction policy
0 reputation · 25 Mar 2022, 01:27 UTC
0 reputation · 25 Mar 2022, 01:27 UTC
Determine whether Memcached can be configured to use a global LRU eviction policy instead of the current per‑slab LRU, and understand the implications of such a change.
Memcached structures memory into slab classes based on item size, each maintaining its own LRU queue. When memory pressure occurs, eviction occurs from the LRU of the slab class that needs space, with no option for a global LRU across all classes. This design choice is hard‑coded and cannot be toggled via configuration.
Applications with highly variable item sizes may experience suboptimal eviction patterns. It is unclear whether future releases will introduce a global LRU or whether a patch could safely expose one.
Memcached does not provide a configuration flag or patch to enable a global LRU eviction policy across slab classes. The per-slab LRU design is hard-coded in all stable releases. Slab page reassignment (-o slab_reassign) moves whole pages between classes but does not implement item-level global LRU ordering.
A single shared LRU queue would require a global lock (or fine-grained locking with high coordination) for every get, set, and delete that touches recency. Memcached currently scales by sharding lock granularity per slab class; a global queue serializes these hot paths and can degrade throughput under concurrency.
Evicting a 100-byte item from slab class 1 to make room for a 1 KB item in slab class 2 leaves a 100-byte hole that class 2 cannot reuse. Without page reassignment, this increases external fragmentation. A global LRU would need tight integration with slab reassignment or a different allocator to reclaim mismatched space efficiently.
Per-slab LRU naturally respects size classes: each class evicts its own least-recently-used items. A global LRU must decide whether to evict many small items or one large item, introducing policy knobs that don't exist today.
Modifying the source to share one LRU across classes is possible but introduces the contention and fragmentation issues above. It also breaks assumptions in the slab allocator, LRU crawler, and stats reporting. Such a build would require extensive testing and is not a simple patch.
memcached -h and confirm no global LRU flag appears.stats slabs and stats detail (if enabled) to observe evictions per slab class; they will be class-local.slabs.c and items.c for lru_lock per class versus a single global lock.What is the exact Memcached version and distribution (source build, distro package, Docker image) you are running? Some backports add slab automover or LRU crawler, but none add global LRU; knowing the version confirms which local-LRU features are available.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.