Redis common configuration and brief introduction

What is Redis

  • Redis is an open source software based on the BSD protocol. It can be used as a database, cache, and message queue.
  • Redis's processing data is all stored in memory, which greatly improves performance. In order to make data persistent, it provides two ways to write data to disk
  • Redis supports many data structures, such as: list, set, hashes, string, etc.
  • Redis has built-in master-slave replication and redis cluster to ensure the high availability of Redis
  • The transactional nature of Redis ensures that all its commands are executed in order. When a Redis transaction occurs, a request from another client will never occur, ensuring that the commands are individually isolated. Another characteristic of the atom is that either everything succeeds or none succeeds.

Install Redis

The epel source of centos7 contains the Redis package. Can be installed directly using yum

 yum install -y redis

Files after Redis installation

/etc/redis.conf   #Redis的配置文件

/usr/lib/systemd/system/redis.service   #Redis的服务
 
/usr/bin/redis-cli   #Redis的客户端
 
/var/lib/redis      #Redis默认存放的路径

Use Redis

Redis is a database based on KEY-VALUE. It supports many data structures, let’s talk about the commonly used

string

The data type supported by Redis, String. It can store up to 512M.

We use redis-cli to enter interactive mode

127.0.0.1:6379> help @string
#查询string的用法

SET
Syntax: SET key value [EX seconds] [PX milliseconds] [NX|XX]
the key used to set the string
EX seconds: expiration time

127.0.0.1:6379> SET name tangseng EX 20
OK
# 设置name键,值为tangseng , 20s后过期

GET
Syntax: GET key
Display the value of the key

127.0.0.1:6379> GET name
"tangseng"

INCR
Syntax: INCR KEY
Increase the value of a key by 1, which must be an integer

DECR
Syntax: DECR KEY
Decrease the value of a key by 1, which must be an integer

SETNX
Syntax: SETNX key value
if the created key does not exist, create it, if it exists, do not create it

127.0.0.1:6379> SETNX name ydong
(integer) 0
# 因为name存在,创建失败

127.0.0.1:6379> SETNX students xiaowang
(integer) 1

SETEX
grammar:SETEX key seconds value

Set key value and expiration time

SETEX name 20 ydong
OK

INCRBYFLOAT
grammar:INCRBYFLOAT key increment

Increment the floating point value of the key by the given amount

127.0.0.1:6379> INCRBYFLOAT number 0.2
"7.2"

127.0.0.1:6379> INCRBYFLOAT number 0.2
"7.4"

MGET
Syntax: MGET key [key ...]
Get all the values ​​of a given key

127.0.0.1:6379> MGET number students
1) "7.8"
2) "xiaowang"

MSET
Syntax: MSET key value [key value ...]
set the given key and key value

127.0.0.1:6379> MSET number 2 students dasheng
OK
127.0.0.1:6379> MGET number students
1) "2"
2) "dasheng"

list

listIt is a list function, like all the data is placed in a box, filled with data, then when we want to insert data, we can insert it from the left or from the right. Insert one at either end and squeeze out the data at the other end. This is what we call a message queue.

LPUSH
grammar:LPUSH key value [value ...]

Set one or more values ​​of the key, insert from the left

127.0.0.1:6379> LPUSH colors green red yellow 
(integer) 4

127.0.0.1:6379> LRANGE colors 0 2
1) "yellow"
2) "red"
3) "green"

RPUSH
grammar:RPUSH key value [value ...]

Set one or more values ​​of the key, insert from the right end

127.0.0.1:6379> RPUSH colors blue 
(integer) 4

127.0.0.1:6379> LRANGE colors 0 3
1) "yellow"
2) "red"
3) "green"
4) "blue"

LPOP
Syntax: LPOP key
delete and get the first element in the list, starting from the left

127.0.0.1:6379> LPOP colors
"yellow"
127.0.0.1:6379> LRANGE colors 0 3
1) "red"
2) "green"
3) "blue"

RPOP
Syntax: RPOP key
delete and get the first element in the list, starting from the right end

127.0.0.1:6379> RPOP colors
"blue"
127.0.0.1:6379> LRANGE colors 0 3
1) "red"
2) "green"

LPUSHX
Syntax: LPUSHX key value
Only when the list exists, add a value before the list

127.0.0.1:6379> LPUSHX name ydong
(integer) 0

127.0.0.1:6379> LPUSHX colors yellow
(integer) 3
127.0.0.1:6379> LRANGE colors 0 3
1) "yellow"
2) "red"
3) "green"

LRANGE
Syntax: LRANGE key start stop
remove the range of elements from the list

LINDEX
Syntax: LINDEX key index
Get elements by index

127.0.0.1:6379> LINDEX colors 0
"yellow"

LSET
Syntax: LSET key index value
Set the value of the index through the specified index

127.0.0.1:6379> LSET colors 0 blue
OK
127.0.0.1:6379> LRANGE colors 0 3
1) "blue"
2) "red"
3) "green"

SET

There is no sorted character set, and the members must be unique. The maximum number of members in the set is 232-1 (4294967295, each set can store more than 4 billion members). The collection of set is implemented by hash, so the performance is extremely high

SADD
grammar:SADD key member [member ...]

Add 1 or more members to the collection

127.0.0.1:6379> SADD name1 dasheng  zhubajie  tangseng bailongma
(integer) 4

SMEMBERS
Syntax: SMEMBERS key
Display all members in the collection

127.0.0.1:6379> SMEMBERS name1
1) "dasheng"
2) "bailongma"
3) "tangseng"
4) "zhubajie"

SPOP
Syntax: SPOP key [count]
randomly delete members and return one or more members, count can be set to delete several members

127.0.0.1:6379> SMEMBERS name1
1) "dasheng"
2) "bailongma"
3) "tangseng"
4) "zhubajie"
127.0.0.1:6379> SPOP name1 2
1) "dasheng"
2) "tangseng"

SREM
Syntax: SREM key member [member ...]
delete 1 or more members

127.0.0.1:6379> SMEMBERS name1
1) "dasheng"
2) "bailongma"
3) "tangseng"
4) "zhubajie"
127.0.0.1:6379> SREM name1 dasheng bailongma
(integer) 2
127.0.0.1:6379> SMEMBERS name1
1) "tangseng"
2) "zhubajie"

SRANDMEMBER

Syntax: SRANDMEMBER key [count]
get one or more members randomly

127.0.0.1:6379> SRANDMEMBER name1 1
1) "tangseng"
127.0.0.1:6379> SRANDMEMBER name1 1
1) "zhubajie"

SINTER
Syntax: SINTER key [key ...]
Take a collection of one or more keys

127.0.0.1:6379> SMEMBERS name1
1) "zhubajie"
2) "tangseng"
127.0.0.1:6379> SMEMBERS name2
1) "dasheng"
2) "shawujing"
3) "tangseng"
127.0.0.1:6379> SINTER name1 name2
1) "tangseng"

SUNION
Syntax: SUNION key [key ...]
Take a key or a collection of multiple keys

127.0.0.1:6379> SUNION name1 name2
1) "dasheng"
2) "shawujing"
3) "tangseng"
4) "zhubajie"

SDIFF
Syntax: SDIFF key [key ...]
the difference between the two keys, the first key removes the same members as the second key, and returns the result of the first key

127.0.0.1:6379> SDIFF name1 name2
1) "zhubajie"

SISMEMBER
Syntax: SISMEMBER key member
Determine whether the given value is in the key

127.0.0.1:6379> SISMEMBER name1 shawujing
(integer) 0
127.0.0.1:6379> SISMEMBER name1 zhubajie
(integer) 1

sorted_set

Ordered collection, no duplicate values, sorted according to the given score.

ZADD
Syntax: ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
Add one or more members to the sorted set. If it already exists, update its score. After the addition is completed, it will be automatically sorted according to the score

127.0.0.1:6379> ZADD score 60 xiaotian  80 xiaoming 70 xiaohong
(integer) 3

ZCARD
Syntax: ZCARD key
get the number of members in the key

127.0.0.1:6379> ZCARD score
(integer) 3

ZCOUNT
Grammar: How ZCOUNT key min max
many members are between the largest and smallest

127.0.0.1:6379> ZCOUNT score 70 90
(integer) 2

ZRANK
Syntax: ZRANK key member
return the index of a given member value

127.0.0.1:6379> ZRANK score xiaotian
(integer) 0

ZRANGE
Syntax: ZRANGE key start stop [WITHSCORES]
Return all members within the given index range

127.0.0.1:6379> ZRANGE score 0 2
1) "xiaotian"
2) "xiaohong"
3) "xiaoming"

hash

Equivalent to an associative array, storing data with key[field] value

HSET
Syntax: HSET key field value
Set the field and value of a key


127.0.0.1:6379> HSET class xiaoming 1
(integer) 1


HMSET
Syntax: HMSET key field value [field value ...]
Set the fields and values ​​of multiple keys

127.0.0.1:6379> HMSET class xiaohong 2 xiaotian 3 xiaoma 1
OK

HGET
Syntax: HGET key field
According to a given field, return value

127.0.0.1:6379> HGET class xiaoming
"1"

HMGET
Syntax: HMGET key field [field ...]
According to the given field, return all values

127.0.0.1:6379> HMGET class xiaoming xiaohong xiaotian
1) "1"
2) "2"
3) "3"

HKEYS

Syntax: HKEYS key
return all fields in the key

127.0.0.1:6379> HKEYS class
1) "xiaoming"
2) "xiaohong"
3) "xiaotian"
4) "xiaoma"

HVALS
Syntax: HVALS key
return all values ​​in the key

127.0.0.1:6379> HVALS class
1) "1"
2) "2"
3) "3"
4) "1"

HDEL
Syntax: HDEL key field [field ...]
delete one or more fields

127.0.0.1:6379> HDEL class xiaoming xiaohong
(integer) 2

HGETALL
Syntax: HGETALL key
Return all fields and values ​​in the key

127.0.0.1:6379> HGETALL class
1) "xiaotian"
2) "3"
3) "xiaoma"
4) "2"

pub/sub

Publish, subscribe. The news published by the publisher can be sent to specific subscribers through the channel. On the contrary, if the subscriber is interested in multiple channels, it can follow more publishers.

If the message published by the publisher is sent to a large number of subscribers, a 1-to-N effect can be achieved.

PSUBSCRIBE
Syntax: PSUBSCRIBE pattern [pattern ...]
listen to messages posted to channels in pattern mode.

127.0.0.1:6379> PSUBSCRIBE football
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "football"
3) (integer) 1

#监听之后,。会阻塞在这里,等发布者发布消息到对应的频道

PUBLISH
Syntax: PUBLISH channel message
publish a message to the specified channel

127.0.0.1:6379> PUBLISH football hello
(integer) 1

Subscribers will receive the message

127.0.0.1:6379> PSUBSCRIBE football
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "football"
3) (integer) 1
1) "pmessage"
2) "football"
3) "football"
4) "hello"

UNSUBSCRIBE
Syntax: UNSUBSCRIBE [channel [channel ...]]
unsubscribe

Redis configuration file

NETWORK

  • bind: Bind address, which is bound to 127.0.0.1 by default. If you want to bind to all addresses, write:bind 0.0.0.0
  • protected-mode: Whether to enable security authentication. When the user does not display the specified bind or password, this option will only be accessed locally. The user has set a password and bind address and can be opened. The default is yes
  • port 6379: Redis port
  • tcp-backlog 511: The length of the queue for tcp to complete the three-way handshake. In the case of high concurrency, it is generally necessary to increase this item. Alleviate the slow down of user connections. You can refer to the definition of tcp-backlog
  • unixsocket /tmp/redis.sock: Set the file path of the socket, it can be opened if it is only accessed by the local machine.
  • timeout 0: The client is automatically disconnected if it does not connect to the server within the specified idle time, 0 means no disconnection
  • tcp-keepalive 300: If the setting is not 0, the so_keepalive value of tcp is used. This detection has two advantages. It can detect the status of the peer device, and it can also detect whether the network device connection in the middle is alive. Linux internally set how long it takes to send an ack message to the Redis socket, and it takes twice as long to close the connection of the peer. This time has been tested and 300s is a reasonable value

GENERAL

  • daemonize no: Whether to run as a daemon
  • pidfile /var/run/redis_6379.pid: Process file
  • loglevel notice: Logging level
  • logfile /var/log/redis/redis.log: Log path
  • syslog-enabled no: Whether to use syslog to record logs, just choose one
  • databases 16: Redis as a database, how many libraries can be supported at most. 16 by default

Redis persistence

SNAPSHOTTING

The default persistent storage function of Redis is to periodically synchronize the data in the memory to the disk according to the command displayed by the client. The data is stored in /var/lib/redis/dump.db by default

The persistent storage method is defined in the configuration file:
save <seconds> <change>

  • save 900 1: If there is a key change in 900 seconds, take a snapshot
  • save 300 10: If there are 10 key changes in 300 seconds, take a snapshot
  • save 60 10000: There are 1w key changes in 60 seconds, just take a snapshot

Other related configuration

  • stop-writes-on-bgsave-error yes: If the dump file fails to write, whether to prohibit new operation requests from coming in
  • rdbcompression yes: Whether the snapshot is compressed when copying to the disk
  • rdbchecksum yes: Check the consistency of the data
  • dbfilename dump.rdb: Store the file name
  • dir /var/lib/redis: Dump storage path

In addition to the automatic storage method in the configuration file above, you can also explicitly use commands to store it in the interactive line. savewithbgsave

  • save: When using save, the redis client will be blocked until the RDB execution ends, and the new RDB file will replace the old file
  • bgsave: Redis forks the parent process and the child process, and all the persistent storage is completed by the child process. This blocking only occurs in the fork process, and this process will be very short.

The use of RDB persistent storage has the following characteristics:

  • Since RDB is a snapshot at a certain moment, it can be used as a full backup
  • RDB is a single file, which can be restored much faster than AOF, and RDB files can be sent to another host for disaster recovery backup
  • Since RDB is a snapshot, when the host fails, data will be lost at a certain moment, resulting in incomplete data

APPEND ONLY MODE

AOF is different from RDB. AOF appends each record to a file on the disk in seconds. When redis is shut down and restarted, it will automatically read the record into the memory.

Since the AOF file will be written larger and larger, redis provides the BGREWRITEAOF command, which reads the data in the existing memory and writes it in the temporary file by command, and replaces the existing AOF file after completion.

Related configuration:

  • appendonly no: Whether to enable AOF, default NO
  • appendfilename "appendonly.aof": The name of the stored file, the default path /var/lib/redis
  • appendfsync everysec: AOF synchronizes once per second, and AOF also provides two ways.
    • always:Every sentence once, as long as the sentence is written, it is immediately saved in the file
    • no: Redis does not automatically perform synchronization operations, but the kernel determines when to perform synchronization operations
  • no-appendfsync-on-rewrite no: Whether to mobilize fsync during AOF rewriting in the background. no means call
  • auto-aof-rewrite-percentage 100: Compare the new part with the original one, and rewrite it when it reaches 100%
  • auto-aof-rewrite-min-size 64mb:Compare the new part with the original one, the minimum difference is 64M
    • The above two differences must be met at the same time to enable automatic rewriting
  • aof-load-truncated yes: There is a problem with the sentence during the recovery process, whether to automatically clear it

LUA SCRIPTING

requirepass centos:safety certificate.
Need to use in interactive modeAUTH password

127.0.0.1:6379> GET name
(error) NOAUTH Authentication required.
127.0.0.1:6379> AUTH centos
OK
127.0.0.1:6379> GET name
(nil)

Or use the -a option in the command line

[root@localhost ~]# redis-cli -a centos

127.0.0.1:6379> keys *
1) "num"
2) "colors"
3) "students"
4) "number"

LIMITS

  • maxclients 10000: Maximum number of concurrent connections
  • maxmemory <bytes>: Set the maximum memory occupied by redis. When the occupied memory reaches the upper limit, the maxmemory-policypreviously set key will be eliminated according to it.
  • maxmemory-policy noeviction: Redis default strategy, no value is eliminated, redis provides the following elimination strategies
    • volatile-lru : Use the LRU algorithm to eliminate the keys that set the expiration time
    • allkeys-lru: LRU algorithm for all keys
    • volatile-random: Randomly eliminate keys with expiration time
    • allkeys-random: Random elimination
    • volatile-ttl:Compared with the key that sets the expiration time, the smaller the key

Guess you like

Origin blog.csdn.net/qq_44564366/article/details/108302236