Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

Time:2020-3-11

1、 Introduction to basic concepts and features of redis

1.1 introduction to basic concepts of Redis

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

1. Redis is remote, with client and server. We generally say server;

2.Redis isMemory basedYes, so it’s much faster than MySQL based on hard disk, but it’s very memory hungry

3. Redis is a non relational database. In essence, it is also a database, but when MySQL relational database is stored, data dictionary must be defined, while redis does not.

1.2 comparison between redis and memcached

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

    RedisAll data types supportpush/pop、add/removeAnd take intersection Union and difference sets and more abundant operations, and these operations are atomic.

AndMemcachedSimilarly, in order to ensure efficiency, data is cached in memory. The difference isRedisPeriodically write the updated data to the disk or write the modification operation to the additional record file, and on this basis, implementmaster-slave(master-slave) synchronization.

1.3 redis supports master-slave synchronization.

Data can be synchronized from the master server to any number of slave servers, and the slave server can be the master server associated with other slave servers. This makesRedisSingle level tree replication can be performed. Saving can write data intentionally or unintentionally. Because it’s fully realizedPublish / subscribeWhen the slave database synchronizes the tree anywhere, it can subscribe to a channel and receive the complete message publishing record of the master server. Synchronization is helpful for scalability and data redundancy of read operations.

2、 Redis application scenario

2.1 cache

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

RedisOften acted asData cache

When some system interfaces are slow, we can put some data in theRedisIt will not take a long time to fetch data next timeSQLOperation, directly from the cache to get data, improve system performance is one of the most commonly used methods;

2.2 queue

Push and pop operations ensure the realization of atomicity;

2.3 data storage

All the additions, deletions and modifications are inRedisIn progress,RedisThe persistence mechanism of hard disk ensures the integrity and security of data.

ViewredisClient:which redis-cli
Login:redis-cli(default login local6379Port)
    infoCommand viewredisinformation

3、 Redis data type

InredisInside, except\nAnd spaces can’t be used as the composition of names, and other contents can be used askeyThe name part of. Name length is not required.

*Exists key test whether the specified key exists
Del key1 key2 Key3 delete the given key
*Type key returns the type of the given value
Keys pattern returns all keys matching the specified pattern
Rename oldkey newkey
*Dbsize ා returns the number of keys in the current database
Expire key seconds specifies the expiration time for the key
*TTL key ා return the remaining expiration seconds of the key
*Select DB index - select 16 databases in total
Move key DB index ා move the key from the current database to the specified database
*Flushdb - delete all keys in the current database
Flush - delete all keys in all databases

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

3.1 operation of string type

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

Function: provide string or integer operation.

set key value(string/int/float)
get key
Incr string [value] // Auto increment
Decrby string [value] // Auto decrement

String is the most basic type of redis

    redisOfstringIt can contain any data. IncludejpgPicture (binary) or serialized object.
SinglevalueThe maximum value is1GByte.
    If onlystringType,redisIt can be seen as amemcache

Set key value ා set the value corresponding to the key to the value of string type
*Mset key1 value1 key2 Value2 sets the value of multiple keys at a time
*Mget key1 key2 ා get the value of multiple keys at one time
*Incr key ා add the value of key and return the new value
Decr key ා decrements the value of key and returns a new value
Incrby key integer ා the same as incr, plus the specified value
Decrby key integer is the same as decr, minus the specified value
*Append key value ා append value to the string value of the specified key
*Substr key start end returns the string value of the intercepted key

3.2 list type

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

Function: store sequence collection, which can be used for queues,pushandpopoperation

Lpush left in
Rpop right out
There are several elements in the len key view list

    ListThe type is first in, first out, suitable for the queue;List does not require that the elements in it are unique

(1) Introduction

Get the latest 10 login user information:select * from user order by logintime desc limit 10;
The above SQL statements can meet the needs of users, but when there is a lot of data, all the data will be affected, and the load on the database is relatively high. If necessary, the key fields (ID or logintime) need to be indexed, which also consumes system resources.

If the above functions are realized through the list list list, only the latest 10 data can be kept in the list list, and each new data will delete one old data. You can get the required data directly from the linked list every time. Greatly save all aspects of resource consumption

(two) application

    listType is actually a two-way linked list. adoptpush,popOperation to add or remove elements from the head or tail of the list.
This makeslistIt can be used either as a stack or as a queue.

Up in up out: stack
Up in down out: queue

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

Lpush key string ා add string elements to the header of the list corresponding to the key
Rpop key ා delete the element at the end of the list and return the delete element
Len key returns the length of key ා corresponding list. If the key does not exist, return 0. If the key corresponding type is not list, return an error
Lrange key start end ා returns the elements in the specified range, with the subscript starting from 0
Rpush key string ා same as above, add at the end
Lpop key ා delete the element from the list header and return the delete element
Ltrim key start end ා intercept the list and keep the elements in the specified interval

1.lrange key 0 -1Remove all elements.
2.lrem key n/-n XDelete specified elementnSecond time.
3.linsert key after/before key1 key2staykey1Insert back / frontkey2
4.rpoplpush key1 key2takekey1RightkeyLeft entrykey2Linked list,Atomicity
Operation and use of

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

Function: unordered set,Every element is different

Add element: Sadd key value
Determine how many elements there are: scar key
Determine whether an element is in the set: sismember key value
Delete an element: SREM key value
Get all the values and return them as an array: $redis - > smembers ('dst ');
int(2)
array(2) {
  [0]=>
  string(1) "4"
  [1]=>
  string(1) "2"
}

3.3 set type

  setType, disorderly storage of multiple different elements, elements can be quickly added, found and deleted;The same element is only one

The set of redis is an unordered set of string type.
The set element can contain a maximum of (2’s 32-1) elements.
In addition to the basic add and delete operations, the set set type,
Other useful operations include collection fetchingUnion(union),intersection(intersection),    Difference set(difference) Through these operations, it is easy to implement theFriend recommendation
Note: each element in each collection cannot be repeated.

Sadd key member ා add a string element to the set set set corresponding to the key, and return 1 successfully. If the element is already in the set, return 0. There is no return error in the set corresponding to the key
SREM key member [MEMBER] ා remove the given element from the key corresponding set and return 1 successfully
Smove P1 P2 member? Remove the member from P1 corresponding set and add it to P2 corresponding set
Scar key ා returns the number of set elements
Sismember key member? Judge whether the member is in the set
Single key1 key2... Keyn ා returns the intersection of all given keys
Sun ion key1 key2... Keyn ා returns the union of all given keys
Sdiff key1 key2... Keyn ා returns the difference set of all given keys
Smembers key returns all elements of the set corresponding to the key. The result is unordered

This type of application:qqFriend recommendation.

Tom's circle of friends: Mary Jack Xiaoming wang5 wang6 
Linken circle of friends (friends with XXX): Yuehan Daxiong Luce wang5 wang6

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

3.4 hash type

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

Function: storedkey-valueStructure,keyMust bestring

    It is often used to store more complex structures. As long as hash has different keys, such as name, age, etc

Insert / modify: hset hASH1 key1 12
Get: hget hASH1 key1
Check the hash length: HLEN key
Get once: hmget hASH1 key1 key2

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

3.5 sort set sort set type

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

Function: storage is an ordered set with scores,Often used in leaderboards

    Sort set is often used for ranking, such as class grade ranking

Storage, like hash, is a mapping. What is stored is the mapping of fractions to elements. You can see it’s a leaderboard.
    Value must be globally unique.
    Score is a floating-point type.
    Sort set type operation. If the scores of two elements are the same, they are arranged in byte order

Add / modify: zadd zset1 10.1 val1
Number of Views: zcard zset1
View rankings: zrange zset1 0 2 WithCores
View a value ranking: zrank zset1 val2

andsetequallysorted setAlsostringThe difference is that each element is associated with apower。 The elements in the set can be acquired orderly by weight.

    TheSort setType suitable for occasion:
Get popular posts (replies) information:select * from message order by backnum desc limit 5;
(the above requirements can be achieved through simplesqlStatement implementation, butsqlStatement comparison costmysqlDatabase resources)

Case: using sort set to get the most popular top 5 posts

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

Each element in the sorting set is a combination of value and weight (each element of the previous set set type is just a value)

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

We only make a sort set sorting set, and only five elements are kept in it, which are the highest reply quantity
When each post is replied, it has the chance to enter the collection, but only the top five posts with the highest reply amount will exist in the collection, and the ones with the lowest reply amount will be deleted.

127.0.0.1:6379> select 2
OK
127.0.0.1:6379 [2] > zadd hotmessage 102 11 // add
(integer) 1
127.0.0.1:6379[2]> zadd hotmessage 141 12
(integer) 1
127.0.0.1:6379[2]> zadd hotmessage 159 13
(integer) 1
127.0.0.1:6379[2]> zadd hotmessage 72 14
(integer) 1
127.0.0.1:6379[2]> zadd hotmessage 203 15
(integer) 1
127.0.0.1:6379[2]> keys *
1) "hotmessage"
127.0.0.1:6379[2]> zrevrange hotmessage 0 100
//Obtain the information of specific element values in the order of weight from high to low
1) "15"
2) "13"
3) "12"
4) "11"
5) "14"
127.0.0.1:6379[2]> zadd hotmessage 189 16
(integer) 1
127.0.0.1:6379[2]> zrevrange hotmessage 0 100
1) "15"
2) "16"
3) "13"
4) "12"
5) "11"
6) "14"
127.0.0.1:6379[2]> zremrangebyrank hotmessage 0 0
(integer) 1
//To delete the lowest right 0 means to delete the first right; 0 1 means to delete the first right and the second right;
127.0.0.1:6379[2]> zrevrange hotmessage 0 100
1) "15"
2) "16"
3) "13"
4) "12"
5) "11"
127.0.0.1:6379[2]> zrank hotmessage 12
//Get element ranking from low to high
(integer) 1
127.0.0.1:6379[2]> zrank hotmessage 11
(integer) 0
127.0.0.1:6379[2]> zrevrank hotmessage 13
//Get element ranking from high to low
(integer) 2
127.0.0.1:6379[2]> zcard hotmessage
//Get the number of elements in the collection
(integer) 5
127.0.0.1:6379[2]> zscore hotmessage 16
//Get the corresponding weight according to the element value
"189"
127.0.0.1:6379[2]> zincrby hotmessage 100 12
//Add some weight information to the specified element value
"241"
127.0.0.1:6379[2]> zrevrange hotmessage 0 100
1) "12"
2) "15"
3) "16"
4) "13"
5) "11"
127.0.0.1:6379[2]>

4、 Persistent operation

    RedisIn order to ensure the security of internal data, we willFile formSave it to a hard disk. After the server restarts, it will automatically restore the hard disk data to memory(redis)Inside. The process of saving data to a hard disk is calledPersistenceEffect

    Relevant instructions:

. / redis cli bgsave // save data to disk asynchronously (snapshot save)
./redis-cli 1h 127.0.0.1 -p 6379 bgsave
. / redis cli lastsave // returns the UNIX timestamp of the last successful save to disk
. / redis cli shutdown // synchronously save to the server and shut down the redis server
. / redis cli bgrewriteaof // optimize AOF log file storage when log file is too long

4.1 snapshot persistence

The persistence is enabled by default, and theredisAll the data in is saved in a hard disk, if there is a lot of data(10-20G)It is not suitable for frequent persistence operations.

    1. Snapshot persistence data backup file reserved on local hard disk

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

    2. Backup frequency of snapshot persistence:

In the redis.conf configuration file

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

If more than one key is modified in save 900 1 񖓿 900 seconds, snapshot saving is initiated 
Save 300 10 ා 300 seconds more than 10 keys are modified to initiate a snapshot 
Save 60 10000 - more than 10000 keys are modified in 60 seconds, and a snapshot is initiated

The above three save means:
    The frequency of data modification and backup is very high
    Low frequency of data modification and backup

    3. Name and storage location of snapshot persistence file:

In the redis.conf configuration file

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

    4. Initiate snapshot persistence manually

./redis-cli -h 192.168.10.138 -p 6379 bgsave
//Snapshot persistence initiated by given IP address
. / redis cli bgsave // local snapshot persistence

4.2 append only file (AOF persistence)

    Nature:Every “write” instruction executed by the user(Add, modify, delete)All of them are backed up to files. When restoring data, specific write instructions are executed.

    1. Enable AOF persistence(Data inside redis will be cleared):
(at the same time, you can change the name of the backup file, which is appendonly. AOF by default.)
In the redis.conf configuration file

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

    2. If the configuration file is modified, you need to delete the old process and start a new process according to the new configuration file:

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

    3. After the new process is started, you will see the corresponding AOF persistent backup file appendonly.aof:

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

    4. AOF adds persistent backup frequency:
In the redis.conf configuration file

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

    5. Optimize the AOF backup file
Optimize and compress the content of appendonly.aof file. (for example:MultipleinrCommand becomes asetInstruction)

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

    6. Persistence related instructions

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

5、 The master-slave mode of redis

5.1 background

In order to reduceLoad of each redis server, you can set up several more servers, and take the initiative to load “write” (add, modify, delete) data from one server, and load “read” data from other servers.The master server data is “automatically” synchronized to the slave server.

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

5.2 setting master-slave mode

    1. From the redis server, find the configuration fileredis.conf

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

The default is to turn off master-slave mode. After modification

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

    2. close fromRedisServer, reload profile

./redis-cli shutdown
./redis-server redis.conf

    3. The write operation is disabled by default from the server (you can modify the parameters as shown in the figure below to make it possible to write data):

    Basis of redis: basic introduction, application scenario of redis, five data types, persistent operation, master-slave mode

Redis master slave copy sentinel mode

Install redis under windows and the extension of phpredis (windows redis PHP & php7)

6、 Create a client to connect to redis

    Connectredis
Createconnect.phpfile

<?php
$redis = new \ redis(); // instantiation
$redis->connect('127.0.0.1',6379);

//String operation
$redis->delete("string1");
$redis->set("string1","val1");
$val = $redis->get("string1");
var_dump($val); //val1

$redis->set("string1",4);
$redis->incr("string1",2);
$val = $redis->get("string1");
var_dump($val); //6


#String stores key / value, which is equivalent to setting a variable. For example, perform incr operation on a key value, and count the real-time hits. Set/get

#Hash is used to store object information. A key is an object. Field / value constitutes a key value pair. hset key field value 

#List (lpush / lrange / lpop)

#Set is used for intersection and union, for example, Sadd / smembers / Sunion / singer, the common concern of sina Weibo 

#Ordered set (Zset) is used for sorting, for example, getting top n zadd / zrangebycore

    Command memory summary:

String and hash are key / value related and created with set. set/hset/hmset

Set and ordered set are set related and created with add. Sadd/zadd

The list is created with push. Lpush

finish

References are as follows:
1. Redis basic video tutorial
2. Redis podcast learning notes