当前位置:网站首页>Application practice of memcached cache database

Application practice of memcached cache database

2021-01-23 16:42:15 Ma Nong blossoms

1.1 Database comparison

cache : Store data in memory , Only when the disk is not up to it , Will enable caching

   shortcoming : Loss of power-off data ( Double electricity ), The purpose of using cache to store data is only to cope with large concurrent business .

database : mysql( Relational database , Can guarantee data consistency , Ensure that data is not lost , When it comes to too many functions , Leading to poor performance ) === Data reference

Cache database : memcache redis( Non relational database , Extremely high performance , But data integrity is not guaranteed ) === The data provider of the business

     memcachedb Will write data from memory to disk

       redis The main workplace is in memory , But regularly back up memory data to the hard disk

1.1.1 The choice of database

   data storage , Data warehouse selection mysql This kind of disk database

   High concurrency , Big business applications memcache This kind of memory database

1.1.2 Database classification

   Relational database mysql

   Non relational database (NOSQL) memcached redis MongoDB

1.2 memcached Introduce

Memcached It's an open source 、 High performance pure memory cache service software .Mem Memory means ,cache Caching means ,d yes daemon It means .

  memcache Is the project name , It's also a piece of software , The architecture is C/S**** framework .

​ memcached Official website :http://memcached.org/

1.2.1 memcache advantage

① For users , Users visit the site faster , The experience is better .

② For websites , Database pressure has been reduced . Only when there is no data in the memory will the database be requested . The data written for the first time will also request the database . The general company didn't warm up , Only when the user has read the database will it be put into Memcached in .

② Improved concurrent access to the site , Reduce the number of servers .

1.3 Memcached Use scenarios in the enterprise

1.3.1 As the front-end cache application of database

    When the database (mysql) Can't handle large concurrent requests , You can cache data into memory ( Cache database ), Then it can be solved

  As the front-end cache of the database, the biggest purpose : Reduce the pressure of database access

1.3.2 As the back end of the cluster session Conversation retention

  session Store in file , database ,memcache, Or memory, etc ,

   cookie On the client browser .

   session Is a file that exists on the server similar to a hash table . It contains the information we need , We can take it out when we need it .

   session rely on cookie There is , After the request client reaches the server , The server will randomly generate a string , As the identity of the user , The string passes through cookie Return to the client , The client browser takes this string as the key Put it in session id Inside , Random string key There can be no value in it . If the user submits again , The user information such as user name and password in the request information is saved in the random string value in , The request arrives at the server , The username and password are correct , Random strings are authorized , Give a mark to sessionid Of random strings in value in , Prove that the user is already logged in , The client again accesses the server with the random string , The server will know that the user has logged in without verification , Return the requested information directly .

session and cookie difference

  1、cookie The data is stored in the user's browser ,session The data is stored on the server

  2、cookie In the local browser , It can be extracted and analyzed , Poor safety . For the sake of safety , Login account and other information can be cached in session in .

  3、session It will be saved on the server for a certain period of time , Increased access puts pressure on the server , You can use the caching tool , Such as memcache etc.

1.3.3 How to judge user information in website development

   The initial technical approach : The server writes one in your browser cookies, This cookies It contains your user name and login information . because cookies Is stored in a local browser , So third party tools are easy to steal cookies Information .

In the beginning :

cookies cookies name : Content ( user name , login information )

After improvement :

The local browser stores :

cookies cookies name : Content (session id Number )

The server stores :

session session id: Content ( user name , login information )

Mainstream usage scenarios :cookies + session

1.3.4 session Different solutions shared

   1、session Document supply NFS share

  2、session Document supply rsync scp share

   3、 take session The content of is stored in the database (mysql) in , All machines can go through ip:port Read

  4、 take session The contents of are stored in the cache database , All machines can go through ip:port Read

   benefits : Take advantage of the blackout 、 Restart the lost data feature . Clean up the data regularly ; Improve concurrency

1.3.5 memcache Principle advantages

   start-up Memcached " , According to the specified memory size parameter , Will be allocated a memory space . When we read all kinds of business data in the database , The data will be put in at the same time Memcached In cache ,, The next time a user requests the same data , The program goes straight to Memcached Take the data and return it to the user .

advantage :

  ① For users , Users visit the site faster , The experience is better .#

  ② For websites , Database pressure has been reduced . Only when there is no data in the memory will the database be requested . Data written for the first time Also requests the database . The general company didn't warm up , Only , Only after the user has read the database can he put it in Memcached in .

  ③ Improved concurrent access to the site , Reducing the number of servers is the most .

img

Schematic diagram

1.4 Memcached Distributed cache cluster

  memcached Naturally, distributed clustering is not supported , Distributed storage needs to be supported by programs

1.4.1 Memcached The characteristics of distributed cache cluster

   1. all MC The contents of server memory are different . These server contents add up close to the capacity of the database . such as 1T The database of , The memory of a cache database is not that large , So it's divided into 10 Cache servers .

  2. On the client side (Web) Procedure or MC On the load balancer HASH Algorithm , Assign the same content to one MC The server .

   3. ordinary HASH The algorithm brings a lot of data flow for node downtime ( invalid ), May cause avalanche effect .

  4. Uniformity HASH It can make the node down and affect the data flow of the node ( invalid ) To the minimum .

*** ordinary hash**** Algorithm *

img

   First of all, will key Deal with it as a 32 A string , Take before 8 position , after hash The calculation is processed as an integer and returns , Then map to one of the servers to get the configuration of one of the servers , Use this configuration to complete distributed deployment . Without changing the number of servers , Ordinary hash Distribution works well , When the number of servers changes , Here's the problem . Just imagine , Add a server , The same key after hash after , The result of the model fetching with the server is definitely different from the result before it is added , And that leads to this , Previously saved data is missing .

*** Uniformity hash**** Algorithm *

img

Consistent hash algorithm

   advantage : In distributed cache In cache , One of them went down , transfer key Highest efficiency

   Sort the list of servers , according to mHash($key) Match adjacent servers

Uniformity hash**** Algorithm Minimize data flow

Reference material

http://blog.csdn.net/cywosp/article/details/23397179
http://blog.csdn.net/zhangskd/article/details/50256111

The first 2 Chapter memcached Use

2.1 install memcached

2.1.1 Environmental statement

[root@cache01 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core) 
[root@cache01 ~]# uname -r
3.10.0-693.el7.x86_64
[root@cache01 ~]# getenforce
Disabled
[root@cache01 ~]# systemctl status firewalld.service 
 firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@cache01 ~]# hostname -I
10.0.0.21 172.16.1.21

2.1.2 install memcached

[root@cache01 ~]# yum -y install memcached

2.1.3 Check the configuration

[root@cache01 ~]# cat /etc/sysconfig/memcached 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

2.1.4 Look at the startup script

[root@cache01 ~]# cat /usr/lib/systemd/system/memcached.service 
[Unit]
Description=Memcached 
Before=httpd.service
After=network.target

[Service]
Type=simple
EnvironmentFile=-/etc/sysconfig/memcached
ExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS

[Install]
WantedBy=multi-user.target

2.1.5 Start the service

[root@cache01 ~]# systemctl start memcached.service

2.2 management memcached

2.2.1 memcached Database syntax format

set              key   0        0           10

<command name>  <key> <flags> <exptime> <bytes>\r\n

\n Wrap the line and move the cursor to the beginning of the line

\r Move the cursor to the beginning of the line , But don't wrap

Parameters explain
<flags> When you retrieve the content , Save any... On the server with the data and send block 16 Bit unsigned shaping ( Write in decimal ). The client can use it as “ Bit field ” To store specific information ; It's opaque to the server .
<exptime> It's the end time . If 0, The item never expires ( Although it may be deleted , To make room for other cache items ). If the 0(Unix Time stamp or second offset of the current time ), After the end time , The client can no longer get this content
<bytes> Is the byte length of the subsequent data block , Excluding... For paging “\r\n”. It can be 0( This is followed by an empty data block ).
<data block>\r\n <data block> It's a lot of 8 Bit data , Its length is determined by... In the previous command line <bytes> Appoint .

2.2.2 Database usage

Write read data

[root@cache01 ~]# printf "set key008 0 0 10\r\noldboy0987\r\n"|nc 10.0.0.21 11211
STORED
[root@cache01 ~]# printf "get key008\r\n"|nc 10.0.0.21 11211
VALUE key008 0 10
oldboy0987
END

Write data length does not match , The definition is too big

[root@cache01 ~]# printf "set key009 0 0 11\r\noldboy0987\r\n"|nc 10.0.0.21 11211
[root@cache01 ~]# printf "get key009\r\n"|nc 10.0.0.21 11211
END

Write data length does not match , The definition is too small

[root@cache01 ~]# printf "set key010 0 0 9\r\noldboy0987\r\n"|nc 10.0.0.21 11211
CLIENT_ERROR bad data chunk
ERROR
[root@cache01 ~]# printf "get key010\r\n"|nc 10.0.0.21 11211
END

timeliness

[root@cache01 ~]# printf "set key011 0 10 10\r\noldboy0987\r\n"|nc 10.0.0.21 11211
STORED
[root@cache01 ~]# printf "get key011\r\n"|nc 10.0.0.21 11211
VALUE key011 0 10
oldboy0987
END
[root@cache01 ~]# printf "get key011\r\n"|nc 10.0.0.21 11211
END

Delete data

[root@cache01 ~]# printf "delete key008\r\n"|nc 10.0.0.21 11211
DELETED
[root@cache01 ~]# printf "get key008\r\n"|nc 10.0.0.21 11211
END

2.3 memcache php Version client installation uses

Command set

# Compile it php_mem
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5
/application/php/bin/phpize
./configure --enable-memcache --with-php-config=/application/php/bin/php-config --with-zlib-dir
make
make install
#  Activate php_memcached
sed -i '$a extension=memcache.so' /application/php/lib/php.ini
pkill php
/application/php/sbin/php-fpm -t
/application/php/sbin/php-fpm
/application/php/bin/php -m|grep memcache

Check the current environment

see php Module

img View Code see php Module

* Execution process *

Compilation and installation

[root@web06 memcache-2.2.5]# make install
Installing shared extensions:     /application/php-5.5.32/lib/php/extensions/no-debug-non-zts-20121212/
[root@web06 memcache-2.2.5]# ls /application/php/lib/php/extensions/no-debug-non-zts-20121212/
memcache.so
[root@web06 memcache-2.2.5]# sed -i '$a extension=memcache.so' /application/php/lib/php.ini
[root@web06 memcache-2.2.5]# pkill php
[root@web06 memcache-2.2.5]# /application/php/sbin/php-fpm -t
[17-Nov-2017 11:39:13] NOTICE: configuration file /application/php-5.5.32/etc/php-fpm.conf test is successful

[root@web06 memcache-2.2.5]# /application/php/sbin/php-fpm
[root@web06 memcache-2.2.5]# /application/php/bin/php -m|grep memcache
memcache

2.3.1 Write test files

[root@web01 blog]# cat /application/nginx/html/blog/mc.php
<?php
    $memcache = new Memcache;
    $memcache->connect('10.0.0.21', 11211) or die ("Could not connect");
    $memcache->set('key20171117', 'hello,world');
    $get_value = $memcache->get('key20171117');
    echo $get_value;
?>

Browser access

img

Database read test

[root@cache01 ~]# printf "get key20171117 \r\n"|nc 10.0.0.21 11211 
VALUE key20171117 0 11
hello,world
END

2.4 web management memcached

   The software used memadmin

   Official website :http://www.junopen.com/memadmin/

Put the package in the site directory , Browser to access it

[root@web06 tools]# tar xf memadmin-1.0.12.tar.gz -C /application/nginx/html/blog/

The default username and password are admin

img

Add a new memcached The server

img

web Interface management in Chinese , It's simpler

img

2.5 memcached Data caching

By program

2.5.1 blog Site implementation memcached Storage

[root@web06 ~]# cat /application/nginx/html/blog/wp-content/object-cache.php

img

2.6 memcached session share

Method 1:

   By program ,web01 Just go to memcahce Write session,web02 from memcahce read session**( More universal )**

Method 2:

   adopt php Configuration file for , Give Way php By default session Stored in a file , Change to store in memcached in

sed -i 's#session.save_handler = files#session.save_handler = memcache#;$a session.save_path = "tcp://10.0.0.21:11211"' /application/php/lib/php.ini

   Use this function , Need to use php Of session function

author : a handsome young man Source :http://clsn.io

Welcome to the official account 【 Magnon blossoms 】 Learn and grow together I'll share it all the time Java dried food , We will also share free learning materials, courses and interviews reply :【 Computer 】【 Design patterns 】【 interview 】 There's a surprise

版权声明
本文为[Ma Nong blossoms]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210123163949220L.html