Interview Java was asked several interview questions about Redis data structure

The answers to the interview questions for Redis data structures are almost covered below.
For time complexity, go to the official website to see the time complexity of each command on the official website.

1. Five data types
of Redis String: String type is the most basic data storage type in Redis. It is a sequence composed of bytes. It is binary safe in Redis, which means that this type can be Accepts data in any format, such as JPEG image data or Json object description information, etc. It is a standard key-value, generally used to store strings, integers and floating-point numbers. The maximum data length that Value can hold is 512MB .
Application Scenario : A very common scenario is used to count the number of website visits, the current number of online users, etc. incr command (++ operation)

list list: Redis' list allows users to push or pop elements from both ends of the sequence. The list is an ordered and repeatable sequence composed of multiple string values, which is a linked list structure. For example, Java's linkedList, when inserting and deleting data at both ends, is very efficient, and inserting data in the middle is very inefficient. The maximum number of elements that can be contained in a List is 2 32  - 1 (that is, 42,9496,7295), which is more than 4 billion.
Application scenarios : 1. Latest news, ranking list . 2. Message queue to complete the message exchange between multiple programs. You can use the push operation to store the task in the list (producer), and then the thread uses the pop operation to take out the task for execution. (Consumer)

Collection set: Redis collections are unordered and non-repeatable. Like lists, they are highly efficient when performing insertion and deletion and judging whether an element exists . The biggest advantage of the collection is that it can beIntersection-union-difference operation. The maximum number of elements that a Set can contain is 2 32  - 1 (ie, 42,9496,7295), which is more than 4 billion .
Application scenarios : 1. Use intersection to seek common friends . 2. Using uniqueness, all independent IPs that visit the website can be counted . 3. When a friend recommends , find the intersection according to the tag , and it can be recommended if it is greater than a certain threshold (critical value) .

Hash: The hash in Redis can be regarded as a map container with String key and String value, which can store multiple key-values ​​in one key. Each Hash can store 2 32  - 1 (ie 42,9496,7295) key-value pairs.
Application scenarios : such as storing, reading, and modifying user attributes (name, age, pwd, etc.)

ordered set zset (sorted set): much like set, it is a collection of strings, and duplicate members are not allowed to appear in one set. The difference between them is that each member in the sorted set will have a score associated with it, and Redis sorts the members of the set from small to large through the score. Although the members of the ordered set must be hooded, scores can be repeated.

Application Scenario: It can be used for the score ranking of a large-scale online game. Whenever the player's score changes, zadd can be executed to update the player's score (score), and then the user information of the top ten of the score can be obtained through zrange .


Reprinted from: https://www.52pojie.cn/thread-558953-1-1.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325589185&siteId=291194637