Memcached item size limits and per-slab-class eviction tradeoffs
0 reputation · 14 Nov 2022, 00:27 UTC
0 reputation · 14 Nov 2022, 00:27 UTC
Memcached allocates memory through fixed-size slab classes, and each class maintains its own approximate LRU for eviction. The size of a single item is capped—commonly near 1 MiB by default and adjustable through the documented -I startup option—so larger values must either be split across keys or accommodated by raising the limit.
The unresolved decision is how to size items for a mixed workload that pairs many small, frequently accessed keys with occasional multi-megabyte values. Raising the cap introduces larger slab classes where each allocation can waste more space, and per-class eviction means a hot small key can still be evicted while another slab class retains unused memory. Compile-time and startup settings vary across deployments, so published defaults may not match a given instance.
Specific questions:
35275 reputation · 14 Nov 2022, 06:43 UTC
Raising the maximum item size with -I does not change the LRU eviction order inside the small-item slab classes. Small hot keys continue to be evicted only within their own slab class. The effect of a larger cap is confined to the larger slab classes that are added to accommodate the new size range.
Per-slab waste is best controlled by setting -I just above the largest item you actually need and keeping the slab growth factor modest for the small-item range. Splitting multi-megabyte values into smaller keys is operationally safer for mixed workloads, at the cost of application-level consistency for multi-key reads and writes.
-I. The commonly cited default is near 1 MiB, but the exact default and the maximum allowed value are version and compile-time dependent.-f. The long-standing default is ~1.25. Each class can waste up to roughly the growth factor minus one of its capacity.-I adds larger slab classes; it does not modify the size or eviction behavior of existing small classes.With a mixed workload of many small hot keys and occasional large values, per-class eviction means a hot small key can be evicted while a large-item slab class still has free space. Raising -I therefore does not rescue small keys, it only enables larger items to be stored.
Waste control for mixed sizes:
-I as low as possible while still fitting the largest expected item. An unnecessarily high cap creates extra large slab classes that sit mostly empty.Splitting large values:
-I, keeps large items out of a dedicated large slab class, allows per-chunk LRU and reduces per-slab waste.memcached -Vecho stats slabs | nc host port and review item size ranges, used vs free chunks per class.-I is changed, restart memcached with the new flag and re-run stats slabs to confirm new classes appear and small classes are unchanged.Missing diagnostic detail that changes recommendation: the memcached version and the current stats slabs output showing utilization of the small-item classes versus the large-item classes. Without that, any specific -I and -f values remain assumptions.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.