The real question and analysis of the redis interview asked by Tencent in 2021

Redis is simply a database, but unlike traditional databases, redis data is stored in memory, so the storage and writing speed is very fast, so redis is widely used in the cache direction. In addition, redis is often used for distributed locks. Redis provides a variety of data types to support different business scenarios. In addition, redis supports transactions, persistence, LUA scripts, LRU-driven events, and multiple cluster solutions. Therefore, we can often see redis in interviews. Today, I will bring you a real redis interview question and analysis. Later, I will share some real questions of the redis regular exam this year.

Ant Financial's Java R&D post: Redis common data structure and usage scenario analysis

 

Interviewer: Redis common data structure and usage scenario analysis

1. String

Common commands: set, get, decr, incr, mget, etc.

The String data structure is a simple key-value type, and the value can be not only a String, but also a number. Conventional key-value cache application; Conventional counting: the number of Weibo, the number of fans, etc.

2.Hash

Common commands: hget, hset, hgetall, etc.

Hash is a mapping table between field and value of string type. Hash is especially suitable for storing objects. During subsequent operations, you can directly modify only the value of a field in this object. For example, we can store user information, product information, etc. in the Hash data structure. For example, below, I use the hash type to store some of my own information:

Ant Financial's Java R&D post: Redis common data structure and usage scenario analysis

 

3.List

Common commands: lpush, rpush, lpop, rpop, lrange, etc.

List is a linked list. Redis list has many application scenarios, and it is also one of the most important data structures of Redis. For example, Weibo watchlist, fan list, message list and other functions can all be implemented with Redis list structure.

The Redis list is implemented as a doubly linked list, which can support reverse lookup and traversal, which is more convenient to operate, but it brings some additional memory overhead.

In addition, you can use the lrange command, which is how many elements to read from a certain element, and you can implement paging query based on the list. This is a great function, based on redis to achieve simple high-performance paging, and you can do continuous pull-downs like Weibo Pagination (going down one page at a time), high performance.

4.Set

Common commands: sadd, spop, smembers, sunion, etc.

The external function provided by set is similar to the function of a list. The special feature is that set can automatically sort the duplicates.

When you need to store a list of data and do not want duplicate data, set is a good choice, and set provides an important interface for judging whether a member is in a set collection, which is also not provided by list. The operation of intersection, union, and difference can be easily realized based on set.

For example, in a Weibo application, all the followers of a user can be stored in a collection, and all its fans can be stored in a collection. Redis can easily implement functions such as common attention, common fans, and common preferences. This process is also the process of finding intersection. The specific commands are as follows:

Ant Financial's Java R&D post: Redis common data structure and usage scenario analysis

 

5.Sorted Set

Commonly used commands: zadd, zrange, zrem, zcard, etc.

Compared with set, sorted set adds a weight parameter score, so that the elements in the set can be sorted according to score.

Example:  In the live broadcast system, the real-time ranking information includes online user lists in the live broadcast room, various gift rankings, barrage messages (can be understood as message rankings based on message dimensions) and other information, suitable for storage using the SortedSet structure in Redis .

40 common resdis interview questions (including analysis)

  1. What is Redis?
  2. Redis data type?
  3. What are the benefits of using Redis?
  4. What are the advantages of Redis over Memcached?
  5. What are the differences between Memcache and Redis?
  6. Is Redis single-process single-threaded?
  7. What is the maximum capacity that a string value can store?
  8. What is the persistence mechanism of Redis? Their advantages and disadvantages?
  9. Redis common performance problems and solutions:
  10. Redis expired key deletion strategy?
  11. Redis recycling strategy (elimination strategy)?
  12. Why does edis need to put all the data in memory?
  13. Do you understand the synchronization mechanism of Redis?
  14. What are the benefits of pipeline, why use pipeline?
  15. Have you ever used Redis cluster? What is the principle of cluster?
  16. Under what circumstances will the Redis cluster solution cause the entire cluster to be unavailable?
  17. What are the Java clients supported by Redis? Which one is the official recommendation?
  18. What are the advantages and disadvantages of Jedis and Redisson?
  19. How does Redis set the password and verify the password?
  20. Talk about the concept of Redis hash slot?
  21. What is the master-slave replication model of Redis cluster?
  22. Will there be write operations lost in the Redis cluster? why?
  23. How is replication between Redis clusters?
  24. What is the maximum number of nodes in a Redis cluster?
  25. How to choose a database for Redis cluster?
  26. How to test the connectivity of Redis?
  27. How to understand Redis transaction?
  28. What are the commands related to Redis transactions?
  29. How to set the expiration time and permanent validity of Redis key?
  30. How does Redis optimize memory?
  31. How does the Redis recycling process work?
  32. Are there any ways to reduce Redis's memory usage?
  33. What happens when Redis runs out of memory?
  34. How many keys can a Redis instance store at most? List, Set, Sorted Set, how many elements can they store at most?
  35. There are 2000w data in MySQL and only 20w data in redis. How to ensure that the data in redis are all hot data?
  36. What scenario is Redis most suitable for?
  37. If there are 100 million keys in Redis, 10w of them start with a fixed, known prefix. What if you can find them all?
  38. If there are a large number of keys that need to be set to expire at the same time, what should generally be noted?
  39. Have you used Redis as an asynchronous queue? How do you use it?
  40. Have you used Redis distributed locks? What is it about?

Ant Financial's Java R&D post: Redis common data structure and usage scenario analysis

 

to sum up:

Because the analysis is too long, I have not listed all the analysis for everyone. In addition to the 40 real redis interview questions, there are 1000 real spring, mybatis, MySQL, dubbo, etc. interview questions. Friends who need a complete analysis and thousands of real interview questions can pay attention to it. After praising this article, add assistant vx: bjmsb10 to get it. Finally, I hope you can find your favorite offer in the new year!

Guess you like

Origin blog.csdn.net/Java0258/article/details/112374682