Question: What happens when Memcached is full?

Answer

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 telnet command:

$ telnet localhost 11211 stats STAT pid 12345 STAT uptime 3600 STAT time 1497506190 ... STAT cmd_set 12345 STAT cmd_get 67890 STAT evictions 54321 END

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.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.