Introduction to the

Redis is an open source, network-based, memory-based, and optional persistence high-performance key-value pair database written in C language. The read and write speed is very fast. Because it runs in memory, its storage capacity is limited and the cost of distributed cluster is very high

Italian Sicilian Salvatore Sanfilippo (Github: Antirez) founded LLOOGG.com with a friend in 2007 and quickly became frustrated with Mysql’s performance, so he developed Redis database in 2009

↓↓↓↓↓↓↓↓↓↓↓↓↓ ↓↓ the photo has the feeling of labor transformation ↓↓↓↓↓↓↓↓↓↓↓↓↓ ↓↓↓↓↓↓

A good open source project cannot be achieved without the support of a large company. Prior to May 2013, its development was sponsored by VMware, from May 2013 to June 2015, its development was sponsored by Bivito, and from June 2015, Redis development was sponsored by Redis Labs

Data structures supported by Redis

The five commonly used data types supported by Redis refer to the value type, which are String, List, Hash, Set, and ZSet. However, Redis later enriched several data types, which are Bitmaps, HyperLogLogs, and GEO

Since Redis is written based on standard C and only has the most basic data types, in order to meet the five data types used externally, Redis developed a unique set of basic data structures and used these data structures to realize the five data types.

The underlying data structures of Redis include: simple dynamic array SDS, linked list, dictionary, skip list, integer set, compressed list, object

Below you can see the implementation of the underlying data structure corresponding to the external data structure


The following figure illustrates the five data types supported by Redis. For Redis, all keys are strings


Common data structures and application scenarios

Common application scenarios of Redis: caching (data queries, short links, news content, product content, etc.) (Most used) Session separation in distributed cluster architecture. A list of online friends in a chat room. Task queue. (Seckill, Snap, 12306, etc.) App charts. Website visit statistics. Data expiration processing (accurate to milliseconds)

String

The String data structure is a simple key-value type. Although Redis is written in C language, Redis does not use C string representation, but builds a simple dynamic string (SDS). Redis SDS can store not only text data but also binary data compared to C’s native strings. Redis SDS can also obtain a string length of O(1) (C strings are O(N)). In addition,Redis SDS API is safe and does not cause buffer overflows

Application scenario: Generally used in the scenario that requires counting, such as the number of users’ visits, the number of likes and forwarding of hot articles, etc

Common commands:

  • set key value: Sets the value of key-value type
  • del key: Deletes the corresponding key
  • get key: Returns a value based on the key, or null if none exists
  • strlen key: Returns the length of the value string corresponding to key
  • exists key: Determines whether a key exists
  • incr key: Increments value. Value is 1 in the first execution and value + 1 in each execution
  • decr key: decrement, which is the opposite of incr, and becomes negative when executed at 0
  • setex key seconds value: Sets a value of the key-value type with expiration time, such assetex key 60 value, the key-value pair will expire after 60s
  • expire key seconds: Sets the expiration time of an existing key-value pair
  • mset key1 value1 key2 value2/ mget key1 key2: Sets or obtains the key-value value in batches


List

List is a linked List. Linked list is a very common data structure, which is easy to insert and delete data elements and flexible to adjust the length of the list, but the random access to the list is difficult. Many high-level programming languages have built-in implementations of linked lists such as LinkedList in Java, but C does not implement lists, so Redis implements its own LinkedList data structure. Redis list is implemented as a two-way linked list, that is, can support reverse lookup and traversal, more convenient operation, but brings some additional memory overhead

The Redis list is a simple list of strings, sorted by insertion order. You can add an element to the top (left) or bottom (right) of the list

A list can contain up to 232-1 elements (4294967295, more than 4 billion elements per list).

Application scenario: Publish and subscribe or message queue, slow query, can be used as a simple message queue implementation but certainly not compared to professional middleware

Common commands:

  • lpush / rpush key value1 value2: Adds elements to the list header (leftmost)/list tail (rightmost)
  • lpop / rpop key: Take the list header (leftmost)/list tail (rightmost) value
  • lrange key start end: Check the value of the corresponding subscript start end in the list. If end is -1, it is the last
  • llen key: View the length of the linked list


Hash

Hash is similar to the pre-JDK1.8 HashMap, with a similar internal implementation (array + linked list). However, Redis’ hash is more optimized. In addition, the hash is a string mapping table of field and value, which is particularly suitable for storing objects. Later on, you can modify the value of only one field in the object. For example, we can use the hash data structure to store user information, product information, etc

Application scenario: Store object data in the system

Common commands:

  • hset object key value: Sets a nameobjectObject and assign a property, key being the property name and value being the property value
  • hexists object key: Checks whether the properties in the object exist
  • hget object key: Gets the property value of the specified property of the object
  • hgetall object: Gets all the properties and property values of an object
  • hkeys object: Queries how many properties an object has
  • hvals object: Queries how many attribute values an object has


Set

A Set is similar to a HashSet in Java. The set type in Redis is an unordered set in which the elements are in no particular order. A set is a good choice when you need to store a list of data that you don’t want to duplicate, and it provides an important interface for determining whether a member is in a set that a list doesn’t provide

** Application scenarios: ** can easily implement the operation of intersection, union and difference set based on set. For example, you can store all of a user’s followers in one set and all of their followers in one set. Redis can be very convenient to achieve such as common attention, common fans, common preferences and other functions. This is the process of finding the intersection

Common commands:

  • sadd myset value1 value2 value3: Adds three values to the collection named myset. If the values already exist, the addition fails and returns 0
  • smembers myset: See how many values are in mySet
  • sismembers myset value1: Checks whether value1 exists in mySet
  • scard mysetLook at the length of myset
  • sinterstore myset3 myset1 myset2: remove the intersection of myset1 and myset2 and store it in myset3


ZSet

Compared with set, zset adds a weight parameter score, so that the elements in the set can be ordered by Score, and the list of elements can be obtained by the range of Score. It’s kind of like a Combination of Java HashMap and TreeSet.

** Application scenarios: ** Need to sort the data according to a certain weight. For example, in the live broadcast system, the real-time ranking information includes the list of online users in the live broadcast room, various gift leaderboards, bullet messages (which can be understood as the leaderboards of messages by message dimensions) and other information

Common commands:

  • Zadd myzset 3.0 value: Add an element to myzset with a weight of 3.0 and a value of value. You can also add multiple values with different weights
  • zcard myzset: To see how many elements are in myzset
  • zscore myzset value: Check the weight of value in myzset
  • zrange myzset 0 -1: Iterates over the elements in myzset, from 0 to the NTH bit, with -1 for all
  • zrevrange myzset 0 -1: Output in reverse order, from 0 to the NTH bit. -1 indicates traversal


BitMap

A bitmap stores consecutive binary digits (0s and 1s). With a bitmap, only one bit is needed to represent the value or state of an element. The key is the element itself. We know that eight bits can form a byte, so a bitmap itself can greatly save storage space

** Application scenarios: ** Suitable for the need to save status information (such as whether to check in, whether to log in…) And require further analysis of this information. Such as user check-ins, active users, user behavior statistics (like whether or not you liked a video)

Common commands:

  • Setbit mykey 7 1: Sets or clears the specified offset of the string value stored by mykey. It sets the seventh bit of the string bitmap to 1

    1. The return value is the value of that bit before the setbit
    2. Value can only be 0 or 1
    3. The offset starts at 0, and the offset can be 1000, even if the in-situ diagram only has 10 bits
  • Getbit myKey 7: Gets the bit at the offset specified by myKey

  • Bitcount mykey: Counts the number of bits set to 1

  • Bitop operation dest key1 key2 key3: do multiple bitmap and | or | not | xor operation results and save into the dest


Cache expiration mechanism

Since Redis runs in memory, memory resources are limited. If the expiration time is not set for each cache, memory is likely to be washed out

Redis can set the expiration time for the cache, for example:

> setName the dog egg
OK
> expire name 60  # Doggs will expire after 60S, only for existing keys
1
>Setex Name Dog egg 60# Create an egg with expiration time of 60 seconds
OK
> ttl name # Check how long Doggy egg has left to live
52
Copy the code


How do I know if data is out of date

Redis uses an expiration dictionary (think of it as a hash table) to keep the expiration time of data. The key of the expiration dictionary refers to a key in the Redis database. The value of the expiration dictionary is a long integer that holds the expiration time of the database key to which the key refers (a UNIX timestamp with millisecond precision).

Delete policy for expired data

There are two commonly used expired data deletion policies, Redis adopts lazy deletion + periodic deletion

  1. Lazy delete: Check whether the Key is expired when the Key is removed
  2. Periodically Delete: Extracts a batch of keys at intervals and deletes expired keys


Memory obsolescence strategy

Deleting expired data alone does not necessarily prevent the overflow of memory, because the data continues to increase, which is when memory obsolescence is introduced

Redis offers 8 data elimination strategies:

  1. noeviction: Indicates the default policy. When the memory size reaches the maximum value, an error is reported for all memory application operations. Read-only operations can be performed normally
  2. volatile-lru: The key with expiration time set is eliminated using the LRU algorithm
  3. allkeys-lru: All keys are eliminated using the LRU algorithm
  4. volatile-lfu: The key whose expiration time is set is eliminated using the LFU algorithm
  5. allkeys-lfu: All keys are eliminated using the LFU algorithm
  6. volatile-random: The key whose expiration time is set uses random elimination
  7. allkeys-random: All keys are randomly eliminated
  8. volatile-ttl: The key with the expiration time is eliminated based on the expiration time. The earlier the expiration time is, the earlier the key is eliminated

LRU algorithm: Picks the least recently used key

LFU algorithm: Selects the keys that are least frequently used

For the Root user, you can use the following command to change the Redis memory weeding policy:

> config set maxmemory-policy allkeys-random
OK
> config get maxmemory-policy
maxmemory-policy
allkeys-random
Copy the code


reference

  • The Denver nuggets: 3 w word depth oliver | Redis interview visitors, reading this can and the interviewer war a few rounds
  • JavaGuide: Redis FAQ summary
  • Brief: Redis Learning – Bitmap