Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Memcache storage mechanism and instruction summary

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. Basic introduction of memcache

Memcached is a high-performance distributed memory cache server. The purpose of general use is to reduce the number of database visits by caching database query results, so as to improve the speed and scalability of dynamic Web applications.

The operation diagram of Memcache:

Characteristics of Memcache

As a high-speed distributed cache server, memcached has the following characteristics.

1. The protocol based on Cramp S is simple.

Memcached's server-client communication does not use complex formats such as XML, but uses a simple text-line-based protocol. Therefore, data can also be saved and obtained on memcached through telnet.

2. Event handling based on libevent

Libevent is a library that encapsulates event handling functions such as epoll of Linux and kqueue of BSD operating systems into a unified interface. O (1) performance can be achieved even if the number of connections to the server increases. Memcached uses this libevent library, so it can perform its high performance on Linux, BSD, Solaris, and other operating systems.

3. Built-in memory storage mode

To improve performance, the data saved in memcached is stored in memcached's built-in memory storage space. Because the data exists only in memory, restarting memcached and restarting the operating system will cause all data to disappear. In addition, when the content capacity reaches the specified value, the unused cache is automatically deleted based on the LRU (Least Recently Used) algorithm. Memcached itself is a server designed for caching, so it doesn't think too much about the persistence of the data.

4. Distributed memcached that does not communicate with each other

Although memcached is a "distributed" cache server, there is no distributed function on the server side. Memcached does not communicate with each other to share information. So, how to distribute it? It all depends on the implementation of the client. (as shown in the following figure)

Back to the top.

2. Understand the memory storage of memcache

Back to the top.

2.1. Storage mechanism

Memcache uses Slab Allocator to store data. This mechanism can organize the memory well for reuse, thus solving the problem of memory fragmentation. Before the advent of this mechanism, memory allocation was done by simply malloc and free all records. However, this approach can lead to memory fragmentation, increase the burden on the operating system memory manager, and, at worst, cause the operating system to be slower than the memcached process itself.

2.2.The basic principles of Slab Allocator

1. According to the predetermined size, the allocated memory is divided into specific blocks (chunk) in units of page (default is 1m per page), and chunk of the same size is divided into groups (sets of chunk).

2. When storing data, a chunk area similar to the size of value will be found for storage.

3. Once memory is allocated in the form of page, it will not be reclaimed or reallocated before reboot to solve the problem of memory fragmentation. (allocated memory is not released, but reused)

Back to the top.

2.3. Understand four nouns

[you can refer to the image analysis chart below for understanding]

Slab

It is used to represent the maximum size data stored, only for definition (in popular terms, it represents the range of data size that can be stored). By default, the front and back slab represents a 1.25x increase in stored size. For example, slab1 is 96 bytes and slab2 is 120 bytes.

Page

The memory space allocated to Slab. The default is 1MB. After giving it to Slab, it will be cut into chunk according to the size of slab.

Chunk

Memory space for caching records

Slab calss

Chunk collections of specific siz

Back to the top.

2.4. the specific process of memory allocation of Slab

Memcached specifies the maximum memory usage with the-m parameter at startup, but this is not used up as soon as it starts, but is gradually allocated to each slab. If a new data is to be stored, first select an appropriate slab, and then check to see if the slab still has free chunk, and if so, store it directly; if not, you need to apply for memory. Slab applies for memory in page units. No matter what the size is, 1m of page will be assigned to the slab (the page will not be reclaimed or reassigned, it will always belong to the slab). After applying to the page, slab will split the memory of the page according to the size of the chunk, so that it becomes an array of chunk, and then select one of the chunk array to store the data. If there is no free page, the changed slab will be LRU instead of the entire memcache LRU.

Image analysis chart: (just make do with this picture, it's not very professional 2333)

Back to the top.

2.5. Memcache stores specific procedures

Memcached does not put all sizes of data together, but pre-divides the data space into a series of slabs, each slab is only responsible for a certain range of data storage. Based on the size of the data received, memcached chooses the slab that best suits the data size. If the slab still has a list of free chunk, select chunk according to the list and cache the data in it; if not, apply for page (1m) [refer to figure 23333 above]

Specific analysis: from the above we understand the role of slab. The growth factor of Slab increases by 1.25 times by default. Then why is it that some are not 1.25 times as much? The answer is affected by decimals, you can use-f int to test an integer growth factor to see the effect. [the details will be explained later]

The following figure is analyzed, such as 112bytes in slab, indicating that value that is larger than 88 bytes and less than or equal to 112bytes can be stored.

Back to the top.

2.6.The shortcomings of Slab Allocator

Slab Allocator solves the problem of memory fragmentation, but the new mechanism also brings new problems to memcached.

The problem is that because a specific length of memory is allocated, the allocated memory cannot be used effectively. For example, if you cache 100 bytes of data into a 128byte chunk, the remaining 28 bytes are wasted (as shown in the following figure).

Back to the top.

2.7. Tuning using the-f growth factor

The growth factor is the multiple of growth between two adjacent chunk. This parameter memcache defaults to 1.25, but let's test it with the integer 2 first to see how it works.

From the figure, we can see that the growth of chunk size is twofold.

Let's take another look at the effect of-f 1.25

Why can't the 1.25x growth factor guarantee the 1.25x growth of all adjacent chunk size?

Because these errors are deliberately set to maintain the alignment of the number of bytes.

Compared with figure 1, it can be seen that the gap between groups with a factor of 1.25 is much smaller than that with a factor of 2, which is more suitable for caching records with hundreds of bytes.

Therefore, when using memcached, it is best to recalculate the expected average length of the data and adjust the growth factor to get the most appropriate setting.

Back to the top.

3. Memcache deletion mechanism

We know from the above that the memory that has been allocated will not be released and recycled. After the record times out, the client cannot see the record, and its storage space can be reused.

Back to the top.

3.1 、 Lazy Expiration

Memcached internally does not monitor whether the record is out of date, but instead looks at the record's timestamp during get to check whether the record is out of date. This technique is called lazy (inert) expiration. Therefore, memcached does not waste CPU time on expiration monitoring.

Back to the top.

3.2.Delete LRU

Memcached will give priority to the space of records that have timed out, but even so, there will be insufficient space when new records are appended, so a mechanism called Least Recently Used (LRU) is used to allocate space. As the name implies, this is the mechanism for deleting "least recently used" records. Therefore, when memcached runs out of memory space (when new space cannot be obtained from slab class), it searches for recently unused records and allocates its space to new records. From a practical point of view of caching, the model is ideal.

However, in some cases, the LRU mechanism can cause trouble. LRU can be disabled when memcached starts with the "- M" parameter.

It is important to note at startup that the lowercase "- m" option is used to specify the maximum memory size. If you do not specify a specific value, the default value 64MB is used.

After specifying the "- M" parameter to start, memcached will return an error when memory is exhausted. After all, memcached is not a memory, but a cache, so it is recommended to use LRU.

Back to the top.

4. Start memcache parameters

[the parameters of boldface are more commonly used]

-p

Listening TCP port (default: 11211)

-U

UDP listening port (off by default: 11211 0)

-d

Run as a daemon

-u

Specify the user to run

-m.

Maximum memory usage, in MB. Default 64MB

-c

Maximum number of simultaneous connections. Default is 1024.

-v

Output warning and error messages

-vv

Print the client's request and return information

-h

Help information

-l

Bind address (any ip address can be accessed by default)

-P

Save PID in a file file

-I

Print memcached and libevent copyright information

-M

Disable LRU policy, return error when memory is exhausted

-f

Growth factor, default 1.25

-n

Initial chunk=key+suffix+value+32 structure, default 48 bytes

-L

Enable large memory pages to reduce memory waste and improve performance

-l

Resize allocated slab pages, default 1m, minimum 1k to 128m

-t

Number of threads. Default is 4. Because memcached uses NIO, more threads don't do much.

-R

Maximum number of concurrency per event connection, default is 20

-C

Disable the CAS command (you can disable version counting and reduce overhead)

-b

Set the backlog queue limit (default: 1024)

-B

Binding protocol-one of ascii, binary or auto (default)

S

UNIX socket

-a

Access mask for UNIX socket, in octal (default: 0700)

Back to the top.

5. Summary of Memcache instructions

Instruction

Description

Examples

Get key

# return the corresponding value

Get mykey

Length of validity of the set key identifier

There are no additions to key, there are updates.

Set mykey 0 60 5

Length of validity of the add key identifier

# add key-value value and return stored/not_stored

Add mykey 0 60 5

Length of validity of the replace key identifier

# value,key exists in replacement key. Success returns stored,key. No failure returns not_stored.

Replace mykey 0 60 5

Length of validity of the append key identifier

# append the value in key, return stored for success and not_stored for failure

Append mykey 0 60 5

Length of validity of the prepend key identifier

# the value value in the preappended key, which returns stored for success and not_stored for failure

Prepend mykey 0 60 5

Incr key num

# add num to value in key. If there are no numbers in key, the value value will be replaced with num. Returns the increased value

Incre mykey 1

Decr

# ditto

Same as above

Delete key [key2...]

Delete one or more key-value. Deleted is returned for successful deletion, and not_found is returned if there is no failure.

Delete mykey

Flush_all [timeount]

# clear all key values in [timeout time], but not delete items, so memcache still takes up memory

Flush_all 20

Version

# return the version number

Version

Verbosity

# Log level

Verbosity

Quit

# close the connection

Quit

Stats

# return Memcache general statistics

Stats

Stats slabs

# returns the information of each slab created during the operation of Memcache

Stats slabs

Stats items

# return the number of item in each slab and the oldest number of item seconds

Stats items

Stats malloc

# display memory allocation data

Stats malloc

Stats detail [on | off | dump]

# on: open detailed operation record, off: close detailed operation record, dump display detailed operation record (number of times of get, set, hit, del of each key)

Stats detail on

Stats detail off

Stats detail dump

Stats cachedump slab_id limit_num

# display the first limit_num key in slab_id

Stats cachedump 1 2

Stats reset

# emptying statistics

Stats reset

Stats settings

# View configuration settings

Stats settings

Stats sizes

# shows the number of items in a fixed chunk size

Stats sizes

Note: identifier: a hexadecimal unsigned integer (expressed in decimal) that needs to be stored with the data and returned when get

Ps: recently, I always think about the future direction. I feel a little confused. I can't study hard. To adjust the state of mind as soon as possible, do not be impetuous, haste makes waste.

Reference:

1. Detailed explanation of the principle and use of Memcached author: heiyeluren (Night passers-by)

Http://blog.csdn.net/heiyeshuwu

2. A comprehensive analysis of memcached by Masahiro Nagano and charlee translation by Maezaka

3. Memcache memory allocation policy and performance (usage) status check author: jyzhou

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report