Redis core data structure & Redis 6 new features in detail

Table of contents

1.list

2.hash 

 3.Set

4. Set 

 5.GeoHash

 6. Interview FAQs

6.1 Client Side Cache 

6.2 ACL permissions 


1.list

Redis is a kV structure in redisDB, which is stored in the dictionary dict. The dict contains two hashTable structures, which exist in the form of an array. The list is an ordered data structure, and the double-ended linked list quicklist and ziplist are used as the underlying implementation.

[root@localhost ~]# cd /usr/local/redis-6.2.7
[root@localhost redis-6.2.7]# src/redis-server redis.conf
[root@localhost redis-6.2.7]# src/redis-cli
127.0.0.1:6379> lpush a-list a b c
(integer) 3
127.0.0.1:6379> rpush a-list e f g
(integer) 6
127.0.0.1:6379> rpush a-list e 1100 f g
(integer) 10
127.0.0.1:6379> type a-list
list
127.0.0.1:6379> object encoding a-list
"quicklist"

 The data structure of the list is as follows:

2.hash 

 The hash is stored in a ziplist when the field is relatively small, and in a hashtable when the field is relatively large. The following figure shows the scene where the object encoding key code is changed from a ziplist to a hashtable.

127.0.0.1:6379> hset a-hash name caiji age 35 f1 v1 f2 v2 f3 v3
(integer) 5
127.0.0.1:6379> hgetall a-hash
 1) "name"
 2) "caiji"
 3) "age"
 4) "35"
 5) "f1"
 6) "v1"
 7) "f2"
 8) "v2"
 9) "f3"
10) "v3"
127.0.0.1:6379> type a-hash
hash
127.0.0.1:6379> object encoding a-hash
"ziplist"
127.0.0.1:6379> hset a-hash f4 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
(integer) 1
127.0.0.1:6379> hgetall a-hash
 1) "f1"
 2) "v1"
 3) "name"
 4) "caiji"
 5) "f2"
 6) "v2"
 7) "age"
 8) "35"
 9) "f3"
10) "v3"
11) "f4"
12) "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
127.0.0.1:6379> type a-hash
hash
127.0.0.1:6379> object encoding a-hash
"hashtable"

 The data structure of hash is as follows, you can configure the storage range of ziplist and hashtable by setting the redis.conf configuration file

Interview questions: string and hash selection and advantages and disadvantages

 

 3.Set

Set is an unordered and automatically deduplicated data type. It is ordered when it is stored in integers. The bottom layer uses the intset data structure. When it is stored in strings, it is unordered and stored in hashtable.

127.0.0.1:6379> sadd a-set 1 2 3 5 10 9 4 4 4
(integer) 7
127.0.0.1:6379> smembers a-set
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "9"
7) "10"
127.0.0.1:6379> type a-set
set
127.0.0.1:6379> object encoding a-set
"intset"
127.0.0.1:6379> sadd a-set a
(integer) 1
127.0.0.1:6379> smembers a-set
1) "a"
2) "3"
3) "10"
4) "9"
5) "4"
6) "2"
7) "5"
8) "1"
127.0.0.1:6379> type a-set
set
127.0.0.1:6379> object encoding a-set
"hashtable"

The data structure of intset is as follows, with three elements.

 

4. Set 

The principle of skiplist uses binary search, and the index method forms an index tree to search for data, and the index number of the Kth layer = n/2^k.

127.0.0.1:6379> zadd a-zset 100 a 200 b 150 c
(integer) 3
127.0.0.1:6379> zrange a-zset 0 -1 withscores
1) "a"
2) "100"
3) "c"
4) "150"
5) "b"
6) "200"
127.0.0.1:6379> type a-zset
zset
127.0.0.1:6379> object encoding a-zset
"ziplist"

 5.GeoHash

GeoHash is an algorithm plus zset implementation

help @geo 

127.0.0.1:6379> help @geo
127.0.0.1:6379> geoadd locations 116.3209103486328 39.92916527606944 guozhuang
(integer) 1
127.0.0.1:6379> geoadd locations 116.391621 39.946583 beijignzhan 116.401969 39.959857 ditangongyuan
(integer) 2
127.0.0.1:6379> geodist locations guozhuang beijignzhan km
"6.3337"
127.0.0.1:6379> geodist locations ditangongyuan beijignzhan km
"1.7200"
127.0.0.1:6379> georadiusbymember locations guozhuang 8 km
1) "guozhuang"
2) "beijignzhan"
3) "ditangongyuan"
127.0.0.1:6379> georadiusbymember locations guozhuang 7 km
1) "guozhuang"
2) "beijignzhan"
127.0.0.1:6379> keys *
1) "locations"
127.0.0.1:6379> type locations
zset
127.0.0.1:6379> object encoding locations
"ziplist"

 The underlying implementation of geohash, the longitude and latitude encoding rules are as follows. gethash can only send the fuzzy range, not the specific latitude and longitude, and is suitable for scenarios where the accuracy is not so high

 

 

 

 6. Interview FAQs

1. The multithreading model of redis , search thread-I/O in redis.conf for detailed notes

Redis execution process: read-parse resp protocol-execute command-write

Multi-threading is off by default, you can set the io-thread parameter in redis.conf, as shown below

 After configuring the io-threads-do-reads parameter, other I/O threads can also perform read and write tasks 

6.1 Client Side Cache 

 After the local client finishes requesting the server, it will cache a piece of data locally, and when the data is modified next time, it will synchronously invalidate the client cache and return the latest data. Redis6.0 only supports stand-alone caching, and clusters cannot cache.

6.2 ACL permissions 

By default, customers can do tasks when they come in, and flushdb will clear all the data.

By setting requirepass foobared in the redis.conf file as the requirepass keyword, you can log in through the auth keyword

 

 Set permission user 

View dangerous users 

 

 

 Persist to configuration file 

 You can also configure ACL permissions to a separate configuration file by setting the aclfile file path in the redis.conf file, and remove parameters such as user aclice on

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_21575929/article/details/125628664