Simple grooming memcached Working principle/Workflow/Optimization Recommendations

Source: Internet
Author: User
Tags memcached

First, memcached working principle
Basic concept: Slab,page,chunk.
Slab, is a logical concept. It is preprocessed at the start of the memcached instance, and each slab corresponds to a chunk size, which means that different slab have different chunk size. How many slab are assigned is determined by the parameter-F (growth factor) and-N (chunk minimum size).
page, which can be understood as memory pages. The size is fixed at 1m. Slab requests the page from the system when the request is stored, and the page is cut by chunk size.
Chunk is the smallest unit in which user data is saved. The user data item (including Key,value) is eventually saved to the chunk. The chunk specification is fixed, and if the user data is put in, then the remainder will not be used for other purposes.


Second, memcached work flow
Memcahed instances are started and pre-allocated slab based on-F and-N. Starting with-N as the minimum value, the geometric series is generated at the ratio of-f until 1m (each slab chunk size is to be complementary by a multiple of 8, for example: if the ratio is 556, it will be added 4到560成 to 8 of the whole multiple). Then each slab is assigned a page. When the user sends a store request (Key,value), Memcached calculates the size of the key+value to see which slab it belongs to. Determine whether there is idle chunk in the slab after the key+value, if not enough to apply a page to the system (if you have reached the memory usage limit of the-m parameter setting, see if the-M is set.) If-M is set to return an error prompt, the data is deleted by the LRU algorithm). After applying, the page is cut by the chunk size of this slab, and then assigned one to hold the user data.
Attention:
1) chunk is divided in the page, and the page is fixed to 1m, so chunk maximum can not exceed 1m.
2) Chunk actually consumes 48B of memory, because the chunk data structure itself needs to occupy 48B.
3) If user data is greater than 1m, memcached will cut it and put it in multiple chunk.
4) The assigned page cannot be recycled.


third, optimization recommendations
1) The setting of the-n parameter, note that this parameter is set to a number divisible by 1024 (also consider the difference of 48B), otherwise the remainder of the part is wasted.
2) do not store more than 1m of data. Because of the need to split into multiple chunk, the computational and time costs are multiplied.
3) Use the Stats command to view the memcached status.
4) Eliminate eviction (deleted data).
caused eviction because of insufficient memory, there are three ideas:
(1) In the case of CPU power to open the compression (PHP extension);
(2) Increase the memory;
(3) Adjust-f parameter, reduce memory waste.
5) Adjust the business code to improve the hit ratio.
6) Cache small data. Save bandwidth, save network I/O time, save memory.
7) According to the business characteristics, for the data size interval Small business allocation dedicated memcached instances. This makes it possible to tune the-f parameter so that there are a few slab in the dataset and less memory wasted.
The most important parameters at startup:
-M total memcached max memory
-F Chunk size growth factor
-N Chunk Minimum allocated space
-C Disable CAs
-VVV Printing Details
We can see from the calculation that each slab chunk Sizes are 1.25 times times the size of the previous size, 1.25 is the value of-f at the start of the memcached, so, to adapt to different business scenarios, to reduce memory waste as little as possible, but also to save the data in our business, for example, to join our business is BT, the size is 200kb, how can we What do you do? Of course it is to ensure that the size of the chunk we apply for is 200kb, although memcached does not support this. If the creation of 38 slabs, the final data all fall into the 第20-25个 slab, that 0-19 and 26-38 of the memory is wasted, this is only can be evaluated, there is a situation, add our chunk size is 500kb, the data is 200KB, Because each chunk can only store one, so a chunk will have 500kb-200kb space waste, if there are 10 million chunk to save, how much space will be wasted? So, before using memcached, evaluate your data and adjust the-f factor based on it.

Simple grooming memcached Working principle/Workflow/Optimization Recommendations

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.