Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is data replaced in memcached when it is full, and memcache performance?

From the memcached wiki:

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

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.

like image 763
samarth Avatar asked Jun 27 '12 08:06

samarth


People also ask

What happens when Memcached is full?

First, when memcached gets full, it will start removing items from the cache using the Least Recently Used algorithm. Second, you can have multiple instances of memcached running, adding them gets complicated because it will change the hashing algorithm used to determine which cache the data is in.

How does memcache store data?

How does Memcached work? Unlike databases that store data on disk or SSDs, Memcached keeps its data in memory. By eliminating the need to access disks, in-memory key-value stores such as Memcached avoid seek time delays and can access data in microseconds.

How long does Memcached keep data?

The expiration time in Memcached is in seconds. For instance, the default value is 10800 seconds. But, it can have a maximum value of 2592000 seconds that is, 30 days.

What is the difference between Memcache and Memcached?

They both have very basic difference while storing value. Memcache mostly considers every value as string whereas Memcached stores it value's original type.


1 Answers

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

like image 53
Maxim Krizhanovsky Avatar answered Oct 06 '22 00:10

Maxim Krizhanovsky