There are so many programming languages, but Redis should be a middleware that you’re familiar with, whether you’re working on Python, Java, Go, PHP, Ruby, etc.

Redis claims to support 110,000 concurrent reads and 80,000 concurrent writes. Due to its excellent performance and easy operation, Redis is popular among major companies in China, such as Alibaba, Tencent, Bytedance, Baidu, Meituan, Xiaomi and so on.

However, most programmers who often write business code may only use set Value and GetValue in actual work, but lack an overall understanding of Redis.

Only when you really get to know Redis in depth, you will find that Redis is so useful that you will feel like a fish in water in both the job and the interview.

This article introduces eight questions to help you understand Redis. (I also put together a Redis study guide for those who need it.)

1. Why Redis?

Redis is used in projects primarily for performance and concurrency. If only distributed locks these can be replaced by middleware such as Zookpeer.

Performance:

As shown in the figure below, in the case of large concurrency, all requests directly access the database, and the database will have a connection exception. At this point, you need to do a buffer operation using Redis so that the request accesses Redis first, rather than directly accessing the database.

However, Redis also has slow query operations, how to optimize? It’s worth thinking about.

There is no set standard for response time, depending on the interaction. Ideally, our page jumps need to be solved in a split second, and for in-page operations in a split second.

Concurrent:

As shown in the figure below, in the case of large concurrency, all requests directly access the database, and the database will have a connection exception. At this point, you need to do a buffer operation using Redis so that the request accesses Redis first, rather than directly accessing the database.

2. What are the disadvantages of Redis?

Cache and database double-write consistency problems

Cache avalanche problem

Cache breakdown problem

Concurrency contention issues for caches

3. Why is single-threaded Redis so fast?

Multiplexing Resp protocol

Single thread

Memory operations

(The interviewer will ask detailed questions about these points.)

4. Data types and usage scenarios of Redis (how many of these 5 types have you used?)

String: generally do some complex counting function cache;

Hash: Stores two-dimensional data or objects.

List: can realize queue, stack and ordered data storage;

Set: often used for blacklist, wechat lottery and other functions, the application scenario is changeable;

SortedSet: make a leaderboard application, take TOPN operation; Delayed task; Do a range lookup.

5. Redis expiration strategy and memory elimination mechanism?

Periodic deletion, lazy deletion strategy, etc.

6. Redis and database double-write consistency;

(Final consistency and strong consistency)

7. How to deal with cache penetration and cache avalanche?

Small and medium-sized companies generally do not encounter these problems, but large concurrent projects, flow of millions of or so, these two problems must be deeply considered.

8. How to solve the problem of Redis concurrency competing for Key?

Do you know how to solve this?

These 8 questions are from the interview questions of Ali, basically you only need to understand the above 8 questions, about Redis will definitely conquer the interviewer!

I have sorted out the detailed answers to these 8 questions and the Redis study Guide in the document. If you need it, you can send a private message or get it in the comment area.