When the memory allocated for Memcached is full, it starts evicting the least recently used (LRU) items from the cache to make room for new items. This process is known as "cache eviction" and is an essential behavior of all caching systems, including Memcached.
Memcached uses a simple LRU algorithm to determine which items to evict. The algorithm maintains a linked list of all the items in the cache, with the most recently used item at the head of the list and the least recently used item at the tail. When the cache is full and a new item needs to be stored, Memcached evicts the item at the tail of the list, which is the least recently used item.
You can monitor the number of evictions that occur in your Memcached instance by looking at the
evictions statistic, which is exposed through the Memcached stats command.
Here's an example of how you can retrieve statistics from Memcached using the
In this example, the
evictions statistic indicates that 54,321 items have been evicted from the cache since the Memcached instance was started.
To avoid excessive cache evictions, you should allocate enough memory to your Memcached instance to accommodate your application's working set of data. You should also consider setting appropriate timeouts for your cached items so that they expire from the cache after a certain amount of time or if they become stale.