# Redis summary of common knowledge (two)

Redis common knowledge summary (two)

Reids single thread and high performance

Single thread

  • Single thread means that the network request module uses one thread (so there is no need to consider concurrency safety), that is, one thread handles all network requests, and other modules still use multiple threads.

high performance

  • The data is in the memory, and all operations are memory-level operations. Single-threaded avoids the problem of resource consumption by multi-thread switching.

Redis single-threaded processing concurrent clients

  • Redis IO multiplexing: Redis uses epoll to implement IO multiplexing, putting connection information and events in the queue, and then putting them in the file event dispatcher in turn. The event dispatcher distributes the events to the event handler.

Redis persistence

RDB(Redis DataBase)

  • At different points in time, snapshots of the data stored in redis are generated and stored on disks and other media.

AOF(Append Only File)

  • AOF is a different point of view to achieve persistence, that is, to record all the write instructions executed by redis. When redis is restarted next time, as long as these write instructions are executed again from front to back, the data can be realized. back to normal.

  • RDB and AOF can be used at the same time. In this case, if Redis is restarted, AOF will be preferred for data recovery, because AOF 's recovery data has a high integrity.


Common data structure

String

String common operations: key value

It is used more frequently. In redis, the key is of type String, and the value is of type String.

set key value // 存入字符串键值对
get key 		// 获得key存储的value
del key			// 删除key存储的值
mset test1 one test2 two // 存储多个键值
mget test1 test2	// 查找多个key的值
setnx test zhangsan // 只有key 不存在时候插入
expire test 5	// 设置test键5秒过期
Atomic addition and subtraction
incr count	// 键count存的值+1
decr count  // 键count存的值-1
incrby a 7	// 给a存的值加上7
decrbt a 7 	// 给a存的值-7
String application scenarios
  • Single value cache
set test zhangsan
get test
  • Object cache
set people:1:id 10001
  • Distributed lock
setnx count:1000 true // 1代表获取锁成功
setnx count:1000 true // 0代表获取锁失败

del count:1000	// 释放锁 返回1
set product:1000 true ex 10 nx //防止死锁
  • counter
incr article:count:{
    
    test}	// test这篇文章的阅读量+1
get article:count:{
    
    test}
  • Session sharing

  • Distributed system global serial number


Hash

Common Hash operations: key field value

Insert picture description here

hset one test2 "test2"  //存储一个哈希表key的键值
hget one test2			// 获得哈希表中某个键的值
hmset one one "2" two "3" // 存储多个key value 在hash one 中

HSETNX key field value  //存储一个不存在的哈希表key的键值
HMGET key field [field ...] //批量获取哈希表key中多个field键值
HDEL key field [field ...]  //删除哈希表key中的field键值
HLEN key //返回哈希表key中field的数量
HGETALL key //返回哈希表key中所有的键值
HINCRBY key field increment //为哈希表key中field键的值加上增量
Hash application scenarios
  • Object storage
hmset user 1001 "张三" 1002 "李四"
Advantages and disadvantages of Hash structure
  • Advantages: consumes less memory and CPU, and saves space compared to string storage
  • Disadvantages: The expiration function cannot be used on the field, but only on the key. Redis cluster architecture is not suitable for large-scale use.

List

List common operations

Insert picture description here

lpush one two  //将一个或多个值value插入到key列表的表头(最左边)
rpush key value //将一个或多个值value插入到key列表的表尾(最右边)

LPOP key //移除并返回key列表的头元素
RPOP key //移除并返回key列表的尾元素
LRANGE key start stop //返回列表key中指定区间内的元素,区间以偏移量start和stop指定
BLPOP key [key ...] timeout //从key列表表头弹出一个元素,若列表中没有元素,阻塞等待timeout秒,如果timeout=0,一直阻塞等待
BRPOP key [key ...] timeout //从key列表表尾弹出一个元素,若列表中没有元素,阻塞等待timeout秒,如果timeout=0,一直阻塞等待
List application scenarios
  • Common data structure
  1. Stack (stack) = LPUSH + RPOP ( FILO )
  2. Queue (queue) = LPUSH +LRPOP ( FIFO )
  3. Blocking MQ (blocking queue) = LPUSH + BRPOP

Set

Set common operations

Insert picture description here

sadd two one two three // 添加key 为two value 
SREM key member [member ...] //从集合key中删除元素
SMEMBERS key //获取集合key中所有元素
SCARD key //获取集合key的元素个数
SISMEMBER key member //判断member元素是否存在于集合key中
SRANDMEMBER key [count] //从集合key中选出count个元素,元素不从key中删除
SPOP key [count] //从集合key中选出count个元素,元素从key中删除
  • Arithmetic operation
SINTER key [key ...] //交集运算
SINTERSTORE destination key [key ..] //将交集结果存入新集合destination中
SUNION key [key ..] //并集运算
SUNIONSTORE destination key [key ...] //将并集结果存入新集合destination中
SDIFF key [key ...] //差集运算
SDIFFSTORE destination key [key ...] //将差集结果存入新集合destination中

ZSet

Common operations of ZSet

Insert picture description here

ZADD myzset 1 "one" //往有序集合key中加入带分值元素
ZREM key member [member …] //从有序集合key中删除元素
ZSCORE key member  //返回有序集合key中元素member的分值
ZINCRBY key increment member //为有序集合key中元素member的分值加上increment 
ZCARD key //返回有序集合key中元素个数
ZRANGE key start stop [WITHSCORES] //正序获取有序集合key从start下标到stop下标的元素
ZREVRANGE key start stop [WITHSCORES]//倒序获取有序集合key从start下标到stop下标的元素
Zset application
1. 点击新闻:
ZINCRBY hotNews:20201221 1 完善低龄未成年人犯罪规定
2. 展示当日排行前十:
ZREVRANGE hotNews:20201221 0 9 WITHSCORES
3. 七日搜索榜单计算:
ZUNIONSTORE hotNews:20201215-20201221  7 
hotNews:20201215 hotNews:20201216... hotNews:20201221
4. 展示七日排行前十:
ZREVRANGE hotNews:20201215-20201221 0 9 WITHSCORES

Cache penetration, cache avalanche

Cache penetration
  • The general cache system caches queries according to the key. If the corresponding value does not exist, it should go to the back-end system to find it (such as DB). Some malicious requests will deliberately query non-existent keys, and the amount of requests is large, which will cause a lot of pressure on the back-end system. This is called cache penetration.
  • The query result is also cached when the query result is empty. The cache time is set to be shorter, or the data corresponding to the key is inserted and the cache is cleared.
  • Filter keys that must not exist. You can put all possible keys in a big Bitmap, and filter by the bitmap when querying.
Cache avalanche
  • When the cache server restarts or a large number of caches fail in a certain period of time, when it fails, it will put a lot of pressure on the back-end system. Cause the system to crash.
  • After the cache is invalid, the number of threads that read the database and write the cache is controlled by locking or queueing. For example, only one thread is allowed to query data and write cache for a certain key, and other threads wait.
  • As a second-level cache, A1 is the original cache, A2 is the copy cache, when A1 fails, you can access A2, the cache expiration time of A1 is set to short-term, and A2 is set to long-term
  • Set different expiration times for different keys to make the time points of cache invalidation as even as possible.

Guess you like

Origin blog.csdn.net/qq_37248504/article/details/111937612