What is Memcached?
Memcached is a distributed memory cache server for caching data base Query results, reduce the number of database visits, improve the speed and scalability of dynamic web pages.
Characteristics of Memcached
a. Simple protocol, Memcached uses simple text line protocol
b. Event handling based on libevent
c. Built-in memory storage
d. No communication between each Memcached server
e.Memcached uses key-value for storage
Libevent: Libevent is a use C Language The compiled lightweight open source high performance event notification library has the following main highlights: event-driven, high performance; lightweight, focused on the network, not as bulky as ACE; the source code is quite refined and easy to read; cross-platform support for Windows, ACE, etc. Linux * BSD and MacOs; support multiple I/O multiplexing technologies, such as epoll, poll, dev/poll, select and kqueue; support I/O, timer and signal events; register event priority.
Libevent has been widely used as the underlying network library, such as memcached, Vomit, Nylon, Netchat and so on.
Memcached Memory Storage
Traditional memory allocation uses malloc function and free function to allocate memory, which results in a large number of memory fragments and can not be used.
Memcached uses Slab Allocator to allocate memory.
The principle of Slab Allocator is to allocate memory into chunk s of different sizes according to pre-specified rules, and to organize memory of equal sizes into groups (slab class es).
How does Memcached cache data through Slab Allocator?
When a data arrives at Memcached, Memcached chooses the slab with the appropriate size according to the size of the data. In the free list of chunks, chunks are selected to cache the data.
How does Memcached achieve unequal memory partitioning during memory allocation?
Memcached specifies the growth factor at startup to control the differences between each slab, and it is not arbitrary. For example, the growth factor is 2, the size of each chunk in the previous slab class is 2, and the size of each chunk in the next slab class is 4, and so on. The value of growth factor in the default Memcahed setting is 1.25
Specify the growth factor of Memcached at startup [root@COS_Clone1 ~]#memcached -f 2 -vv - f: Specify growth factor - vv: View process 12341234
Timeout Principle of Memcached
How does Memcached monitor whether the resources cached in memory are expired? It uses Lazy Expiration. Memcached does not automatically monitor and detect whether the contents cached in memory are expired. When a request arrives, Memcached checks the record timestamp of the requested resources, checks whether the records are expired and returns if they are not expired.
The deletion principle of Memcached
When Memcached caches data in memory, it first chooses the time-out space to record the record to be cached. If the memory space is insufficient, Memcached will use LRU (least not used recently). algorithm ) To allocate resource space to new records.
Memcached Distributed Scheduling
If a web server wants to cache a database to a Memcached server, where should it be cached?
In fact, the Memcached API provides several scheduling algorithms
a. Residual calculation:
The key value of the record to be cached on the Memcached is calculated by C32, and the result is redundant with the number of node s + 1. The result is the Memcached server that the data is to be cached.
Disadvantages:
When adding or removing nodes, the cache data is reorganized and the same server can not be obtained as it was saved, thus affecting the cache hit rate.
b.Consistent hash:
The host name of node is computed by HASH, which is arranged on the circle of 0-2^23. Then the key value of resource is computed by HASH, and mapped on the circle. Looking clockwise from the mapping position, the first server found saves the data on this server.
Installation of Memcached
Download mode: [root@COS_Clone1 ~]#wget http://memcached.org/files/memcached-1.4.36.tar.gz installation: [root@COS_Clone1 ~]#tar -xzf memcached-1.4.36.tar.gz [root@COS_Clone1 ~]#mkdir /usr/local/memcached[root@COS_Clone1 ~]#yum install libeven-devel[root@COS_Clone1 ~]#cd memcached-1.4.36[root@COS_Clone1 memcached-1.4.36]# ./configure --prefix=/usr/local/memcached --bindir=/usr/local/bin --sbindir=/usr/local/sbin [root@COS_Clone1 memcached-1.4.36]#make && make install123456789123456789
Start-up of Memcached
[root@COS_Clone1 ~]#memcached -p 20001 -m 64m -d11
Memcached status query
[root@COS_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 How many bytes were 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 acquisitions have been made cmd_set 0 How many times to insert conn_yields 0 connection_structures 11 curr_connections 10 curr_items 0 decr_hits 0 decr_misses 0 delete_hits 0 delete_misses 0 evictions 0 get_hits 0 How many hits 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 are in memory uptime 35 version 1.4.41234567891011121314151617181920212223242526272829303132333435363738394012345678910111213141516171819202122232425262728293031323334353637383940
Monitoring memcached with zabbix
Monitoring data: how many hits, how many resources, how many acquisitions, how many additions, reading byte books, writing bytes
1. Install zabbix on the Maiched host
[root@COS_Clone1 ~]# yum install zabbix zabbix-agent[root@COS_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 configuration on the web page
configure->host->item(memcached host)
Define item:
Define Graphs
test Result