Redis supports five data types: String, Hash, List, Set, and Zset.

The data type refers to the type of data stored, the value part, the key part is always a string

Type String

String is the most basic type of Redis. Each key corresponds to a value

The String type is binary safe. The Redis String can contain any data. Like JPG images or serialized objects

The String type is the most basic data type of Redis. A String value can store up to 512MB

Suggestion: For the choice of single data and multi-data operation, xiaobian personally believes that the amount of data is small (50 pieces of data), we can choose not to split, and send a single instruction at a time (50 pieces of data); If the amount of data is very large (100 million pieces of data), you can choose to split it into multiple instructions to complete, and send one instruction (1 million pieces) for 100 times. You need to balance the relationship between the sending duration and the execution duration and select the data volume that has the least impact on performance

Note: a key can store up to 512MB. Usually there is not a key that large. Do not try to test the threshold

Usage scenarios

1. In large-scale enterprise applications, separate table operation, use multiple tables to store data of the same type, and solve the problem that primary keys may be repeated (incR, DECR operation, not commonly used)

2. The use of time-limited services, by setting the expiration time, Redis will delete it after the expiration time. It can be used in business scenarios such as time-limited promotional event information and mobile verification code

3. Regular counting: number of micro blog posts, number of fans, etc

4. Some query data, not frequently changed data (objects, lists, etc.), using JSON string stored in Redis, etc

Note:

1) String is a String by default in Redis. When incR is encountered, DECR will be converted to numeric type for calculation

2) All operations of Redis are atomic, using a single thread to process all business, and commands are executed one by one, so there is no need to consider the data impact caused by concurrency

3) If the original data cannot be converted to a value or exceeds the redis value upper limit, an error will be reported

  • Minimum value: long.min_value =-9223372036854775808 (-2^63)
  • MAX_VALUE=9223372036854775807 (2^63-1)

Hash type

Redis hash is a set of key=>value pairs; Hash is a mapping of fields and values of type string, and hash is particularly useful for storing objects

A storage space for holding multiple key-value pair data

The bottom layer uses hash table structure to realize data storage

Hash storage structure optimization:

  • If the number of fields is small, the storage structure is optimized to an array structure
  • If the number of fields is large, the storage structure uses the HashMap structure

Usage scenarios

1. Store some structured data, such as the user’s nickname, age, gender, score, etc., and store a user information object data. (Modify the properties of the object to make deletion easier)

2. Design and implementation of shopping cart on e-commerce website

3. Merchants launch data storage for businesses such as snap up, purchase limit and limited coupon issuance

Note:

1) Value under Hash type can only store strings, and other data types are not allowed. If the data is not retrieved, the corresponding value is nil, which is equivalent to null in the program

2) Each hash can store 2^ 32-1 key-value pairs (over 4 billion)

3) The Hash type is very close to the data storage form of objects, and can flexibly add/delete object attributes. However, Hash is not designed to store a large number of objects, and should not be abused, let alone used as a list of objects

4) The HGEtall operation can obtain all attributes. If there are too many internal fields, the overall data traversal efficiency will be very low and may become a data access bottleneck

List the type

Stores multiple data and distinguishes the sequence of data entering the storage space

A storage space stores multiple data, and the sequence can be reflected through the data

Save a number of data, the underlying use of bidirectional linked list storage structure

A Redis list is a simple list of strings, sorted by insertion order. You can add an element to either the head (left) or the tail (right) of the list

Usage scenarios

1. Storage of mutual relations such as likes and friends

2. News and information websites display the latest news and information in chronological order

3. During enterprise operation, the system generates a large amount of operation data to ensure that multiple servers output operation logs in a unified sequence

4. Apply to data control with operation sequence

5. Task queues

Note:

1) Lists can store up to 2^32-1 elements (4294967295, each List can store more than 4 billion).

2) List has the concept of index, but operation data is often queued into the queue, queued out of the operation, or in and out of the stack operation

3) Get all data end index set to -1

4) The List can be paginated for data, usually the first page of information from the List, the second page and more information through the form of database loading

The Set type

Store large amounts of data to provide greater efficiency in queries

Can save a large amount of data, efficient internal storage mechanism, easy to query

Identical to the Hash storage structure, only keys are stored, no values (nil) are stored, and duplication is not allowed

A Set is an unordered collection of strings, and like a list, it is efficient to perform inserts and deletions and determine the presence of an element

Usage scenarios

1. Random recommendation information retrieval, such as hot song list recommendation, hot news recommendation, hot selling travel routes, big V recommendation, etc

2. Use intersection to find mutual friends

3. Using uniqueness, you can count all the independent IP addresses that visit the website

4. When friends make recommendations, calculate the intersection according to the tag, and the recommendation can be made if the tag is greater than a certain threshold

5. Apply to association search, second degree association search and deep association search of similar information

Note:

1) The Set type does not allow duplicate data. If the added data already exists in the Set, only one copy will be kept

2) Although Set has the same storage structure as Hash, it cannot enable the space to store values in Hash

3) The maximum number of members in the set is 2^ 32-1 (4294967295, each set can store more than 4 billion members)

Sortet_Set type

Data sorting is conducive to the effective presentation of data, and it is necessary to provide a way to sort data according to its own characteristics

New storage model that can hold sortable data

Add sortable fields to the storage structure of a Set

A zset, like a set, is a set of elements of type string. It does not allow duplicate members, except that each element is associated with a double score. Redis uses scores to sort the members of a collection from smallest to largest

Members of a Zset are unique, but scores can be repeated

Usage scenarios

1. When an ordered and non-repetitive collection list is needed

2. Ranking corresponding to the combination sorting function of the counter

3. All kinds of rankings and data storage of list information to obtain ranking

Note:

1) Min and Max are used to limit the conditions of search queries

2) Start and stop are used to limit the scope of the query, and apply to the index, indicating the start and end of the index

3) Offset and count are used to limit the query scope and act on the query result, indicating the starting position and total amount of data

4) The data storage space saved by SCORE is 64 bits. If it is an integer, it ranges from -9007199254740992 to 9007199254740992

5) The data stored by SCORce can also be a double, which may lose precision due to the characteristics of a double floating-point number. Use with caution

6) The underlying storage of sorTED_SET is still based on the set structure, so the data cannot be repeated. If the same data is added repeatedly, the score value will be overwritten repeatedly and only the result of the last modification will be retained

Today’s share is over here, I hope to bring you help!!