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

  1. The node preempted the leaderKeySET leaderKey hostId NX EX 30
  2. The node is queried every 20 secondsGET leaderKey
  3. 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:

  1. INCRBYStep size, if key does not exist, increases from 0; Returns the incremented value if key exists.
  2. Client allocation number segment [result – step, result)

The data structure

The Simple Dynamic String has two fields: len and free.

  1. Binary security. Unlike c strings, which determine the length of the string by ‘\0’, SDS records the length by len

Functions of Free:

  1. Space preallocation. When performing expansion operations such as concat, SDS reserves free space to avoid memory allocation consumption caused by continuous expansion
  2. 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

  1. User 110 accesses index.htmlHINCRBY index.html0527 110 1
  2. User 110 accesses index.html againHINCRBY index.html0527 110 1
  3. Query index.html May 27 PVHGETALL index.html0527, and then calculate the sum (not recommended,HGETALLO(n), which blocks the main thread.
  4. Query index.html May 27 UVHLEN index.html0527

Statistical UV (BitMap)

  1. User 110 accesses index.htmlSETBIT index.html0527 110 1
  2. User 233 accesses index.htmlSETBIT index.html0527 233 1
  3. Query index.html May 27 UVBITCOUNT index.html0527

Statistical UV (HyperLogLog)

  1. Users 110, 233 access index.htmlPFADD index.html0527 110 233
  2. 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

  1. User 110 follows user 996SADD follow110 996
  2. 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)

  1. Fixed rate token generationLEFTPUSH limit UUID
  2. Token generation stops when the upper limit is reachedLLEN limit
  3. Use the tokenRIGHTPOP limit

The data structure

Quicklist bidirectional linked list, each node is ziplist

zset

Usage scenarios

Current limiting (sliding window)

  1. User 110 requests to come inZADD limit110 UUID cur_time
  2. Request within the time rangeZRANGEBYSCORE limit110 cur_time – interval cur_time
  3. Example Delete historical request recordsZREMRANGEBYSCORE limit110 0 cur_time – interval

list

  1. User 110 got 99 pointsZADD board 99 110
  2. Query the top 100 high-scoring usersZREVRANGE board 0 99
  3. Query the ranking of user 110ZRANK board 110

Delay queue

  1. Ascending by task timestampZADD deplay timpestamp A
  2. While checks the first elementZRANGE deplay 0 0
  3. 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