Sword refers to Offer: 14 scenes of Redis!

Yesterday a netizen was asked in an interview with Tencent, Redis is single-threaded, why is it so fast? His answer was not very good, and the interviewer also kindly reminded some key points, but because he was not prepared, he failed again.

Regarding this question, I actually wrote this answer a year ago, "Redis is a single-threaded structure, but why can single-threaded support high concurrency? ".

Today we will discuss another high-frequency interview question about Redis: What are the usage scenarios of Redis?

This question is actually quite routine. The first answer will often be "cache". Caching is indeed the most used area of ​​Redis, but this answer is often guided by interviewers to other technologies, such as Memcache. Or it may ask about cache coherency.

If your answer is not very good, the interviewer may ask, what are the other usage scenarios for Redis? Or what can Redis be used for?

Some people may answer: distributed locks. Speaking of this, the interviewer may ask, how to achieve it? What is the principle? What's wrong? I turned around and found that I had jumped into the pit again.

So, today I will take a moment, based on my previous experience in e-commerce. Share the top 10 usage scenarios of Redis.

1. Record the number of likes, comments and clicks (hash) of posts or articles.

2. Record the user's post ID list (sorted) for quick display of the user's post list (zset).

3. Record the title, abstract, author, and cover information of the post for display on the list page (hash).

4. Record the like user ID list of the post and the comment ID list for display and de-duplication counting (zset).

5. Cache the content of recent hot posts (post content takes up a lot of space) to reduce database pressure (hash).

6. Record the related article ID of the post, and recommend related posts (list) based on the content.

7. If the post ID is incremented by an integer, you can use Redis to assign the post ID (counter).

8. The relationship between collections and posts (zset).

9. Record the hot list post ID list, total hot list and category hot list (zset).

10. Cache user behavior history and perform malicious behavior filtering (zset, hash).

11. Snapshot, panic buying (distributed lock).

12. Conversation, hot data (cache).

13. Collect goods, chat, group chat, publish and subscribe (pub/sub).

14. System decoupling, queues (lpush, rpop).

There are also some scenes that I have not listed. As long as you can answer the above, basically the interview questions are not big. However, we should also pay attention not to deliberately use a certain technology in order to use a certain function. For example, the author of Redis does not recommend using Redis for queues and other aspects.

Welcome to share your thoughts with me in the message area, and you are also welcome to record your thought process in the message area. Thanks for reading. If you think this article is helpful to you, you are welcome to share it with more friends.

Guess you like

Origin blog.51cto.com/15127565/2664960