the basic concept of redis

The Basic Concept Of Redis

  the basic data type of redis

  1. String . format: set key value . String is safe for binary files. It means that the string of redis can be included any data type , such as jpg , serialized objects. A String of reids’s key maximum occupancy 512M.

  2. Hash . format : hmset name key1 value1 key2 value2. Redis hash is a key-value collection.Redis hash is a mapping table for the string type of field and value , so redis hash is fit for storing object.

  3. List . format : lpush name value . Redis List is simple string List, sorted by inserted. You can add an element at list of head(left) or tail(right).

    • Insert an element at the head of list, as follows rpush name value
    • Insert an element at the tail of list , as follows lrem name index
    • remove the list of key name and return the count of removed.llen name
  4. Set . format: sadd name value. Redis set is a unsorted string type of collection. Set is realized by hash table , so its time complexity is O(1) for add 、delete or search .

  5. zset . format : zadd name score value. Redis set is a sorted unrepeat string type of collection.Every element has a relationship with a double score. Zset is sorted by redis using this socre.

    what’s the meaning of Redis persistence ? How many methods is used by Redis persistence ? What does its advantage and shortcoming ?

      Redis persistence is an action writing data from memory to disk , it can be prevented data lose when pc powered down.
      Redis had provided two persistence methods, RDB (default) and AOF.

    RDB

      RDB is a abbreviation of Redis DataBase.The core of RDB function are rdbSave(generate RDB file) and rdbLoad(load data to memory from disk).RDB is a strategy that uses the COW(copy on write) of operation system to implement.COW is need OS to support,it is a native function supported by OS.It will be generate a process when Redis use COW to implement RDB.
    avatar

    AOF

      AOF is a abbreviation of Append-only file.It is a after-log function,don’t need check the command of wirite.RDB implement data backup has three strategy.There is as below:

  • always (when you run a write command,it will synchronizate AOF file at once)

  • everysec(OS will write the memory of cache to AOF file every second.)

  • no(OS will write the memory of cache to AOF file when the size of cache is enough big or the cache has been existed for a long time. )
    avatarThe function of
    flushAppendOnlyFile will be invoked when the redis run the timing task.This function will do as belows:

  • WRITE: according to the cause, wirte the cache of aof_buf to AOF file

  • SAVE: according to the cause, invoke the function fsync or fdatasync , save AOF file in disk.

    The storage of structure in RDB or AOF

It is a redis communication protocol(RESP,a communication protocol at Redis client and server, has many advantages,such as simplly implemented,fastly analysis, excellently readable) file.

Compre AOF and RDB

  • AOF file has the higher update frequency than RDB, so use AOF file to restore at first.
  • AOF is more safe than RDB.
  • RDB is more performance than AOF.
  • if you set this two methods, AOF will be loaded at first.

###Redis cache、Redis Cache Penetration 、Redis cache breakdown、Redis cache avalanche

Redis cache

   when the front send the request to get data , the back-end will get the data from redis,if the result is not empty,return the result to the front, or the back-end will get data from database. if the result of databse is empty then return null to the front or return this result.There is a full and right process for Redis,but it will have a problem , if a requesting run the full process and somebody will invoke this request without stop,the database will down.This phenomenon also called as Redis Cache Penetration. To present this phenomenon, there are two methods to solve it as below.

  • the API should add check for needing search the database directly.
  • if you get the empty data from redis cache and the result of database also be empty then you can write the key-null to the redis in a short time.
      If the result of redis is null but the database has this value meanwile many users is visiting this value,the database will be affected greatly.This phenomenon also called as Redis Cache breakdown and it is in the light of one specific key. There are two methods to solve it as below.
  • set the hot data is no expire
  • use the mutex according to redis key
      Redis cache avalanche is that many redis’s datas expire at the same time, the back-end will request the database at once , it is possible for causing the datase down.There are three methods to solve it as below.
  • set the expire time of redis data at Randomly.
  • set the hot data is no expire
  • set the hot data in different database
    Reference by REDIS