Welcome to the Hindi Tutor QA. Create an account or login for asking a question and writing an answer.
Aditya in Web Development

I have the following questions:

  1. Which data will be purged? The one which is older by insertion, or the one which is least recently used? I mean if recently accessed data is d1 which is oldest by insertion and the cache is full while replacing data will it replace d1?

  2. I am using PHP for interacting with memcached. Can I have control over how data is replaced in memcached? Like I do not want some of my data to get replaced until it expires even if the cache is full. This data should not be replaced instead other data can be removed for insertion.

  3. When data is expired is it removed immediately?

  4. What is the impact of the number of keys stored on memcached performance?

  5. What is the significance of -k option in memcached.conf? I am not able to understand what "lock down all paged memory" means. Also, the description in README is not sufficient.

1 Answer

0 votes
Pooja

When memcached needs to store new data in memory, and the memory is already full, what happen is this:

  • memcached searches for a a suitable* expired entry, and if one is found, it replaces the data in that entry. Which answers point 3) data is not removed immediately, but when new data should be set, space is reallocated
  • if no expired entry is found, the one that is least recently used is replaced

*Keep in mind how memcached deals with memory: it allocates blocks of different sizes, so the size of the data you are going to set in the cache plays role in deciding which entry is removed. The entries are 2K, 4K, 8K, 16K... etc up to 1M in size.

All this information can be found in the documentation, so just read in carefully. As @deceze says, memcached does not guarantee that the data will be available in memory and you have to be prepared for a cache miss storm. One interesting approach to avoid a miss storm is to set the expiration time with some random offset, say 10 + [0..10] minutes, which means some items will be stored for 10, and other for 20 minutes (the goal is that not all of items expire at the same time).

And if you want to preserve something in the cache, you have to do two things:

  • a warm-up script, that asks cache to load the data. So it is always recently used
  • 2 expiration times for the item: one real expiration time, let's say in 30 minutes; another - cached along with the item - logical expiration time, let's say in 10 minutes. When you retrieve the data from the cache, you check the logical expiration time and if it is expired - reload data and set it in the cache for another 30 minutes. In this way you'll never hit the real cache expiration time, and the data will be periodically refreshed.

5) What is the significance of -k option in "memcached.conf". I am not able to understand what does "Lock down all paged memory" means. Also description in README is also not sufficient.

No matter how much memory you will allocate for memcached, it will use only the amount it needs, e.g. it allocates only the memory actually used. With the -k option however, the entire memory is reserved when memcached is started, so it always allocates the whole amount of memory, no matter if it needs it or not

...