Memcached overview, basic operating instructions and deployment

One: Overview of memcached

1.1: Definition of memcached

  • memcache is a distributed high-speed caching system developed by Bard Fitzpartrick of LiveJournal, but it is currently used by many websites to improve website access speed, especially for large-scale websites that require frequent access to the database. The improvement effect is very significant. . memcache is a set of open source software, released under the authorization of the BSD license

  • The following is the official website of memcache (http://memcached.org/)

Insert picture description here

1.2: The workflow of memcached

  • work process:
    • First check whether the client's requested data is in memcached. If so, directly return the requested data without performing any operations on the database. If the requested data is not in memcached, check the database and return the data obtained from the database to The client, at the same time, caches a copy of the data in memcached (the memcache client is not responsible and needs to be implemented explicitly by the program);
    • Every time the database is updated, the data in memcached is updated to maintain consistency; when the memory space allocated to memcached is used up, the LRU (Least Recently Used) strategy plus the expiration failure strategy will be used. The failure strategy is first Replace, and then replace the recently unused data

1.3: Features of memcached

  • Memcached is essentially a memory key-value cache, which accelerates dynamic web applications by reducing the load on the database.

  • Memcached does not support data persistence, and all data is lost after the server is shut down.

  • The memcached protocol is simple and uses a text line-based protocol.

  • Memcached is a multi-threaded work, redis is a single-threaded work, each memcached server does not communicate with each other, each accesses data independently, and does not share any information

  • The memcached server does not have distributed functions, and distributed deployment depends on the memcache client

  • memcached uses lazy expiration to achieve key expiration

  • Memcached guarantees operating performance by allocating memory in advance, and memory fragmentation rarely occurs

  • Memcached uses non-blocking IO to take the network model, the purpose is to improve data throughput

  • memecached uses the multi-threaded model of listen/work. The advantage is that it can make full use of multi-core, but it will bring some lock conflicts.

  • Why doesn't memcached support persistence and the analysis of complex data structures?

    • The business determines the technical solution. Memcached is designed to "manage KV memory in a service mode, not a library mode". It is different from other cache databases in the KV memory management component library. Persistence and complex data structures are not Its original intention

1.4: usage scenarios of memcached

  • Data query cache: load the data of the database to memcached to provide the access speed of the program

  • Counter scenario: through the incr/decr command to realize the number of comments, click count, number of operations and other scenarios

  • Optimistic lock implementation: For example, in the scenario of multi-instance deployment of scheduled tasks, non-repetitive execution is achieved through CAS

  • Anti-duplicate processing command: CAS command

  • The difference between cluster and distributed: A cluster can deploy multiple services with the same configuration on a single machine or multiple computers; a distributed deployment of multiple different services on multiple computers

1.5: Consistent Hash algorithm used in distributed/clustered redis/memcached/kafka/hadoop/mycat

  • When the cluster increases or decreases machines, if the hash algorithm is used, a large area of ​​cache will be insufficient, causing the database server to crash, so a consistent hash algorithm appears at this time. The data is stored in a node after the hash modulus, but the consistent hash algorithm cannot solve the problem of load balancing because the data itself is not balanced. So the enhanced version is to add virtual nodes, because the more virtual nodes, the data is as uniform as possible. However, virtual nodes need to be maintained, and there is an upper limit (2^32). The consistent hash algorithm cannot completely achieve uniform data.

Two: deploy memcached

2.1: Experimental environment

  • Two centos 7.6

    • memcached server: 20.0.0.51, installation service: libevent-2.1.8-stable.tar.gz

      memcached-1.5.6.tar.gz

    • lamp server: 20.0.0.52, installation service: memcached-1.5.6.tar.gz

      httpd-2.4.29.tar.bz2

      apr-1.6.2.tar.gz

      apr-util-1.6.0.tar.gz

      mysql-5.6.26.tar.gz

      php-5.6.11.tar.bz2

2.2: Experimental schematic diagram

Insert picture description here

  • The memcached server provides a cache database, installs memcache on the LAMP architecture host, and memcache provides an API interface. Call this interface on the php on LAMP to cache the data in memcached

2.3: Configure memcached server

  • Modify hostname
hostnamectl set-hostname memcached
  • Install event notification library, libevent (can be downloaded on the memcached official website)
'解压数据包'
tar xzvf libevent-2.1.8-stable.tar.gz -C /opt
cd /opt/libevent-2.1.8
'安装环境源'
yum install -y gcc gcc-c++ make 
'编译、安装libevent'
./configure --prefix=/usr/local/libevent
make && make install

Since memcached installation depends on libevent, libevent must be installed first

  • Install memcahed
'解压软件包' 
tar xzvf memcached-1.5.6.tar.gz -C /opt
'手工编译安装'
cd /opt/memcached-1.5.6/
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
make && make install

'优化memcached的执行文件'
ln -s /usr/local/memcached/bin/* /usr/local/bin

'开启memcached服务'
memcached -d -m 32m -p 11211 -u root
'-d守护进程;-m指定缓存大小;-p指定端口;-u指定用户'

'验证进程是否开启'
netstat -natp | grep 11211
[root@memcahced ~]# netstat -ntap |grep memc
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      20076/memcached     
tcp6       0      0 :::11211                :::*                    LISTEN      20076/memcached     

'关闭防火墙和SElinux功能'
systemctl stop firewalld.service 
setenforce 0

'使用telnet连接memcached数据库'
'查看是否存在telnet功能,安装Telnet'
rpm -q telnet
yum install telnet -y

'连接数据库'
[root@memcahced ~]# telnet 192.168.43.101 11211
Trying 192.168.43.101...
Connected to 192.168.43.101.
Escape character is '^]'.
  • Command operation of memcached cache database
Grouping command description
Storage command set Used to store the value in the specified key. The key already exists, update the original data corresponding to the key
add Used to store the value in the specified key, if it exists, it will not be updated
replace Replace the value of the existing key, if it does not exist, the replacement fails
append The command is used to append data to the value of the existing key
prepend Append data to the value of the existing key
case Compare and replace, after comparison, can only be written without being modified by other users
Search command get Get the value stored in the key, if it does not exist, return empty
gets Get the value stored with the CAS token, if the key does not exist, the return is empty
delete delete Delete the existing key
Calculation incr/decr Increment or decrement the data value of the existing key
statistics stats Return statistical information such as PID, version number, number of connections, etc.
stats items Display the number and storage duration of items in each slab (the number of seconds since the last visit)
stats sizes Display the size and number of all items
stats slabs Display the information of each slab, including the size, number, usage, etc. of the chunk
Clear flush_all Clear all content

2.4: Configure LAMP and memcache

  • Modify hostname
hostnamectl set-hostname lamp
  • Configure the LAMP architecture (manually compiled), see my previous blog for specific configuration

Link: https://blog.csdn.net/m0_47219942/article/details/107779877 .

  • Configure memcache
yum install autoconf -y
'解压memcache软件包'
[root@localhost ~]# tar xf memcache-2.2.7.tgz -C /opt/
手工编译安装
[root@localhost ~]# cd /opt/memcache-2.2.7/
[root@localhost memcache-2.2.7]# /usr/local/php5/bin/phpize
'配置,开启memeche,增加PHP模块'
[root@localhost memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/usr/local/php5/bin/php-config
[root@localhost memcache-2.2.7]# vim /usr/local/php5/php.ini
'添加'
extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/"
extension = memcache.so			'指向memcache模块'
[root@localhost memcache-2.2.7]# cd /usr/local/httpd/htdocs/
'编辑php测试页面'
[root@localhost htdocs]# vim index.php 
<?php
$memcache = new Memcache();
$memcache->connect('20.0.0.52',11211);
$memcache->set('key','Memcache test Successfull!',0,60);
$result = $memcache->get('key');
unset($memcache);
echo $result;
?>
[root@localhost htdocs]# service httpd restart

2.5: Verify configuration

  • The following screen appears, indicating successful deployment
    Insert picture description here

Guess you like

Origin blog.csdn.net/m0_47219942/article/details/108537888