First published in: Detailed Explanation of 5 Basic Data Structures in Redis - JavaGuide
Related articles: Summary of common interview questions in Redis (Part 1) .
The five basic data structures of Redis (String, List, Hash, Set, Sorted Set) are often asked in interviews. Let's review and review in this article.
There are also several special data structures (HyperLogLogs, Bitmap, Geospatial, Stream) that are also very important. We will talk about it next time!
Below is the text.
You can find a very detailed introduction to the Redis data structure on the Redis official website:
With the release of new versions of Redis in the future, new data structures may appear. You can always get the most reliable information by checking the corresponding introduction on the Redis official website.
String (string)
introduce
String is the simplest and most commonly used data structure in Redis.
String is a binary-safe data structure that can be used to store any type of data such as strings, integers, floating-point numbers, images (base64 encoding or decoding of images or paths to images), and serialized objects.
Although Redis is written in C language, Redis does not use C's string representation, but builds a simple dynamic string (Simple Dynamic String, SDS ). Compared with the native string of C, Redis's SDS can not only save text data but also binary data, and the complexity of obtaining string length is O(1) (C string is O(N)), in addition to , Redis's SDS API is safe and will not cause buffer overflows.
Common commands
Order | introduce |
---|---|
SET key value | Set the value of the specified key |
SETNX key value | Set the value of key only if key does not exist |
GET key | Get the value of the specified key |
MSET key1 value1 key2 value2 … | Set the value of one or more specified keys |
MGET key1 key2 ... | Get the value of one or more specified keys |
STRLEN key | Returns the length of the string value stored at key |
INCR key | Increment the numeric value stored in key by one |
DECR key | Decrements the numeric value stored in key by one |
EXISTS key | Determine whether the specified key exists |
DEL key (generic) | delete the specified key |
EXPIRE key seconds (generic) | Set the expiration time for the specified key |
For more Redis String commands and detailed usage guides, please check the corresponding introduction on the Redis official website: https://redis.io/commands/?group=string .
Basic operation :
> SET key value
OK
> GET key
"value"
> EXISTS key
(integer) 1
> STRLEN key
(integer) 5
> DEL key
(integer) 1
> GET key
(nil)
Batch settings :
> MSET key1 value1 key2 value2
OK
> MGET key1 key2 # 批量获取多个 key 对应的 value
1) "value1"
2) "value2"
Counter (can be used when the content of the string is an integer):
> SET number 1
OK
> INCR number # 将 key 中储存的数字值增一
(integer) 2
> GET number
"2"
> DECR number # 将 key 中储存的数字值减一
(integer) 1
> GET number
"1"
Set expiration time (default is never expire) :
> EXPIRE key 60
(integer) 1
> SETNX key 60 value # 设置值并设置过期时间
OK
> TTL key
(integer) 56
Application scenarios
Scenarios that need to store regular data
- For example: cache session, token, image address, serialized object (more memory-saving compared to Hash storage).
- Related commands:
SET
,GET
.
Scenarios that need to be counted
- For example: the number of user requests per unit time (simple current limit can be used), the number of page visits per unit time.
- Related commands:
SET
,GET
,INCR
,DECR
.
Distributed lock
Using the SETNX key value
command can implement a simplest distributed lock (there are some defects, it is generally not recommended to implement distributed locks in this way).
List
introduce
The List in Redis is actually the implementation of the linked list data structure. I introduced the data structure of linked list in detail in the article Linear Data Structure: Array, Linked List, Stack, Queue , and I will not introduce it here.
Many high-level programming languages have built-in implementations of linked lists, such as LinkedList
in Java, but C language does not implement linked lists, so Redis implements its own linked list data structure. The implementation of Redis List is a doubly linked list , which can support reverse search and traversal, which is more convenient to operate, but brings some additional memory overhead.
Common commands
Order | introduce |
---|---|
RPUSH key value1 value2 ... | Adds one or more elements to the end (right) of the specified list |
LPUSH key value1 value2 ... | Adds one or more elements to the head (left) of the specified list |
LSET key index value | Sets the value at the specified list index index position to value |
LPOP key | Remove and get the first element (leftmost) of the specified list |
RPOP key | Remove and get the last element (rightmost) of the specified list |
LLEN key | Get the number of list elements |
LRANGE key start end | Get the elements between the start and end of the list |
For more Redis List commands and detailed usage guides, please check the corresponding introduction on the Redis official website: https://redis.io/commands/?group=list .
Implement the queue via RPUSH/LPOP
or LPUSH/RPOP
:
> RPUSH myList value1
(integer) 1
> RPUSH myList value2 value3
(integer) 3
> LPOP myList
"value1"
> LRANGE myList 0 1
1) "value2"
2) "value3"
> LRANGE myList 0 -1
1) "value2"
2) "value3"
Implement the stack via RPUSH/RPOP
or LPUSH/LPOP
:
> RPUSH myList2 value1 value2 value3
(integer) 3
> RPOP myList2 # 将 list的头部(最右边)元素取出
"value3"
I drew a diagram specially for your understanding RPUSH
, LPOP
, lpush
, RPOP
command
View the list elements corresponding to the subscript range through LRANGE
:
> RPUSH myList value1 value2 value3
(integer) 3
> LRANGE myList 0 1
1) "value1"
2) "value2"
> LRANGE myList 0 -1
1) "value1"
2) "value2"
3) "value3"
Through the LRANGE
command, you can implement paging query based on List, and the performance is very high!
View the length of the linked list through LLEN
:
> LLEN myList
(integer) 3
Application scenarios
Information flow display
- For example: latest articles, latest developments.
- Related commands:
LPUSH
,LRANGE
.
message queue
The Redis List data structure can be used as a message queue, but the function is too simple and there are many defects, so it is not recommended to do so.
Relatively speaking, a newly added data structure in Redis 5.0 Stream
is more suitable for message queue, but the function is still very simple. Compared with professional message queues, there are still many shortcomings, such as message loss and accumulation problems that are difficult to solve.
Hash
introduce
Hash in Redis is a field-value (key-value pair) mapping table of String type, which is especially suitable for storing objects. During subsequent operations, you can directly modify the values of some fields in this object.
Hash is similar to the one before HashMap
, and the internal implementation is similar (array + linked list). However, Redis's Hash is more optimized.
Common commands
Order | introduce |
---|---|
HSET key field value | Sets the value of the specified field in the specified hash table |
HSETNX key field value | Set the value of the specified field only if the specified field does not exist |
HMSET key field1 value1 field2 value2 ... | Simultaneously set one or more field-value (field-value) pairs into the specified hash table |
HGET key field | Get the value of the specified field in the specified hash table |
HMGET key field1 field2 ... | Get the value of one or more specified fields in the specified hash table |
HGETALL key | Get all key-value pairs in the specified hash table |
HEXISTS key field | Check whether the specified field in the specified hash table exists |
HDEL key field1 field2 ... | Delete one or more hash table fields |
HLEN key | Get the number of fields in the specified hash table |
For more Redis Hash commands and detailed usage guides, please check the corresponding introduction on the Redis official website: https://redis.io/commands/?group=hash .
Mock Object Datastore :
> HMSET userInfoKey name "guide" description "dev" age "24"
OK
> HEXISTS userInfoKey name # 查看 key 对应的 value中指定的字段是否存在。
(integer) 1
> HGET userInfoKey name # 获取存储在哈希表中指定字段的值。
"guide"
> HGET userInfoKey age
"24"
> HGETALL userInfoKey # 获取在哈希表中指定 key 的所有字段和值
1) "name"
2) "guide"
3) "description"
4) "dev"
5) "age"
6) "24"
> HSET userInfoKey name "GuideGeGe"
> HGET userInfoKey name
"GuideGeGe"
Application scenarios
Object Data Storage Scenario
- For example: user information, product information, article information, shopping cart information.
- Related commands:
HSET
(set the value of a single field),HMSET
(set the value of multiple fields),HMGET
HGET
(get the value of a single field)HMGET
(get values for multiple fields).
Set
introduce
The Set type in Redis is an unordered collection. The elements in the collection are not sequential but unique, which is somewhat similar to HashSet
in Java. Set is a good choice when you need to store a list of data and don't want duplicate data, and Set provides an important interface for judging whether an element is in a Set collection, which List can't provide.
You can easily implement the operations of intersection, union, and difference based on Set. For example, you can store all followers of a user in a set, and all followers of a user in a set. In this way, Set can easily implement functions such as common attention, common fans, and common preferences. This process is also the process of seeking intersection.
Common commands
Order | introduce |
---|---|
SADD key member1 member2... | Adds one or more elements to the specified collection |
SMEMBERS key | Get all elements in the specified collection |
SCARD key | Get the number of elements in the specified collection |
SISMEMBER key member | Determines whether the specified element is in the specified collection |
SINTER key1 key2 ... | Get the intersection of all sets given |
SINTERSTORE destination key1 key2 ... | Stores the intersection of all sets given in destination |
SUNION key1 key2 ... | Get the union of all the sets given |
SUNIONSTORE destination key1 key2 ... | Stores the union of all sets given in destination |
SDIFF key1 key2 ... | Get the difference of all sets given |
SDIFFSTORE destination key1 key2 ... | store the difference of all sets given in destination |
SPOP key count | Randomly remove and get one or more elements from the specified collection |
SRANDMEMBER key count | Randomly get the specified number of elements in the specified collection |
For more Redis Set commands and detailed usage guides, please check the corresponding introduction on the Redis official website: https://redis.io/commands/?group=set .
Basic operation :
> SADD mySet value1 value2
(integer) 2
> SADD mySet value1 # 不允许有重复元素,因此添加失败
(integer) 0
> SMEMBERS mySet
1) "value1"
2) "value2"
> SCARD mySet
(integer) 2
> SISMEMBER mySet value1
(integer) 1
> SADD mySet2 value2 value3
(integer) 2
-
mySet
:value1
,value2
. -
mySet2
:value2
,value3
.
Seek intersection :
> SINTERSTORE mySet3 mySet mySet2
(integer) 1
> SMEMBERS mySet3
1) "value2"
Find the union :
> SUNION mySet mySet2
1) "value3"
2) "value2"
3) "value1"
Find the difference :
> SDIFF mySet mySet2 # 差集是由所有属于 mySet 但不属于 A 的元素组成的集合
1) "value1"
Application scenarios
Scenarios where the data to be stored cannot be repeated
- For example: website UV statistics (scenarios with huge amount of data are more suitable for scenes such as
HyperLogLog
), article likes, dynamic likes and other scenarios. - Related commands:
SCARD
(get the number of sets).
Scenarios that need to obtain the intersection, union and difference of multiple data sources
- For example: common friends (intersection), common fans (intersection), common concern (intersection), friend recommendation (difference), music recommendation (difference), subscription account recommendation (difference + intersection) and other scenarios.
- 相关命令:
SINTER
(交集)、SINTERSTORE
(交集)、SUNION
(并集)、SUNIONSTORE
(并集)、SDIFF
(intersection),SDIFFSTORE
(intersection).
Scenarios that need to randomly get elements from the data source
- For example: lottery system, random.
- Related commands:
SPOP
(randomly obtains the elements in the set and removes, suitable for scenarios where repeated winning is not allowed),SRANDMEMBER
(randomly obtains the elements in the set, suitable for allowing repeated winning Scenes).
Sorted Set
introduce
Sorted Set is similar to Set, but compared with Set, Sorted Set adds a weight parameter score
, so that the elements in the set can be ordered according to score
, and can also be ordered score
to get a list of elements. Kind of like a combination of HashMap
and TreeSet
in Java.
Common commands
Order | introduce |
---|---|
ZADD key score1 member1 score2 member2 ... | Adds one or more elements to the specified sorted set |
ZCARD KEY | Get the number of elements in the specified sorted set |
ZSCORE key member | Get the score value of the specified element in the specified sorted set |
ZINTERSTORE destination numkeys key1 key2 ... | Store the intersection of all the given ordered sets in destination, perform SUM aggregation operation on the score values corresponding to the same elements, numkeys is the number of sets |
ZUNIONSTORE destination numkeys key1 key2 ... | Union set, others are similar to ZINTERSTORE |
ZDIFF destination numkeys key1 key2 ... | Find the difference set, others are similar to ZINTERSTORE |
ZRANGE key start end | Get the elements between start and end of the specified sorted set (score from low to high) |
ZREVRANGE key start end | Get the elements between start and end of the specified sorted set (score from high to bottom) |
ZREVRANK key member | Get the rank of the specified element in the specified sorted set (score is sorted from large to small) |
For more Redis Sorted Set commands and detailed usage guides, please check the corresponding introduction on the Redis official website: https://redis.io/commands/?group=sorted-set .
Basic operation :
> ZADD myZset 2.0 value1 1.0 value2
(integer) 2
> ZCARD myZset
2
> ZSCORE myZset value1
2.0
> ZRANGE myZset 0 1
1) "value2"
2) "value1"
> ZREVRANGE myZset 0 1
1) "value1"
2) "value2"
> ZADD myZset2 4.0 value2 3.0 value3
(integer) 2
-
myZset
:value1
(2.0),value2
(1.0) . -
myZset2
:value2
(4.0),value3
(3.0).
Get the rank of the specified element :
> ZREVRANK myZset value1
0
> ZREVRANK myZset value2
1
Seek intersection :
> ZINTERSTORE myZset3 2 myZset myZset2
1
> ZRANGE myZset3 0 1 WITHSCORES
value2
5
Find the union :
> ZUNIONSTORE myZset4 2 myZset myZset2
3
> ZRANGE myZset4 0 2 WITHSCORES
value1
2
value3
3
value2
5
Find the difference :
> ZDIFF 2 myZset myZset2 WITHSCORES
value1
2
Application scenarios
Scenarios where elements in the data source need to be randomly obtained and sorted according to a certain weight
- For example: various rankings, such as the ranking of gifts sent in the live broadcast room, the ranking of WeChat steps in the circle of friends, the ranking of rankings in the glory of the king, the ranking of hot topics, etc.
- Related commands:
ZRANGE
(sort from smallest to largest),ZREVRANGE
(sort from largest to smallest),ZREVRANK
(specify element rank).
The "Technical Interview Questions " of "Java Interview Guide " has an article detailing how to use Sorted Set to design and make a leaderboard.
Scenarios where the data to be stored has priority or importance, such as a priority task queue.
- Example: Priority task queue.
- Related commands:
ZRANGE
(sort from small to big),ZREVRANGE
(sort from big to small),ZREVRANK
(specify element rank).
refer to
- Redis Data Structures: https://redis.com/redis-enterprise/data-structures/ .
- Redis Commands: https://redis.io/commands/ .
- Redis Data types tutorial: https://redis.io/docs/manual/data-types/data-types-tutorial/ .
- Does Redis use Hash or String to store object information: https://segmentfault.com/a/1190000040032006
postscript
Focus on Java original dry goods sharing, junior open source JavaGuide ("Java Learning + Interview Guide" covers the core knowledge that most Java programmers need to master. Prepare for Java interviews, JavaGuide is the first choice!), currently has 120k+ Stars.
Originality is not easy, welcome to like and share, welcome to follow my account in Sifu , I will continue to share original dry goods! Come on, go!
If this article is helpful to you, please like and share, it is very important for me to continue to share & create high-quality articles. Thanks 🙏🏻
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。