[Redis] - Redis basic data structure and application scenarios

What is redis database

  • Redis is a memory-based database . All read and write operations on data are completed in memory , so the read and write speed is very fast . It is often used in scenarios such as caching, message queues, and distributed locks .
  • , Redis also supports  transactions, persistence, Lua scripts, multiple cluster solutions (master-slave replication mode, sentinel mode, slicer group mode), publish/subscribe mode, memory elimination mechanism, expired deletion mechanism
  • Redis is widely used, such as Twitter, Blizzard Entertainment, Github, Stack Overflow, Tencent, Alibaba, JD.com, Huawei, Sina Weibo, etc., and is also used by many small and medium-sized companies;

application

  • Record the number of likes, comments and clicks in Moments (hash)
  • Record the circle of friends talk list (sorting) to facilitate quick display of the circle of friends (list)
  • Record the title, abstract, author and cover of the article for list page display (hash)
  • Record the like user ID list (list) and comment ID list (list) in the circle of friends for display and deduplication counting (zset)
  • Cache hotspot data to reduce database pressure (hash)
  • If the Moments ID is an integer ID, you can use redis to assign the Moments ID (counter) (string)
  • Recording of friend relationships (set) is achieved through the intersection and difference operation of sets (set). In the game business, the results of each game are stored (list)

Redis storage structure

The overall internal storage structure of Redis is a large HashMap , which stores organizational data through key-value . Key conflicts are realized through linked lists. Each dictEntry is a key/value object, and the value is RedisObject .

All key values ​​are of string type.

Naming convention for key values:

[Recommendation] Redis key naming must be readable and manageable . Keys with unclear meanings and particularly long key names should not be used;

[ Mandatory ] It starts with an English letter . Only lowercase letters, numbers, English dots (.) and English half-width colons (:) can appear in the name ;

[ Mandatory ] Do not include special characters, such as underscores, spaces, newlines, single and double quotation marks, and other escape characters ;

 [ Mandatory ] Naming convention: Business module name: Business logic meaning: Others: value type

例如:user:basic.info:{userid}:string

What is the type of value data structure in Redis?

Redis provides a wealth of data types, and there are five common data types: String (string), Hash (hash), List (list), Set (set), and Zset (ordered set).

  • string is a safe binary string;
  • Double-ended queue (linked list) list: ordered (insertion order);
  • Hash table hash: does not pay attention to the order, the field is unique ;
  • Unordered set set: no attention is paid to the order, and the values ​​inside are all unique;
  • Ordered set zset: It pays attention to the order, and the values ​​inside are unique; uniqueness is determined based on member ; ordering is determined based on score;

String

String is a safe string.
What safe string?

Functions that use safe strings will never exceed the string buffer.

Common commands

//设置 key-value 类型的值
127.0.0.1:6379> set name sjp
OK
# 根据 key 获得对应的 value
127.0.0.1:6379> get name
"sjp"
# 判断某个 key 是否存在
127.0.0.1:6379> exists name
(integer) 1
# 返回 key 所储存的字符串值的长度
127.0.0.1:6379> strlen name
(integer) 3
# 删除某个 key 对应的值
127.0.0.1:6379> del name
(integer) 1

#批量设置 key-value类型的值
127.0.0.1:6379> mset name:1001 sjp name:1002 lisi
OK
# 批量获取多个 key 对应的 value
127.0.0.1:6379> mget name:1001 name:1002
1) "sjp"
2) "lisi"

Application of string 

Directly cache the JSON of the entire object

Command example: 

SET user:1 '{"name":"sjp", "age":18}'。

The key is separated into user:ID: attributes, MSET is used to store it, and MGET is used to obtain the value of each attribute. Command example: 

MSET user:1:name xiaolin user:1:age 18 user:2:name xiaomei user:2:age 20

Counter (can be used when the content of the string is an integer)

# 设置 key-value 类型的值
127.0.0.1:6379> set count 1
OK
# 将 key 中储存的数字值增一
127.0.0.1:6379> incr count
(integer) 2
# 将key中存储的数字值加 10
127.0.0.1:6379> incrby count 100
(integer) 102
# 将 key 中储存的数字值减一
127.0.0.1:6379> decr count
(integer) 101
# 将key中存储的数字值键 10
127.0.0.1:6379> decrby count 10
(integer) 91

List

List The list is implemented as a doubly linked list . The time complexity of operations on the head and tail of the list (deletion and addition) is O(1); the time complexity of finding the middle element is O(n);

The basis for whether the data in the list is compressed:

1. The element length is less than 48 and is not compressed;

2. The length difference between the elements before and after compression does not exceed 8, and there is no compression ;

Basic commands:

# 从队列的左侧入队一个或多个元素
LPUSH key value [value ...]
# 从队列的左侧弹出一个元素
LPOP key
# 从队列的右侧入队一个或多个元素
RPUSH key value [value ...]
# 从队列的右侧弹出一个元素
RPOP key 
# 返回从队列的 start 和 end 之间的元素 0, 1 2 负索引
LRANGE key start end
# 从存于 key 的列表里移除前 count 次出现的值为 value 的
元素
# list 没有去重功能   hash set zset
LREM key count value
# 它是 RPOP 的阻塞版本,因为这个命令会在给定list无法弹出
任何元素的时候阻塞连接
BRPOP key timeout  # 超时时间 + 延时队列

Application scenarios

stack

LPUSH + LPOP
# 或者
RPUSH + RPOP

queue

LPUSH + RPOP
# 或者
RPUSH + LPOP

message queue

When a message queue accesses messages, it must meet three requirements: message order preservation, duplicate message processing  , and  message reliability .

  • The producer uses lpush to add messages to the list collection.
  • Consumers use brpop to get messages from the list collection 

 The BRPOP command is also called blocking reading . When the client does not read the queue data, it automatically blocks until new data is written to the queue, and then starts reading new data.

2. How does the message queue handle duplicate messages?

For consumers to realize the judgment of duplicate messages, they need two requirements:

  • Each message has a global ID.
  • The consumer should record the ID of the message that has been processed . After receiving a message, the consumer program can compare the received message ID with the recorded processed message ID to determine whether the currently received message has been processed. If it has been processed, then the consumer program will no longer process it.

 List does not generate an ID number for each message, so we need to generate a globally unique ID for each message ourselves. After generation, when we use the LPUSH command to insert the message into the List, we need to include this globally unique ID in the message.

3. How does the message queue ensure the reliability of messages?

When the consumer program reads a message from the List , the List will no longer retain the message. Therefore, if the consumer program fails or crashes while processing the message, the message will not be processed. Then, after the consumer program is started again, it will not be able to read the message from the List again.

The List type provides  BRPOPLPUSH a command. The function of this command  is to allow the consumer program to read messages from a List. At the same time, Redis will insert the message into another List (which can be called a backup List) for storage .

Hash

Hash is a collection of key-value pairs, where value is in the form:  value=[{field1,value1},...{fieldN,valueN}]. Hash is particularly suitable for storing objects.

internal implementation

The underlying data structure of the Hash type is implemented by a compressed list or hash table :

If  the number of hash type elements is less  512 than (default value,  hash-max-ziplist-entries configurable) and all values ​​are less than  64 bytes (default value,  hash-max-ziplist-value configurable), Redis will use a compressed list as the underlying data structure of the Hash type.

Basic commands

# 获取 key 对应 hash 中的 field 对应的值
HGET key field
# 设置 key 对应 hash 中的 field 对应的值
HSET key field value
# 设置多个hash键值对
HMSET key field1 value1 field2 value2 ... fieldn
valuen
# 获取多个field的值
HMGET key field1 field2 ... fieldn
# 给 key 对应 hash 中的 field 对应的值加一个整数值
HINCRBY key field increment
# 获取 key 对应的 hash 有多少个键值对
HLEN key
# 删除 key 对应的 hash 的键值对,该键为field
HDEL key field

application

Generally, objects are stored in String + Json. Some frequently changing attributes in the object can be extracted and stored in Hash type .

shopping cart

set

gather

The Set type is an unordered and unique set of key values . Its storage order will not be stored in the order of insertion.

A collection can store at most  elements. The concept is basically similar to the set of numbers in mathematics. It can be intersection, union, difference set , etc. . 2^32-1

storage structure

If the elements are all integers and the number of nodes is less than or equal to 512 ( set-max-intsetentries), use integer array storage ;

If one of the elements is not an integer or the number of nodes is greater than 512 , dictionary storage is used

Basic commands 

# 添加一个或多个指定的member元素到集合的 key中
SADD key member [member ...]
# 计算集合元素个数
SCARD key
# SMEMBERS key
SMEMBERS key
# 返回成员 member 是否是存储的集合 key的成员
SISMEMBER key member
# 随机返回key集合中的一个或者多个元素,不删除这些元素
SRANDMEMBER key [count]
# 从存储在key的集合中移除并返回一个或多个随机元素
SPOP key [count]
# 返回一个集合与给定集合的差集的元素

SDIFF key [key ...]
# 返回指定所有的集合的成员的交集
SINTER key [key ...]
# 返回给定的多个集合的并集中的所有成员
SUNION key [key ...]

 The difference between set and list

List can store repeated elements, and set can only store non-repeating elements.

A list stores elements in the order in which they are inserted, while a set stores elements in an unordered manner.

application

like

Set can ensure that a user can only click one like. For example, the key is the article ID and the value is the user ID.

uid:1 , uid:2,uid:3  three users respectively liked article:1.

#uid:1 uid:2 uid:3 用户对文章 article:1 点赞 
127.0.0.1:6379> sadd article:1 uid:1
(integer) 1
127.0.0.1:6379> sadd article:1 uid:2
(integer) 1
127.0.0.1:6379> sadd article:1 uid:3
(integer) 1

#获取 article:1 文章所有点赞用户 :
127.0.0.1:6379> smembers article:1
1) "uid:3"
2) "uid:2"
3) "uid:1"

#获取 article:1 文章的点赞用户数量:
127.0.0.1:6379> scard article:1
(integer) 3

Recommend friends

The Set type supports intersection operations, so it can be used to calculate jointly followed friends, public accounts, etc.

The key can be the user ID, and the value is the friend.

#插入A的好友
127.0.0.1:6379> sadd follow:A sjp king lisi
(integer) 3
#插入B的好友
127.0.0.1:6379> sadd follow:B chen lisi sjp
(integer) 3
#A不同于B的好友
127.0.0.1:6379> sdiff follow:A follow:B
1) "king"
#A与B的共同好友
127.0.0.1:6379> sinter follow:A follow:B
1) "sjp"
2) "lisi"

lottery

The key is the name of the lottery and the value is the name of the employee. Put all the employee names into the lottery box.

#添加抽奖人员
127.0.0.1:6379> sadd lucky A B C D E
(integer) 5

#允许重复抽奖
127.0.0.1:6379> srandmember lucky 1
1) "C"
127.0.0.1:6379> srandmember lucky 2
1) "E"
2) "D"
127.0.0.1:6379> srandmember lucky 3
1) "B"
2) "C"
3) "A"


#不重复抽奖
127.0.0.1:6379> spop lucky 1
1) "A"
127.0.0.1:6379> spop lucky 2
1) "D"
2) "E"

check

Compared with the Set type, the Zset type (ordered set type) has an additional sorting attribute score (score) . For ordered sets 

An ordered set retains the property that a set cannot have duplicate members ( scores can be repeated ), but the difference is that the elements in an ordered set can be sorted. zset sorts based on score.

Basic commands:

# 添加到键为key有序集合(sorted set)里面
ZADD key [NX|XX] [CH] [INCR] score member [score
member ...]
# 从键为key有序集合中删除 member 的键值对
ZREM key member [member ...]
# 返回有序集key中,成员member的score值
ZSCORE key member
# 为有序集key的成员member的score值加上增量increment
ZINCRBY key increment member
# 返回key的有序集元素个数
ZCARD key
# 返回有序集key中成员member的排名
ZRANK key member
# 返回存储在有序集合key中的指定范围的元素   order by id
limit 1,100
ZRANGE key start stop [WITHSCORES]
# 返回有序集key中,指定区间内的成员(逆序)
ZREVRANGE key start stop [WITHSCORES]

 Application scenarios

A typical usage scenario for ordered collections is the ranking list. For example, student performance rankings , game points rankings , video playback rankings, product sales rankings in e-commerce systems , etc.

Guess you like

Origin blog.csdn.net/sjp11/article/details/132094354