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 C language, lightweight open source high-performance event notification library, mainly has the following highlights: event-driven (Event-driven), high performance, lightweight, focus on the network, not as large as the ACE; The source code is quite refined and easy to read; cross-platform, support Windows, Linux, *bsd and Mac Os, supports multiple I/O multiplexing Technologies, Epoll, poll, Dev/poll, select, and Kqueue, supports events such as I/O, timers and Signals, and registers event priorities.
Libevent has been widely used as the underlying network library, such as memcached, vomit, Nylon, netchat, and so on.
memcached Memory Storage
Because the traditional memory allocations use the malloc function and the free function for memory allocation, the memory is generated by a large amount of memory fragmentation that cannot be exploited.
Memcached uses slab allocator to achieve memory allocation.
Slab Allocator principle: According to the pre-defined memory allocation to size-specific and unequal memory fast (chunk), and the size of the memory is fast organized into groups (Slab Class).
memcached How does the data cache be implemented through the slab allocator?
When a data arrives at the memcached, Memcached chooses the size suitable slab according to the data size, in through Chunk's idle list, selects the chunk to cache the data up.
How does the memcached implement unequal memory partitioning during the allocation of memory?
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 12341234
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; "Src=" http://img.blog.csdn.net/20170522180412589?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here to write a picture describing "title=" "style=" border:none; "/>
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; "Src=" http://img.blog.csdn.net/20170522182448130?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here to write a picture describing "title=" "style=" border:none; "/>
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 install123456789123456789
memcached Start-up
[[Email protected]_clone1 ~] #memcached-P 20001-m 64m-d11
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.412345678910111213141516171819202122232425262728293031323334353637383940123456789101112131415161718192021222324252627 28293031323334353637383940
use Zabbix to monitor memcached ,
Listen for data: How many hits, how many resources, how many times, how many times to add, read the byte book, 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} ' 1234567812345678
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; "Src=" http://img.blog.csdn.net/20170522223813969?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here to write a picture describing "title=" "style=" border:none; "/>
650) this.width=650; "Src=" http://img.blog.csdn.net/20170522223844110?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here to write a picture describing "title=" "style=" border:none; "/>
650) this.width=650; "Src=" http://img.blog.csdn.net/20170522223947185?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here to write a picture describing "title=" "style=" border:none; "/>
Define Graphs
650) this.width=650; "Src=" http://img.blog.csdn.net/20170522224116562?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here to write a picture describing "title=" "style=" border:none; "/>
Test Results
650) this.width=650; "Src=" http://img.blog.csdn.net/20170522224150031?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqva2fpcnvpmtiz/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "alt=" here to write a picture describing "title=" "style=" border:none; "/>
This article is from the "Lnsanellinux" blog, make sure to keep this source http://lnsane784.blog.51cto.com/1226518/1928524
memcached comprehensive analysis and use of Zabbix monitoring