Memcached Cache Server
Memcached is a high-performance distributed memory cache server. The general purpose is to reduce the number of database accesses by caching database query results, so as to speed up dynamic web applications and improve scalability. Main features: 1. C/S architecture, simple protocol; 2. libevent-based event processing (epoll); 3. Slab allocation memory management mechanism; 4. client-based distributed architecture; memcached stores all the data in the memory. Therefore, restarting memcached will cause all data loss. In addition, after the memory capacity reaches the specified value, it is based on LRU (least recently used) the algorithm automatically deletes unused caches; Memcached restrictions: 1. There is no limit on the number of items to be saved, as long as the memory is sufficient; 2. the maximum data expiration time for 30 days is controlled by the constant realtime_maxdelta 60*60*24*30; 3. The maximum key length is 250 bytes, controlled by the constant key_max_length 250; 4. The maximum data size of a single item is 1 MB, controlled by the constant power_block 1048576; 5. the maximum number of simultaneous connections is 200, use freetotal control in conn_init; the maximum soft connection is 1024, through settings. maxconns = 1024 control;
Distributed memcached:
Consistent hashingThe memcached server does not have the distributed function. Therefore, the distributed function relies entirely on the implementation of the client. The client uses a consistent hash algorithm to select different memcached servers for storage for different keys. Consistent hashing algorithm: 1. Obtain the hash value of the memcached server (node) and configure it to 0 ~ 2 ^ 32 on the circle; 2. Use the same method to obtain the hash value of the key, map it to the circle, and start searching clockwise from the position of the Data ing, save the data to the first server.
I. command line options
1. memcached command line options:-P specifies the TCP listening port. The default value is the maximum memory size of listen 11-m. The default value is 64mb-d, which is used as the daemon to start in backend-VV very verbose mode, debug information and error information are output to console-F to specify the growth factor. The LRU is disabled by default in 1.25-M. After the memory is exhausted, the maximum number of concurrent connections in error-C is returned, by default, 1024-u specifies the username (only when the root process is started).-P specifies the PID file path, for example, to start memcache with a daemon process. /memcached-D-M 10-u root-l 127.0.0.1-P 12000-C 256-P/tmp/memcached. PID
2. Connect the client to the memcached Server:Telnet 127.0.0.1 12000 (enter the quit command to exit the connection) II. Command Format
Common commands
Command |
Command description |
Storage commands (in the following format) |
Set |
Add a new key-value pair to the cache. If the key already exists, replace it |
Add |
A key-value pair is added to the cache only when the key does not exist in the cache. If the key already exists, the operation fails. |
Replace |
The cached key is replaced only when the key already exists. If the key does not exist, the operation fails. |
Append |
Add the cached data. If the key does not exist, return not_stored. |
Prepend |
Similar to append, but adding data before caching |
CAS |
The checked and set parameter can be stored only when the last parameter matches the parameter obtained by gets. Otherwise, "exists" is returned" |
Incr |
Auto-increment, incr key value, adds a value to the numeric value |
Decr |
Auto-Subtraction |
|
|
READ command |
Get |
Search key, such as get key1, key2... |
Gets |
The gets command returns a number more than the common GET command. This number can be used to check whether the data has changed. |
Delete |
Delete key |
Flush_all |
Clear Cache |
STATUS Command |
Stats |
View the basic status of the memcached Server |
Stats cachedump slab_id limit_num |
|
Storage Command Format
<command name> <key> <flags> <exptime> <bytes><data block>
Parameter description:
<Command name> |
Set/Add/replace |
<Key> |
Search for keywords |
<Flags> |
The client uses it to store additional information about key-value pairs. |
<Exptime> |
The survival time of the Data. 0 indicates that the data will never expire. |
<Bytes> |
Storage bytes |
<Data block> |
Stored data blocks (which can be directly understood as values in the key-value structure) |
Example 1:
Iii. memcached Memory Management Mechanism memcached memory management mechanism:
Slab allocation1. Divide the allocated memory into chunk blocks of various sizes and divide the chunk blocks into groups) ==> to solve the memory fragmentation problem, memcached can control the differences between Slab by specifying the growth factor (-F option) at startup. The default value is 1.25. 2. The allocated memory will not be released, but will be reused ==> avoid frequent calls to malloc, free3, and memcached. Select the slab that best fits the data size based on the received data size. Concepts: page: memory space allocated to slab. The default value is 1 MB; Chunk: memory space used for cache record; slab class: Chunk group of a specific size;
Lazy expiration: Memcached does not monitor whether the record expires. Instead, it checks the timestamp of the record during get to check whether the record expires. After the record times out, memcached does not release the allocated memory, but marks it as reusable.
LRU: When the memory space is insufficient (the new space cannot be retrieved from the slab class), search from the unused records and allocate the space to the new records.
Memcached Cache Server