Memcached Do you really understand LRU (4)

Source: Internet
Author: User

As is known to all, Memcached uses the LRU (Least recently used least recently used) algorithm to reclaim the cache, freeing the data that belongs to LRU out of memory to make room for loading additional data. So how is the least recently used algorithm of memcached implemented? Perhaps a lot of people will answer: not in the case of full memory, the most recently used key is replaced, and then insert a new Key-value key value pair? In fact, the following we have to analyze the internal implementation of memcached LRU, before analyzing LRU, let us first understand the internal principle of memcached.

Memory allocation for memcached

Memcached is the use of slab allocator mechanism to allocate and manage memory, first of all, we must understand three concepts:

Slab
A collection of the same chunk size, a slab contains multiple page, and a page (the default is 1M) contains multiple chunk,chunk where the data is eventually stored.
PAGE  
page default is 1M, and a page contains multiple chunk.
Chunk
By default the size of chunk is: 96, with the specified growth factor change (parameter-F <factor>)

Slab allocator mechanism is allocated to memcached memory, cut into a number of Slab, each Slab the size of the page is 1M by default, that is, if a Slab occupies 50M of memory, in the default case there are 50 page. When the memcached boot is not active slab, when inserting data, if chunk not enough to apply for slab, once allocated memory will not be released, re-use.

Specific:


memcached Caching principle memcached Select the slab (such as) that best fits the data size, depending on the size of the data received. Memcached holds a list of idle chunk within slab, selects chunk based on the list, and caches the data in it.

Memcached of memory Waste:

By caching 100 bytes of data into a 128-byte chunk, the remaining 28 bytes are wasted (for example):


After understanding some of the memcached basic concepts above, let's talk about the memcached LRU principle.

Memcache LRU:

First we need to know:

the LRU algorithm for 1,memcached is executed for each slab, not for the whole.

2, the data will only exist in the specified slab, even if the slab is full, and the larger slab has space, this situation will also execute the LRU algorithm, because the data will not be stored in the larger slab.


A slab will have multiple page, a page default is 1M, start memcached will pre-allocate 1M, when the 1M of data is full, if there is new data come in, then will be reassigned a page to this slab, but memcached has a memory limit, If the page cannot be applied, then the LRU algorithm will be used to eliminate the least recently used data for this slab.


One way to effectively mitigate the use of LRU is to:

1. Avoid large objects

If there are only a few large objects on the system, it will waste memory space, because the Slab application page is not free memory, and the individual large objects will cause slab to request memory resources and can not be fully exploited.

2, adjust the growth factor

Adjust the growth factor to the needs of your project and make the memory fully available.


All in all, in short, let the memory be fully utilized. Avoid the chunk of slab in the vacancy.


Reference Documentation:

http://blog.charlee.li/memcached-001/

http://blog.charlee.li/memcached-002/

http://blog.charlee.li/memcached-003/

http://blog.charlee.li/memcached-004/

http://blog.charlee.li/memcached-005/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Memcached Do you really understand LRU (4)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.