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

Deploy Memcache services

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

Share

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

Blog structure

Introduction to Memcache

Memcache workflow

Memcache scheduling algorithm

Principle of Memcache implementation

Install Memcache

I. introduction to Memcache

Memcache is a free, open source, high-performance, distributed cache system. Because Memcache reduces the number of times the database is read by caching data and objects in memory. At present, it is used by many websites to improve the access speed of the website, especially for some large websites that need to visit the database frequently.

Memcache is a distributed cache system, which can improve the speed of website access, especially for some large companies or websites that frequently access the database. Memecache is an open source free software. Memcache caches data in memory by key-value pairs, reducing the number of times to read data from the back-end database.

II. Memcache workflow

Although MemCache is called "distributed cache", MemCache itself has no distributed function, and MemCache clusters will not communicate with each other (in contrast, JBoss Cache, when a server has cache data updates, it will notify other machines in the cluster to update the cache or clear the cached data). The so-called "distributed" depends entirely on the implementation of the client program, just like the flow in the figure above. At the same time, based on this diagram, take a look at the process of writing cache once to MemCache:

(1) the application program inputs the data that needs to be written to the cache; (2) API inputs Key into the routing algorithm module, and the reason algorithm obtains the server number according to the list of Key and Memcache cluster servers; (3) gets the Memcache and its IP address and port number from the server number; (4) API calls the communication module to communicate with the specified number server, writes the data to the server, and completes a write operation of distributed cache. Third, Memcache scheduling algorithm remainder Hash

A simple routing algorithm can use the remainder Hash: divide the number of servers by the hash value of the cached data KEY, and the remainder is the subscript number of the server list. If the HashCode corresponding to a certain str is 52 and the number of servers is 3, take the remainder to get 1, and the str corresponds to the node Node1, so the routing algorithm routes the str to the Node1 server. Because of the strong randomness ratio of HashCode, the use of remainder Hash routing algorithm can ensure a more balanced distribution of cached data in the whole MemCache server cluster.

For example, if the cluster of Memcache servers has changed from 3 to 4, suppose there are 20 pieces of data with a HashCode of 0,19, as shown in the figure:

Now the capacity is expanded to 4, and the bold red indicates a hit:

In the website business, most of the business data operation requests are actually obtained through the cache, and only a small number of read operations will access the database, so the load capacity of the database is designed on the premise of cache. When most of the cached data cannot be read correctly because of the expansion of the server, the pressure of these data access falls on the database, which will greatly exceed the load capacity of the database, which may seriously lead to database downtime.

There is a solution to this problem, and the steps to solve it are:

(1) when the number of visits to the website is low, usually late at night, the technical team works overtime, expands capacity, and restarts the server. (2) gradually preheat the cache by simulating requests to redistribute the data in the cache server to the consistent Hash algorithm.

The consistent Hash algorithm implements the Hash mapping from the Key to the cache server through a data structure called the consistent Hash ring.

To put it simply, a consistent hash organizes the entire hash space into a virtual ring (this ring is called a consistent Hash ring). For example, assuming that the value space of a space hash function H is 0 ~ 2 ^ 32-1 (that is, the hash value is a 32-bit unsigned), the whole hash space is as follows:

Then each server uses H for a hash calculation. Specifically, you can use the server's IP address or hostname as the keyword, so that each machine can determine its position on the above hash ring and arrange it clockwise. Here, we assume that the positions of the three nodes memcache are as follows:

Next, the hash value h of the data is calculated using the same algorithm, and the position of the data on this hash ring is determined. if we have data A, B, C, D, four objects, the position after hash calculation is as follows:

According to the consistent hash algorithm, data An is bound to server01, D is bound to server02, and B and C are clockwise to find the nearest service node on server03.

The hash ring scheduling method thus obtained has high fault tolerance and scalability:

Suppose server03 crashes:

You can see that C and B are affected at this time, and the B and C nodes are relocated to Server01. In general, in a consistent hash algorithm, if a server is not available, the data affected is only the data between this server and the previous server in its ring space (that is, the first server encountered when walking counterclockwise). Nothing else will be affected. Consider another situation, if we add a server Memcached Server 04 to the system:

At this point, A, D, and C are not affected, only B needs to be relocated to the new Server04.

The consistent hashing algorithm only needs to relocate a small part of the data in the ring space for the increase or decrease of nodes.

It has good fault tolerance and scalability.

The disadvantage of consistent hash: when there are too few service nodes, it is easy to cause the problem of data skew due to the uneven division of nodes. We can solve the problem by adding virtual nodes. More importantly, the more cache server nodes in the cluster, the smaller the impact of adding / decreasing nodes, which is easy to understand. In other words, with the increase of the size of the cluster, the probability of continuing to hit the original cached data will increase. Although there are still a small number of scores cached in the server that cannot be read, the proportion is small enough that even if the database is accessed, it will not cause fatal load pressure on the database.

IV. MemCache implementation principle

The data of Memcache is stored in memory. 32-bit machines can only use 2GB's memory bow and arrow at most. 64-bit machines can be considered not to display.

Figure: Memcache's data storage mechanism (memory allocation mechanism): fixed space allocation

(1) Memcache divides the memory space into a set of slab

(2) there are several page under each slab, and each page defaults to 1m. If a slab occupies 100m of memory, then there should be 100m page under this slab.

(3) each page contains a set of chunk,chunk, where the real data is stored, and the size of the chunk in the same slab is fixed.

(4) slab with the same size chunk is organized together, which is called slab_class

The method of Memcache memory allocation is called allocator (allocation operation). The number of slab is limited, several, a dozen or dozens, which is related to the configuration of startup parameters.

Where the value in the Memcache is stored is determined by the size of the value, and the value is always stored in a slab that is closest to the size of the chunk.

The characteristics of Memcache: the speed of accessing database is faster than traditional relational database (because Memcache is stored in memory, while traditional relational database is stored in disk); the data of Memcache is stored in memory, which means that as long as Memcache restarts, the data will be lost. Since most of them are 64-bit operating systems, the impact of memory on 32-bit systems will not be discussed here; the workflow of memcache

1. Check whether the request data of the client is in the memcached. If so, return the request data directly without any operation on the database. The path is treated as ①②③⑦.

2. If the requested data is not in the memcached, check the database, return the data obtained from the database to the client, and cache a copy of the data to the memcached (the memcached client is not responsible and needs to be explicitly implemented by the program), and the path is operated as ①②④⑤⑦⑥.

3. Update the data in memcached every time you update the database to ensure consistency

4. When the memory space allocated to memcached is used up, the LRU (Least Recently Used, least recently used) policy plus expiration policy is used to replace the invalidated data first, and then replace the recently unused data.

Characteristics of Memcached

Text-based protocols: common protocols such as http, ftp and smtp are all based on line of text, which refers to the transmission of information in text.

Based on libevent event handling: libevent is a library developed in C language, kqueue of bsd system (BSD is a derivative version of unix), epoll of linux system, all data are stored in memory, data access speed block

All data is stored in memory, data access speed block

Distributed system

Each memcached server does not communicate with each other, accesses data independently and does not share any information. The server does not have the distributed function. The distributed deployment depends on the memcache client. The installation of Memcache is divided into two processes, the installation of memcache server and the installation of memcached client.

five。 Install Memcache

Memcache needs to be built with the help of LAMP or LNMP. This blog uses LNMP structure.

The installation environment is as follows: 192.168.148.129 Memcache192.168.148.130 php192.168.148.131 nginx192.168.148.136 mysql install nginx

Download the nginx package

[root@bogon] # useradd-M-s / sbin/nologin nginx [root@bogon ~] # yum-y install openssl-devel [root@bogon ~] # tar zxf pcre-8.39.tar.gz-C / usr/src [root@bogon ~] # tar zxf zlib-1.2.8.tar.gz-C / usr/src [root@bogon ~] # tar zxf nginx-1.14.0.tar.gz-C / usr/src [root@bogon ~] # cd / usr/src/nginx-1.14.0/ [root@bogon nginx-1.14.0] #. / configure-- prefix=/usr/local/nginx\-- user=nginx-- group=nginx-- with-http_dav_module\-- with-http_stub_status_module-- with-http_addition_module\-- with-http_sub_module-- with-http_flv_module-- with-http_mp4_module\-- with-pcre=/usr/src/pcre-8.39 -- with-zlib=/usr/src/zlib-1.2.8\-- with-http_ssl_module-- with-http_gzip_static_module & & make & & make install [root@bogon ~] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin [root@bogon ~] # nginx [root@bogon ~] # netstat-anpt | grep 80tcp 00 0.0.0.0 .0.0: * LISTEN 8460/nginx: master installs the PHP server

Download the php package

[root@localhost ~] # yum-y install libxml2-devel lzip2-devel libcurl-devel libmcrypt-devel openssl-devel bzip2-devel [root@localhost ~] # tar zxf libmcrypt-2.5.7.tar.gz [root@localhost ~] # cd libmcrypt-2.5.7/ [root@localhost libmcrypt-2.5.7] #. / configure-- prefix=/usr/local/libmcrypt & & make & make install [root@localhost ~] # tar zxf php-5.6.27.tar.gz [root @ localhost ~] # cd php-5.6.27/ [root@localhost php-5.6.27] #. / configure-- prefix=/usr/local/php5.6-- with-mysql=mysqlnd\-- with-pdo-mysql=mysqlnd-- with-mysqli=mysqlnd-- with-openssl-- enable-fpm-- enable-sockets\-- enable-sysvshm-- enable-mbstring-- with-freetype-dir-- with-jpeg-dir-with-png-dir-- with-zlib\-- with-libxml-dir= / usr-enable-xml-with-mhash-with-mcrypt=/usr/local/libmcrypt\-with-config-file-path=/etc-with-config-file-scan-dir=/etc/php.d\-with-bz2-enable-maintainer-zts & & make & & make install [root@localhost php-5.6.27] # cp php.ini-production / etc/php.ini [root@localhost php-5.6.27] # cp sapi/fpm/init.d. Php-fpm / etc/init.d/php-fpm [root@localhost php-5.6.27] # chmod + x / etc/init.d/php-fpm [root@localhost php-5.6.27] # chkconfig-- add php-fpm [root@localhost php-5.6.27] # chkconfig php-fpm on [root@localhost php-5.6.27] # cp / usr/local/php5.6/etc/php-fpm.conf.default / usr/local/php5.6/etc/php -fpm.conf [root@localhost php-5.6.27] # vim / usr/local/php5.6/etc/php-fpm.conf is modified as follows: pid = run/php-fpm.pid listen = 192.168.31.141vim 9000\\ Local ip address (do not use 127.0.0.1) pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 35 [root@localhost Php-5.6.27] # service php-fpm startStarting php-fpm done [root@localhost php-5.6.27] # netstat-anpt | grep php-fpmtcp 00 192.168.148.130 root@localhost php-5.6.27 9000 0.0.0.0 anpt * LISTEN 130988/php-fpm: mas installs the MySQL database

Download the mysql script and install it with one click

[root@bogon ~] # mysql-u root-p123mysql > create database testdb1;mysql > use testdb1;mysql > grant all on *. * to xws@'192.168.148.%' identified by '123456 create table test1 create table test1 (id int not null auto_increment,name varchar (20) default null,primary key (id)) engine=innodb auto_increment=1 default charset=utf8;mysql > insert into test1 (name) values (' tom1'), ('tom2'), (' tom3'), ('tom4'), (' tom5'); mysql > select * from test1 +-- +-- +-+ | id | name | +-+-+ | 1 | tom1 | 2 | tom2 | 3 | tom3 | 4 | tom4 | | 5 | tom5 | +-+-+ 5 rows in set (0.00 sec) nginx operation is as follows: [root@bogon nginx-1.14.0] # vim / usr/local/nginx/conf/nginx.conf / / modified (about 43 lines) Location / {root html Index index.php index.html index.htm;} location ~\ .php$ {root / var/www/html; fastcgi_pass 192.168.148.130 root 9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME / scripts$fastcgi_script_name; include fastcgi.conf } [root@bogon nginx-1.14.0] # nginx- tnginx: the configuration file / usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file / usr/local/nginx/conf/nginx.conf test is successful [root@bogon nginx-1.14.0] # nginx- s reload

The php operation is as follows:

[root@bogon ~] # mkdir-p / var/www/html [root@bogon ~] # vim / var/www/html/test.php [root@bogon ~] # vim / var/www/html/test1.php

The client access is as follows:

Install Memecache

Download memcache

[root@bogon ~] # tar zxf libevent-2.0.22-stable.tar.gz-C / usr/src/ [root@bogon ~] # cd / usr/src/libevent-2.0.22-stable/ [root@bogon libevent-2.0.22-stable] #. / configure & & make & & make install [root@bogon ~] # tar zxf memcached-1.4.33.tar.gz-C / usr/src/ [root@bogon ~] # cd / usr/src/memcached- 1.4.33 / [root@bogon memcached-1.4.33] #. / configure-- prefix=/usr/local/memcached\ >-- with-libevent=/usr/local/ & & make & & make install [root@bogon ~] # memcached- d-m 2048-l 192.168.1.7-p 11211-c 10240-P / usr/local/memcached/memcached.pid-u root [root@bogon ~] # netstat-anpt | grep 11211php operates as follows (install Memecache client) [root@bogon ~] # tar zxf memcache-3.0.8.tgz-C / usr/src/ [root@bogon ~] # cd / usr/src/memcache-3.0.8/ [root@bogon memcache-3.0.8] # / usr/local/php5.6/bin/phpize [root@PHP memcache-3.0.8] #. / configure-- enable-memcache\-- with-php-config=/usr/local/php/bin/php-config & & make & & make After install// is executed, it will show the path where memcache.so is stored [root@PHP ~] # echo "extension = / usr/local/php/lib/php/extensions/no-debug-zts-20131226/memcache.so" > > / etc/php.ini//. Fill in the path where memcache.so modules are stored in the PHP main configuration file. [root@PHP ~] # systemctl restart php-fpm [root@PHP ~] # vim / var/www/html/test2.php// this test script is Displays the version of memcached / / and inserts a key-value pair with a cache time of 600 seconds to "test=123" Its ID is "key"

The client access is as follows:

Install the telnet tool test on the PHP server

[root@PHP ~] # yum-y install telnet [root@PHP ~] # telnet 192.168.148.129 11211 / / Log in to port 11211 of memcached Trying 192.168.148.129...Connected to 192.168.148.129.Escape character is'^] .get key / / query the key-value pair whose ID is "key" You can see the "test=123" VALUE key 1 66O:8: "stdClass": 2: {Sv8: "str_attr" written by our test script SRV 4: "test"; SRV 8: "int_attr"; iRV 123 } END// needs to increase / / or revisit the save time value of the key-value pair inserted in the test2.php file when performing the above get verification, so as to avoid cache invalidation Unable to query quit / / exit the current environment Connection closed by foreign host. [root@PHP ~] # Test Memcache cache database [root@PHP ~] # vim / var/www/html/test4.php / / areas that often need to be modified have been marked! And this test script is accessed for the first time in Memcache software.

Second access (after refresh)

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