What is memcached?
Memcached is a distributed memory cache server, which is used to cache database query results, reduce the number of database accesses, and improve the speed and extensibility of dynamic Web pages.
Characteristics of memcached
A. Simple protocol, memcached use simple text line protocol
B. Libevent-based event handling
C. Built-in memory storage mode
D. Non-communication between each memcached server
e.memcached is stored using the Key-value method
Libevent: Libevent is a lightweight, open source high-performance event notification library written in C, with the following highlights: event-driven (Event-driven), high performance, lightweight, network-focused, less ACE is so bloated and bulky; The source code is quite refined and easy to read; cross-platform support for Windows, Linux, *bsd and Mac Os, support for multiple I/O multiplexing Technologies, Epoll, poll, Dev/poll, select and kqueue; Support i/ O, timer and signal events, register event priority. The
Libevent has been widely used as the underlying network library, such as memcached, vomit, Nylon, netchat, and so on. Memory storage for
memcached
Because the traditional memory allocation uses the malloc function and the free function for memory allocation, the memory is generated by a large amount of memory fragmentation, which cannot be exploited. The
Memcached uses slab allocator to implement memory allocations. The principle of the
Slab allocator: Allocates memory to a specific and unequal amount of memory (chunk) in advance, and organizes the memory of equal size into groups (Slab Class).
Memcached How do I cache data through slab allocator?
When a data arrives at memcached, Memcached selects the appropriate size slab based on the size of the data, and in the free list of chunk, selects the chunk to cache the data. How does the
memcached implement unequal memory partitioning during the allocation of memory? The
memcached specifies the growth factor factor at startup to control the difference between each slab and is not randomly divided. For example, if the growth factor factor is 2, and the size of each chunk in the previous slab class is 2, then each slab in the next chunk class is 4 in size, and so on. The value of growth factor in the default memcahed setting is 1.25
Specify memcached growth factor factor at startup [[email protected]_clone1 ~] #memcached-F 2-vv-f: Specify growth factor factor-VV: View procedure
memcached Time-out principle
memcached How to monitor if the resources cached in memory are out of date, the lazy expiration,memcached does not automatically monitor and detect the expiration of cached content in memory, and when a request arrives, Memcached will check the timestamp of the requested resource's record, check if the record is out of date, and then return it if there is no time.
The principle of memcached removal
Memcached cache data in memory, first selects the space that has timed out to record the secondary cache, and if the memory space is not sufficient to cache, Memcached uses the LRU (least recently unused algorithm) to allocate the resource space to the new record.
distributed scheduling of memcached
650) this.width=650; "title=" "src=" http://img.blog.csdn.net/20170522180412589?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here write a picture description "/>
If the Web server is going to cache a database to the memcached server, what about the cache?
In fact, Memcached API provides several scheduling algorithms
A. Remainder calculation:
The key value of the record that will be cached on the memcached is computed, and the result is C32 with node number +1, and the result is the memcached server to which this data will be cached.
Disadvantages:
When a node is added or removed, the cached data is reorganized to get the same server as it was saved, which affects the cache hit ratio.
B.consistent Hash:
The host name of node is hashed and configured on the 0~2^23 circle, then the key value of the resource is hashed and mapped on the circle, and the first server found is saved on this server by searching the map position clockwise.
650) this.width=650; "title=" "src=" http://img.blog.csdn.net/20170522182448130?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here write a picture description "/>
How to install memcached
Download mode: [[email protected]_clone1 ~] #wget http://memcached.org/files/memcached-1.4.36.tar.gz Installation: [Email protected]_ CLONE1 ~] #tar-xzf memcached-1.4.36.tar.gz [[email protected]_clone1 ~] #mkdir/usr/local/memcached[[email protected]_ CLONE1 ~] #yum install Libeven-devel[[email protected]_clone1 ~] #cd memcached-1.4.36[[email protected]_clone1 memcached-1.4.36]#./configure--prefix=/usr/local/memcached--bindir=/usr/local/bin--sbindir=/usr/local/sbin [[ Email protected]_clone1 memcached-1.4.36] #make && make install
memcached Start-up
[[Email protected]_clone1 ~] #memcached-P 20001-m 64m-d
memcached status Query
[[email protected]_clone1 bin]# memcached-tool 127.0.0.1:11211 stats#127.0.0.1:11211 Field Value accepting_conns 1 auth_cmds 0 auth_errors 0 bytes 0 bytes_read 7 &nbsHow many bytes did p; read bytes_written 0 how many bytes were written cas_badval 0 cas_hits 0 cas_misses 0 cmd_flush 0 cmd_get 0 How many times did you get cmd_set 0 How many times to insert conn_yields 0 connection_structures 11 curr_connections 10 curr_items 0 decr_hits &nbSp; 0 decr_ misses 0 delete_hits 0 delete_misses 0 evictions 0 get_hits 0 Hit How many times get_misses 0 incr_hits 0 incr_misses 0 limit_maxbytes 67108864 listen_disabled_num 0 pid 4465 pointer_size 64 rusage_system 0.035994 rusage_user 0.000999 threads 4 time 1495460002 total_connections 11 total_items 0 how many records in memory uptime 35 version 1.4.4
listens for data using Zabbix listener memcached
: How many hits, how many resources, how many times, how many times to add, read the byte book, and write the number of bytes
1. Install Zabbix
on the memached host
[[email protected]_clone1 ~]# yum install zabbix zabbix-agent[[email Protected]_clone1 ~] #vim /etc/zabbix_agent.conf userparameter=memcached.bytes _read[*],/usr/bin/memcached-tool $1:$2 stats | grep "Bytes_read" | awk ' {print $2} ' userparameter=memcached.bytes_written[*],/usr/bin/ memcached-tool $1:$2 stats | grep "Bytes_written" | awk ' {print $2} ' UserParameter=memcached.cmd_get[*],/usr/bin/memcached-tool $1:$2 stats | grep "Cmd_get" | awk ' {print $2} ' userparameter=memcached.cmd_set[*],/usr/bin/memcached-tool $1:$2 stats | grep " Cmd_set " | awk ' {print $2} ' userparameter=memcached.get_hits[*],/ Usr/bin/memcached-tool $1:$2 stats | grep "Get_hits" | awk ' {print $2} ' Userparameter=memcached.total_items[*],/usr/bin/memcached-tool $1:$2 stats | grep "Total_items" | awk ' {print $2} '
2. Install Zabbix-server on another host and start configuring it on a Web page
Configure->host->item (memcached host)
Define item:
650) this.width=650; "title=" "src=" http://img.blog.csdn.net/20170522223813969?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here write a picture description "/>
650) this.width=650; "title=" "src=" http://img.blog.csdn.net/20170522223844110?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here write a picture description "/>
650) this.width=650; "title=" "src=" http://img.blog.csdn.net/20170522223947185?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here write a picture description "/>
Define Graphs
650) this.width=650; "title=" "src=" http://img.blog.csdn.net/20170522224116562?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here write a picture description "/>
Test Results
650) this.width=650; "title=" "src=" http://img.blog.csdn.net/20170522224150031?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here write a picture description "/>
memcached comprehensive analysis and monitoring with Zabbix