tags:

views:

47

answers:

1

Hi,

According to the Memcached wiki, the following occurs when an attempt to allocate a value to the cache is executed and the memory limit is reached:

When do expired cached items get deleted from the cache?

memcached uses a lazy expiration, which means it uses no extra cpu expiring items. When an item is requested (a get request) it checks the expiration time to see if the item is still valid before returning it to the client.

Similarly when adding a new item to the cache, if the cache is full, it will look at for expired items to replace before replacing the least used items in the cache.used items in the cache.

The question is, what exactly occurs when it replaces the 'least used items'. Does it simply maintain a rank by access for each key, or does it track access over a period?

For example. I add 2 items to the cache (A and B). The access patterns for A and B are slightly different. I access A 5000 times every hour while I access B once every second. According to the documentation, it seems that if I attempt to add another item (C) after two hours and the max memory allocation is reached, B will be removed. This is because B has only been accessed 7200 times while A has been accessed 10000 times.

Is this correct? If not, is there a sliding time window where access is tracked? For example, A would be removed if the window is 30 minutes because it would have been accessed 0 times in the last 30 minutes while B would have been accessed 1800 times.

Any thoughts?

A: 

It's not an exact LRU. In general, you shouldn't make any assumptions about availability of things in the cache.

An exact LRU would be a lot more expensive as things would be moving around too frequently. Instead, memcached won't place an item at the head of an LRU if it's done so recently.

Dustin
Thanks. When you say 'not an exact LRU' are you confirming that in the scenario described that B would most likely be removed? I was intrigued when I read the FAQ about the deletion algorithm. For example, if an item is accessed disproportionately more than any other item in a short space of time, it could theoretically live in the cache forever without ever being accessed again.
Rohland
There's an LRU list per slab (this is the thing that makes finding space O(1) in practice). A given item won't move to the beginning of the recency used list more than once over a given period of time (imagine a couple keys getting pounded a couple hundred thousand times a second). Eventually, if space is needed at a particular size, items that haven't been used will work their way out.
Dustin