The Article We introduced that memcached is a distributed high-speed cache server.
Server. This section describes the implementation of the internal structure of memcached and the memory management mode. In addition
The vulnerabilities caused by internal structures will also be described.
2.1 slab allocation mechanism: sort out the memory for Reuse
By default, the latest memcached uses the Slab allocator mechanism to allocate and manage memory. In this mechanism
Before, the memory allocation is done by simply malloc and free for all records. However, this method causes memory
Fragments increase the burden on the memory manager of the operating system. In the worst case, the operating system is worse than the memcached process.
Slow. Slab allocator was born to solve this problem.
Next let's take a look at the Slab allocator principle. Below are the objectives of Slab allocator in the memcached document:
The primary goal of the slabs subsystem in memcached was to eliminate memory fragmentation issues
Totally by using fixedsize
Memory chunks coming from a few predetermined size classes.
That is to say, the basic principle of Slab allocator is to divide the allocated memory into blocks of a specific length according to the predefined size,
To completely solve the memory fragmentation problem.
The principle of slab allocation is quite simple. Split the allocated memory into chunk blocks of various sizes, and split the chunk blocks with the same size.
Group (the chunk set) (figure 2.1 ).
Figure 2.1: Structure of slab allocation
In addition, Slab allocator can reuse allocated memory. That is to say, the allocated memory will not be released,
12
Chapter 2 about memcached memory storage
Reuse.
Slab allocation terminology
Page
Memory space allocated to slab. The default value is 1 MB. After being allocated to slab, it is split into chunks based on the size of slab.
Chunk
Memory space used to cache records.
Slab class
A chunk group of a specific size.
2.2 principle of caching records in Slab
The following describes how memcached selects slab for the data sent by the client and caches it to chunk.
Memcached selects the slab that best fits the data size according to the size of the received data (Figure 2.2 ). Stored in memcached
The list of idle chunks in slab. Select a chunk based on the list and cache the data in it.
Figure 2.2: Method for Selecting a storage record group
In fact, Slab allocator also has advantages and disadvantages. The following describes its shortcomings.
2.3 disadvantages of Slab allocator
Slab allocator solves the original memory fragmentation problem, but the new mechanism also brings new problems to memcached.
This problem occurs because the allocated memory is of a specific length, so the allocated memory cannot be effectively used. For example, convert 100 bytes
The data is cached to 128 bytes of chunk, and the remaining 28 bytes are wasted (Figure 2.3 ).
13
Chapter 2 about memcached memory storage
Figure 2.3: Use of chunk Space
There is no perfect solution for this problem, but the document records a more effective solution.
The most efficient way to reduce the waste is to use a list of size classes that closely matches (if that's at all
Possible) common sizes of objects that the clients of this particle installation of memcached are likely
Store.
That is to say, if you know the public size of the data sent by the client in advance or only cache data of the same size
Use a list of groups that are suitable for data size to reduce waste.
However, it is a pity that no optimization can be performed now. We can only look forward to future versions. However, we can adjust slab class
. The following describes the growth factor option.
2.4 use growth factor for Tuning
Memcached specifies the growth factor at startup (through F
To control
Difference. The default value is 1.25. However, before this option appeared, this factor was fixed to 2, called the "Powers of 2" policy.
Let's start memcached in verbose mode with the previous settings:
$ Memcached F
2 VV
The following figure shows the verbose output after startup:
Slab Class 1: Chunk Size 128 perslab 8192
Slab Class 2: Chunk Size 256 perslab 4096
Slab Class 3: chunk size 512 perslab 2048
Slab Class 4: chunk size 1024 perslab 1024
Slab Class 5: Chunk Size 2048 perslab 512
Slab Class 6: Chunk Size 4096 perslab 256
Slab Class 7: Chunk Size 8192 perslab 128
Slab Class 8: Chunk Size 16384 perslab 64
Slab Class 9: Chunk Size 32768 perslab 32
Slab class 10: Chunk Size 65536 perslab 16
Slab class 11: Chunk Size 131072 perslab 8
Slab Class 12: Chunk Size 262144 perslab 4
Slab class 13: Chunk Size 524288 perslab 2
It can be seen that, starting from the 128-byte group, the size of the group increases to 2 times that of the original group. In this case, the difference between Slab
In some cases, memory is a waste. Therefore, to minimize memory waste, the growth factor was added two years ago.
This option.
Let's take a look at the output of the current default settings (F = 1.25) (limited space, only 10th groups are written here ):
Slab Class 1: Chunk Size 88 perslab 11915
14
Chapter 2 about memcached memory storage
Slab Class 2: Chunk Size 112 perslab 9362
Slab Class 3: Chunk Size 144 perslab 7281
Slab Class 4: Chunk Size 184 perslab 5698
Slab Class 5: Chunk Size 232 perslab 4519
Slab Class 6: Chunk Size 296 perslab 3542
Slab Class 7: Chunk Size 376 perslab 2788
Slab Class 8: Chunk Size 472 perslab 2221
Slab Class 9: Chunk Size 592 perslab 1771
Slab class 10: Chunk Size 744 perslab 1409
It can be seen that the gap between groups is much smaller than the factor of 2, and it is more suitable for caching hundreds of bytes of records. As shown in the preceding output
It may feel that some calculation errors are intentionally set to maintain the alignment of the number of bytes.
When you introduce memcached into a product or directly use the default value for deployment, it is best to recalculate the expected average data length.
Degrees, adjust the growth factor to get the most appropriate settings. Memory is a precious resource, which is a waste of resources.
Next we will introduce how to use the memcached stats command to view various information such as slabs utilization.
2.5 view the internal status of memcached
Memcached has a command named stats, which can be used to obtain various information. There are many ways to execute commands.
Telnet is the simplest:
$ Telnet host name port number
After connecting to memcached, enter stats and press enter to obtain information including resource utilization. In addition,
Enter "stats slabs" or "stats items" to obtain information about cache records. End Program Enter quit.
For more information, see protocol.txt in the memcachedsoftware package.
$ Telnet local host 11211
Trying: 1...
Connected to localhost.
Escape Character is '^]'.
Stats
Stat PID 481
Statuptime 16574
Stat time 1213687612
Stat version 1.2.5
Stat pointer_size 32
Stat rusage_user 0.102297
Stat rusage_system 0.214317
Stat curr_items 0
Stat total_items 0
Stat bytes 0
Stat curr_connections 6
Stat total_connections 8
Stat connection_structures 7
Stat performance_get 0
Stat performance_set 0
Stat get_hits 0
Stat get_misses 0
Stat evictions 0
Stat bytes_read 20
Stat bytes_written 465
Stat limit_maxbytes 67108864
Stat threads 4
End
15
Chapter 2 about memcached memory storage
Quit
In addition, if the libmemcached client library for C/C ++ is installed, the memstat command is installed. Enable
The method is simple. You can use fewer steps to obtain the same information as telnet and obtain information from multiple servers at a time.
$ Memstat servers =
Server1, server2, server3 ,...
Libmemcached can be obtained from the following address:
Http://tangent.org/552/libmemcached.html
2.6 view slabs usage
Memcached creates the memcachedtool written by Brad.
To facilitate slab usage.
Situation (it sorts the returned values of memcached into easy-to-read formats ). You can obtain the script from the following address:
Http://code.sixapart.com/svn/memcached/trunk/server/scripts/memcachedtool
The usage is extremely simple:
$ Memcachedtool
Host Name: Port options
You do not need to specify an option to view slabs usage. Use the following command:
$ Memcachedtool
Host Name: Port
The obtained information is as follows:
# Item_size max_age 1mb_pages count full?
1 104 B 1394292 s 1215 12249628 Yes
2 136 B 1456795 s 52 400919 Yes
3 176 B 1339587 s 33 196567 Yes
4 224 B 1360926 s 109 510221 Yes
5 280 B 1570071 s 49 183452 Yes
6 352 B 1592051 s 77 229197 Yes
7 440 B 1517732 s 66 157183 Yes
8 552 B 1460821 s 62 117697 Yes
9 696 B 1521917 s 143 215308 Yes
10 872 B 1695035 s 205 246162 Yes
11 1.1 kb 1681650 s 233 221968 Yes
12 1.3 kb 1603363 s 241 183621 Yes
13 1.7 kb 1634218 s 94 57197 Yes
14 2.1 kb 1695038 S 75 36488 Yes
15 2.6 kb 1747075 S 65 25203 Yes
16 3.3 kb 1760661 s 78 24167 Yes
16
Chapter 2 about memcached memory storage
The meaning of each column is:
Column meaning
# Slab class no.
Item_size Chunk Size
Survival time of the oldest record in max_age LRU
Number of pages allocated to Slab by 1mb_pages
Count the number of records in Slab
Full? Whether the slab contains idle chunk
The information obtained from this script is very convenient for tuning and is strongly recommended.
2.7 conclusion
This section briefly describes the cache mechanism and Optimization Method of memcached. Hope readers can understand the memory management principles of memcached
And its advantages and disadvantages.
Next time, we will continue to explain the principles of LRU and expire, as well as the latest development direction of memcached-extensible System
(Pluggable architecher )).
17
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