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

From the memcached wiki:

When the table is full, subsequent inserts cause the old data to be cleared at least by the recently used (LRU) order.

I have the following questions:

  • What data will be deleted? The one that is older than the insert, or the one that has been used recently? I mean, if the recently received data was d1 , which is the oldest insertion path, and the cache is full when replacing data, will it replace d1 ?

  • I use PHP to interact with memcached. Can I control how data is replaced in memcached? It’s as if I don’t want some of my data to be replaced before they expire, even if the cache is full. This data should not be replaced, and other data may be deleted for insertion.

  • When the data has expired, is it deleted immediately?

  • What is the effect of the number of keys stored in memcached performance?

  • What is the value of the -k option in memcached.conf ? I can’t understand what it means to “lock all memory”. Also, the description in README is not enough.

+7
source share
1 answer

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

  • memcached searches for a suitable * expired record, and if it is found, it replaces the data in that record. At what answer point 3) the data is not deleted immediately, but when it is necessary to install new data, the space is redistributed.
  • If no expired record is found, then the replaced one is used.

* Keep in mind how memcached deals with memory: it allocates blocks of different sizes, so the size of the data that you are going to install in the cache plays a role in determining which record is deleted. Records - 2K, 4K, 8K, 16K ... etc. up to 1M.

All this information is in the documentation so just read it carefully. As @deceze notes, memcached does not guarantee that data will be available in memory, and you should be prepared for a miss schooner. One interesting approach to avoid the storm is to set the expiration time with some random offset, for example, 10 + [0.10] minutes, which means that some items will be saved for 10 and others for 20 minutes (the goal is that not all items expire at the same time).

And if you want to store something in the cache, you need to do two things:

  • A warm-up script that requests a cache for loading data. So it's always used recently
  • 2 expiration dates for the goods: one real time of expiration, say, after 30 minutes; the other is cached along with the item - the logical expiration time, say, after 10 minutes. When you extract data from the cache, you check the logical expiration time, and if it expired, reload the data and set it in the cache for another 30 minutes. Thus, you will never hit the expiration time of the real cache, and the data will be updated periodically.

5) What is the value of the -k option in "memcached.conf". I'm not able to understand what “Block all memory” means. Also the description in README is also not enough.

No matter how much memory you allocate for memcached, it will only use the right amount, for example. it allocates only used memory. However, with the -k option, all memory is saved when memcached starts, so it always allocates the entire amount of memory, regardless of whether it needs it or not.

+16
source

All Articles