1, Introduction to Redis Foundation

1. Introduction to redis Basics

1.1 redis is a high-performance key value cache database,

1. Support data persistence
2. Rich data types
3. Atomic operation

1.2 role of redis

1. Provide cache service
2. For distributed locks
3. Used for message queuing (not the advantage of redis)

1.3 why Redis

The traditional application of Mysql based on disk reading and writing is not enough to support more visits,
We can use redis, a memory based caching middleware, to eliminate the need for real-time data
It can be put into redis, so that it can support more traffic easily

  • at work
    For example:
    1. For the cache of course information, a large number of client requests directly hit the cache, and there is no need to query the database
    2. Make distributed locks to control the competition of resources

1.3. 1. High performance

1. Pure memory access
2. Using io multiplexing model

  • 1. Monitor socket time through io multiplexing model
  • 2 and put it into the event queue
  • 3 event dispatcher processing, processing
    • Connect response processor (handle socket connection)
    • Command processor (read the request data of socket)
    • Command replier (return processing result)

3. Single thread avoids the consumption caused by thread switching and locking
4. Redis supports powerful data structure and provides faster access

1.4 why use redis instead of mencached?

Because redis supports more data structures and persistent storage, it can recover data through RDB and AOF during disaster recovery
The value of memcache can be 1M at most, and the expiration time can be set to 30 seconds at most. Persistence is not supported. At present, many scenarios have turned to redis
(it cannot be persisted. The expiration time can be set to 30 at most, and the value value can only be 1M)

1.5 what data structures does Redis have

1.5.1 String

Implementation mechanism:

  • SDS simple dynamic string, which saves the length field and pre allocated space and inert recycling
struct SDS{ 
    T capacity  capacity
    T len Array length
    byte flags Flag bit
    char[] content Array contents
}

Usage scenario:

  • Cache: in the classic usage scenario, common information, strings, pictures or videos are put into redis. Redis is used as the cache layer and mysql is used as the persistence layer to reduce the reading and writing pressure of mysql.
  • Counter: redis is a single thread model. The next command will be executed only after one command is executed. At the same time, the data can be landed to other data sources step by step.
  • Session: the common scheme spring session + redis realizes session sharing,

1.5.2 list

Implementation mechanism:

  • When the elements are relatively small, ZipList (compressed list) is used to place all elements next to each other and allocate a continuous space,
struct ziplist{ 
       int zlbytes  //Bytes occupied by the whole compressed list
       int zltail_offset //The offset of the last element from the actual position of the compressed list
       int zllength  //Number of elements
       T[] entries //Element content
       int zlend //The end value of the standard compressed list is always 0XFF
}
struct entry{
    int prevlen     //Byte length of the previous entry
    int encoding   //Element type code
    byte[] content  //Element content
}
  • When there is more data, it becomes QucikList (quick list) and connects multiple ziplists with pointers.
struct quicklist{
       quicklistNode* head
       quicklistNode* tail
       long count         //Element sum
       int nodes         //Number of ziplost nodes
       int compressDepth  //LZF algorithm compression depth
}
struct   quicklistNode{
       quicklistNode* prev
       quicklistNode* next
       ziplist* zl     //If the data of the current node is not compressed, it points to a ziplost structure; Otherwise, it points to a quicklistLZF structure.     
       int32 size      //Total number of bytes in ziplost
       int16 count    //Number of elements in ziplost
       int2  encoding//The storage type is element byte array LZF compressed storage
}

Usage scenario:

  • Microblog TimeLine: someone posted a microblog and added lpush to the TimeLine to display new list information.
  • Message queue
  • List data

1.5.3 Hash

Implementation mechanism:

  • When the data volume is small, use the compressed list zipList
  • Use dictionary dict when there is a large amount of data
redisDb{
  dict
  expires
  blocking_keys
  ready_keys
  watched_keys
  id
  avg_ttl
  expires_cursor
  defray_later
}
dict{
    type
    privdata
    dictht ht[2]  //It contains two hashtable s, one for storing values and one for expanding and shrinking dictionaries
    rehashidx
    iterators
}
dictht{
    dictEntry table  //Linked list
    size    //Length of the first dimensional array
    sizemask   //
    userd    //Number of elements in hash table
}
dictEntry{
   key
   union{
   val
   u64
   s64
   }
   dictEntry* next  //Next node in the linked list
}

Usage scenario:
Cache: it can be intuitive, save more space than string, and maintain cache information, such as user information, video information, etc.

1.5.4 set

Implementation mechanism:
When two conditional tasks are met, set will. 1. The number of elements is less than set Max intset entries, 2 Element cannot be represented by an integer

  • When satisfied, store data with dict
  • If not, use intset to store data
struct intset{
 int32  encoding      //Determines the integer bit width
 int32 lenght       //Number of elements
 int<T>  contents   //Integer array
}

Usage scenario:

  • Like, step, collect, etc. can be put into set
  • Some de duplication scenes

1.5.5 ZSet

Implementation mechanism:

  • When the amount of data is small, use the compressed linked list ziplost
  • If there is a large amount of data, use the skip list
struct zslnode{
     string value   //key
     double score  //fraction
     zslnode*[] forwards   //Multilayer connection pointer
     zslnode*    backward   //Backtracking pointer
}
struct  zsl{ 
      zslnode* header   //Jump list header pointer
      int maxLevel       //Skip the current top level of the list
      map<String,zslnode*> ht      //All key value pairs of hash structure
}

Usage scenario:
Leaderboard: an orderly collection of classic usage scenarios. For example, websites such as novel videos need to make a ranking list of novel videos uploaded by users. The list can be scored and ranked according to the number of users' attention, update time, number of words, etc.

1.5. 6. More advanced ones include bloom filter, hyperlog (used for mass data De duplication), GEO (geographic location)

Keywords: Java Redis

Added by holstead on Sat, 18 Dec 2021 00:38:38 +0200