String and hash contain a maximum of 2^32 keys. The maximum value of each key and value is 512MB. List, set, zset have a maximum of 2^32 elements.
string
Usage scenarios
The election
- The node preempted the leaderKeySET leaderKey hostId NX EX 30
- The node is queried every 20 secondsGET leaderKey
- If you are master, do a renewalEXPIRE leaderKey 30
A distributed lock
A distributed lock
A distributed ids
You can use INCR to increment by 1 each time, but if you do this, the client will request Redis once, which affects performance. Recommended number segment generation:
- INCRBYStep size, if key does not exist, increases from 0; Returns the incremented value if key exists.
- Client allocation number segment [result – step, result)
The data structure
The Simple Dynamic String has two fields: len and free.
- Binary security. Unlike c strings, which determine the length of the string by ‘\0’, SDS records the length by len
Functions of Free:
- Space preallocation. When performing expansion operations such as concat, SDS reserves free space to avoid memory allocation consumption caused by continuous expansion
- Inert free space. When performing string reduction operations, SDS does not immediately reclaim space to avoid memory allocation by subsequent expansion operations
hash
Usage scenarios
PV and UV were counted
- User 110 accesses index.htmlHINCRBY index.html0527 110 1
- User 110 accesses index.html againHINCRBY index.html0527 110 1
- Query index.html May 27 PVHGETALL index.html0527, and then calculate the sum (not recommended,HGETALLO(n), which blocks the main thread.
- Query index.html May 27 UVHLEN index.html0527
Statistical UV (BitMap)
- User 110 accesses index.htmlSETBIT index.html0527 110 1
- User 233 accesses index.htmlSETBIT index.html0527 233 1
- Query index.html May 27 UVBITCOUNT index.html0527
Statistical UV (HyperLogLog)
- Users 110, 233 access index.htmlPFADD index.html0527 110 233
- Query index.html May 27 UVPFCOUNT index.html0527
The data structure
Dict hash table, used with more than 64 elements. Chain address method
set
Usage scenarios
Mutual concern
- User 110 follows user 996SADD follow110 996
- The common concern of users 110 and 233SUNION follow110 follow233
The data structure
Dict hash table, used with more than 64 elements. The value stored null
list
Usage scenarios
Flow limiting (token bucket)
- Fixed rate token generationLEFTPUSH limit UUID
- Token generation stops when the upper limit is reachedLLEN limit
- Use the tokenRIGHTPOP limit
The data structure
Quicklist bidirectional linked list, each node is ziplist
zset
Usage scenarios
Current limiting (sliding window)
- User 110 requests to come inZADD limit110 UUID cur_time
- Request within the time rangeZRANGEBYSCORE limit110 cur_time – interval cur_time
- Example Delete historical request recordsZREMRANGEBYSCORE limit110 0 cur_time – interval
list
- User 110 got 99 pointsZADD board 99 110
- Query the top 100 high-scoring usersZREVRANGE board 0 99
- Query the ranking of user 110ZRANK board 110
Delay queue
- Ascending by task timestampZADD deplay timpestamp A
- While checks the first elementZRANGE deplay 0 0
- Compare the timestamp and delete the first element if it can be executedZREM deplay A
The data structure
Dict hash table, key is member, value is score. Zskiplist jump table. Layer 0 stores complete data, and new layers are created randomly, up to 32 layers can hold 2^64 elements.
Contrast red black tree
The command | Red and black tree | Jump table | instructions |
---|---|---|---|
ZADD | O(logN) | O(logN) | In concurrent environments, hops need to be locked with less granularity |
ZSCORE | O(1) | O(1) | Introducing the dict |
ZRANK | O(logN) | O(logN) | Same complexity, skip table implementation is simpler |
ZRANGE | O(N) | O(logN+k) | Jumpers are more efficient |
Compressed data structure
Intset If the set element number is less than 64 use zipmap if the hash element number is less than 64 use ziplist if the zset element number is less than 64 The nodes of the list are ziplist
reference
- Take you to Redis’ Zset
- How to use red-black tree to achieve ranking