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

Full parsing of Memcached and monitoring using zabbix

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

What is Memcached?

Memcached is a distributed memory cache server, which is used to cache database query results, reduce database access times, and improve the speed and expansibility of dynamic web pages.

Characteristics of Memcached

a. The protocol is simple, and Memcached uses a simple text line protocol.

b. Event handling based on libevent

c. Built-in memory storage

d. Each Memcached server does not communicate with each other

E.Memcached uses key-value for storage

Libevent: Libevent is a lightweight open source high-performance event notification library written in C language, with the following highlights: event-driven (event-driven), high performance; lightweight, network-focused, not as bloated as ACE; source code is quite concise and easy to read; cross-platform support for Windows, Linux, * BSD and Mac Os; supports a variety of Imax O multiplexing technologies, such as epoll, poll, dev/poll, select and kqueue. Support for events such as Icano, timers and signals; register event priority.

Libevent has been widely used as the underlying network library, such as memcached, Vomit, Nylon, Netchat and so on.

Memory storage of Memcached

Because the traditional memory allocation uses malloc function and free function for memory allocation, a large number of memory fragments produced by memory can not be utilized.

Memcached uses Slab Allocator to allocate memory.

The principle of Slab Allocator: according to the predetermined allocation of memory into specific and different sizes of memory fast (chunk), and the same size of memory fast organized into groups (slab class).

How does Memcached cache data through Slab Allocator?

When a data arrives at Memcached, Memcached selects the appropriate slab according to the size of the data, and selects chunk to cache the data through the free list of chunk.

How does Memcached achieve unequal memory partition in the process of allocating memory?

Memcached will specify the growth factor factor at startup to control the differences between each slab, 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 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 growth factor factor of Memcached at startup [root@COS_Clone1 ~] # memcached-f 2-vv-f: specify growth factor factor-vv: view process 12341234

The timeout principle of Memcached

How to monitor whether the resource cached in memory is expired by Memcached? using Lazy Expiration,Memcached will not automatically monitor and detect whether the content cached in memory is expired. When a request arrives, Memcached will check the record timestamp of the requested resource, check whether the record has expired, and return if it has not expired.

The principle of deleting Memcached

When Memcached caches data in memory, it first selects the space that has timed out to record the records to be cached this time. If there is not enough memory space to cache, Memcached will use LRU (at least recently unused algorithm) to allocate resource space to new records.

Distributed scheduling of Memcached

If the web server wants to cache a database to the Memcached server, which one should it cache?

Actually, Memcached API provides several scheduling algorithms.

a. Remainder calculation:

The key value of the record to be cached on the Memcached is calculated by C32, and then the result is offset with the node number + 1. The result is the Memcached server where the data is to be cached.

Disadvantages:

When adding or removing nodes, the cache data is reorganized and cannot get the same server as when it was saved, thus affecting the hit rate of the cache.

B.Consistent hash:

The hostname of the node node is calculated by HASH and configured on the circle of 0 ~ 2 ^ 23. Then the key value of the resource is calculated by HASH and mapped on the circle. The first server found saves the data on this server after searching clockwise from the mapping location.

How to install Memcached

Download method: [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- yum install libeven- [root @ root ~] # 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

Startup of Memcached

[root@COS_Clone1] # memcached-p 20001-m 64m-d11

Memcached status query

[root@COS_Clone1 bin] # memcached-tool 127.0.0.1 stats#127.0.0.1:11211 Field Value accepting_conns 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 written by bytes_written 0? how many times did cas_badval 0 cas_hits 0 cas_misses 0 cmd_flush 0 cmd_get 0 get? How many times does cmd_set 0 insert conn_yields 0 connection_structures 11 curr_connections 10 curr_items 0 decr_hits 0 decr_misses 0 delete_hits 0 How many delete_misses 0 evictions 0 get_hits 0 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 in memory Uptime 35 version 1.4.41234567891011121314151617181920212223242526272829303132333435363738394012345678910111213141516171819202122232425262728293031323334353637383940

Use zabbix to listen to memcached

Monitoring data: how many hits, how many resources, how many times to get, how many times to add, read byte books, write bytes

1. Install zabbix on the host of Memached

[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 UserParameter=memcached.bytes_written 2 stats | grep "bytes_read" | awk'{print $2} 'UserParameter=memcached.bytes_written [*], / usr/bin/memcached-tool $1 UserParameter=memcached.bytes_written 2 stats | grep "bytes_written" | awk' {print $2} 'UserParameter=memcached.cmd_get [*] / usr/bin/memcached-tool $1awk $2 stats | grep "cmd_get" | awk'{print $2} 'UserParameter=memcached.cmd_set [*], / usr/bin/memcached-tool $1V $2 stats | grep "cmd_set" | awk' {print $2} 'UserParameter=memcached.get_hits [*], / usr/bin/memcached-tool $1grep $1 cmd_get 2 stats | grep "get_hits" | awk' {print $2} 'UserParameter=memcached.total_items [*] / usr/bin/memcached-tool $1 total_items 2 stats | grep "total_items" | awk'{print $2} '1234567812345678

two。 Install zabbix-server on another host and launch the configuration on the web page

Configure- > host- > item (memcached host)

Define item:

Define Graphs

Test result

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