Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memcached expiration time

Memcached provides a cache expiration time option, which specifies how long objects are retained in the cache. Assuming all writes are through the cache I fail to understand why one would ever want to remove an object from the cache. In other words, if all write operations update the cache before the DB, then the cache can never contain a stale object, so why remove it?

One possible argument is that the cache will grow indefinitely if objects are never removed, but memcached allows you to specify a maximum size. Once this size is reached, memcached uses a least-recently-used (LRU) algorithm to determine which items to remove. To summarise, if a sensible maximum size has been configured, and all writes are through the cache, why would you want to expire objects after a certain length of time?

Thanks, Don

like image 902
Dónal Avatar asked Jun 09 '09 02:06

Dónal


People also ask

How do I set the expiration date on Memcached?

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).

Is Memcached persistent?

When deciding whether to use Redis or Memcached a major difference between these two is data persistence. While Redis is an in-memory (mostly) data store and it is not volatile, Memcached is an in-memory cache and it is volatile.

Does memcache support TTL?

Adding TTL At the same time, you can and largely avoid cluttering up the cache with extra data. Time to live (TTL) is an integer value that specifies the number of seconds until the key expires. Memcached specifies this value in seconds.


2 Answers

Expiration times are useful when you don't need precise information, you just want it to be accurate to within a certain time. So you cache your data for (say) five minutes. When the data is needed, check the cache. If it's there, use it. If not (because it expired), then go and compute the value anew.

Some cached values are based on a large set of data, and invalidating the cache or writing new values to it is impractical. This is often true of summary data, or data computed from a large set of original data.

like image 119
Ned Batchelder Avatar answered Sep 20 '22 02:09

Ned Batchelder


I was curious about this myself, when I first started working with memcached. We asked friends who worked at hi5 and facebook (both heavy users of memcached).

They both said that they generally use something like a 3 hour default expire time as sort of a "just in case".

  1. For most objects, it's not that expensive to rebuild them every 3 hours
  2. On the off chance you've got some bug that causes things to stay cached that shouldn't otherwise, this can keep you from getting into too much trouble

So I guess the answer to the question "Why?" is really, "Why not?". It doesn't cost you much to have an expiration in there, and it will probably only help ensure you're not keeping stale data in the cache.

like image 24
Frank Farmer Avatar answered Sep 20 '22 02:09

Frank Farmer