Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memcache maximum key expiration time

Tags:

memcached

What's memcached's maximum key expiration time?

If I don't provide an expiration time and the cache gets full, what happens?

like image 749
cfischer Avatar asked Sep 13 '09 17:09

cfischer


People also ask

How long does memcached keep data?

You can set expire times up to 30 days in the future. After that memcached interprets it as a date, and will expire the item after said date. This is a simple (but obscure) mechanic. When memcached hits its memory limit, it will automatically expire the oldest / least used entries first.

What is the maximum key size in memcached?

The maximum length of the key in Memcached has a restriction of 250 bytes.

Does memcached support TTL?

Memcached uses lazy expiration to remove keys when the time to live (TTL) expires. This means that the key isn't removed from the node even though it's expired. However, when someone tries to access an expired key, Memcached checks the key, identifies that the key is expired, and then removes it from memory.

What happens when memcache is full?

From the memcached wiki: When the table is full, subsequent inserts cause older data to be purged in least recently used (LRU) order.


2 Answers

You can set key expiration to a date, by supplying a Unix timestamp instead of a number of days. This date can be more than 30 days in the future:

Expiration times are specified in unsigned integer seconds. They can be set from 0, meaning "never expire", to 30 days (60*60*24*30). Any time higher than 30 days is interpreted as a unix timestamp date. If you want to expire an object on january 1st of next year, this is how you do that.

https://github.com/memcached/memcached/wiki/Programming#expiration

But, as you say, if you’re setting key expiration to an amount of time rather than a date, the maximum is 2,592,000 seconds, or 30 days.

like image 112
Paul D. Waite Avatar answered Oct 13 '22 18:10

Paul D. Waite


If you don't provide expiration and cache gets full then the oldest key-values are expired first:

Memory is also reclaimed when it's time to store a new item. If there are no free chunks, and no free pages in the appropriate slab class, memcached will look at the end of the LRU for an item to "reclaim". It will search the last few items in the tail for one which has already been expired, and is thus free for reuse. If it cannot find an expired item however, it will "evict" one which has not yet expired. This is then noted in several statistical counters

https://github.com/memcached/memcached/wiki/UserInternals#when-are-items-evicted

like image 26
Eimantas Avatar answered Oct 13 '22 19:10

Eimantas