Java Gang Dachang Interview Hundred Days Sprint-Accumulate over time, three questions a day [Day 2]-Redis Chapter 1

Hello everyone, I am Chen Haha, and I have been drifting north for five years. Friends who know me know that I was born in a non-major class, renounced halfway through, and college is also very poor! Coming to Beidiao with this background, you don’t know what you will experience 🙃🙃.

I can’t agree. I believe everyone is like me 都有一个大厂梦. As a senior Java player, I know the importance of interviews. Next, I’m going to spend 100 days, based on the high-frequency interview questions in Java post interviews, 每日3题and take you in the form of Go through the popular interview questions and the appropriate answers. Of course, I won't go too deep, because I'm afraid I can't remember! !

因此,不足的地方希望各位在评论区补充疑惑、见解以及面试中遇到的奇葩问法, I hope these 100 days will allow us to fly over qualitatively and rush into the big factory together! ! , Let us learn together (juan)! ! !
Insert picture description here


This column Java developers Kong high-frequency face questions from each of the following major technology Java基础知识stack: 集合容器, 并发编程, JVM, Spring全家桶, MyBatis等ORMapping框架, MySQL数据库, Redis缓存, RabbitMQ消息队列, , Linux操作技巧and so on.

Interview question 1: Why use Redis? Where is the business used?

Seriously answer:

Redis is one of the most well-known methods to alleviate high concurrency and improve high-availability capabilities, and it has a significant effect in improving server performance.

I have to mention the high concurrency scenario here. We know that the core point in the concurrency scenario is the database. The purpose of introducing caching (and introducing any load balancing, clustering, etc. strategies) is to reduce the pressure on the database and let more original hits on the DB. The request was intercepted and processed in the middle. Just like you ask a fake fart to do something big and have a big boss sign it?

Insert picture description here

To put it in a simple way, high concurrency is to the server as if you were punched by someone. This fist is very hard. If you are shirtless, you can vomit blood with a punch. . So in order to withstand this punch? Wear padded jacket, wear pads, wear...Yes, as long as it is thick enough, I thought you were tickling me~ Similarly, Redis is a thick and elastic padded jacket.

Having said that, how thick and elastic is it? The operation cache is the direct operation of the memory, and the speed is quite fast. The number of requests that the direct operation cache can withstand is far greater than that of the direct access to the database.

Redis advantages:

  • The read and write performance is excellent. Redis can read at a speed of 110,000 times/s and write at a speed of 81,000 times/s.
  • Support data persistence, support AOF and RDB two persistence methods.
  • Support transactions, all operations of Redis are atomic, and Redis also supports the atomic execution of several operations after merging.
  • Rich data structure, besides supporting string type value, it also supports hash, set, zset, list and other data structures.
  • Supports master-slave replication, the master will automatically synchronize data to the slave, and read and write can be separated.
  • Support a large number of cluster nodes.

Suppose the user accesses some data in the database for the first time. This process will be slower because it is read from the hard disk. Store the data accessed by the user in Redis, so that the next time you access the data, you can get it directly from the cache. Similarly, we can transfer part of the data in the database to the cache, so that part of the user's request will be directly hit the cache instead of the database (that is, intercepted halfway). If the corresponding data in the database is changed, the corresponding data in the cache can be changed synchronously!

In our business, including hot word query, some real-time ranking data, traffic and likes statistics, session sharing, etc., can be imported into Redis for processing.

Insert picture description here

In-depth inquiry:

Follow-up 1: What data types are there in Redis?

Rich data types. Redis has 8 data types. Of course, the 5 types commonly used are String, Hash, List, Set, and SortSet. They all organize data based on key values. Each data type provides a very rich operation commands, which can meet most of the needs. If you have special needs, you can create new commands yourself through the lua script (atomic);

Insert picture description here

Follow-up 2: What are the differences between Redis and Memcached?

Both are 非关系型内存键值数据库. Now companies generally use Redis to implement caching. Why not use Memcached?

Types of1. Support memory
2. Non-relational database
1. Support memory
2. Key-value pair form
3. Cache form
Data storage type1. String 2. List 3. Set 4. Hash 5. Sort Set1. Text type 2. Binary type
Additional features1. Publish/subscribe model 2. Master-slave partition 3. Serialization support 4. Script support [Lua script]Multi-threaded service support
Network IO model单线程Multiple IO multiplexing model多线程, Non-blocking IO mode
持久化支持1. RDB 2. AOFnot support
Cluster modeNatively supports cluster mode, which can realize master-slave replication and read-write separationThere is no native cluster mode, and you need to rely on the client to write data to the shards in the cluster
Memory management mechanismIn Redis, not all data is always stored in memory, and some values ​​that have not been used for a long time can be exchanged to diskMemcached data will always be in the memory. Memcached divides the memory into blocks of a specific length to store data to completely solve the problem of memory fragmentation.
Applicable sceneComplex data structure, with persistence, high availability requirements, large value storage content, maximum 512MPure key-value, a very large amount of data, and a very large amount of concurrent business
  • All values ​​of memcached are simple strings. Redis, as its replacement, supports richer data types.
  • Redis is much faster than memcached
  • Redis can persist data to disk. This is very important. Downtime and power failure are no longer a flaw.

Follow-up 3: How does Redis prevent abnormal data from being lost? How to persist?

RDB 持久化 (快照)

  • Generate a snapshot of all data at a certain point in time and store it on the hard disk. When the amount of data is large, it will be slow.
  • You can copy the snapshot to another server to create a server copy with the same data.
  • If the system fails, the data after the last snapshot was created will be lost.

AOF 持久化(即时更新)

  • Add the write command to the end of the AOF file (Append Only File).
  • To use AOF persistence, you need to set synchronization options to ensure the timing of the write command synchronization to the disk file. This is because writing to a file does not immediately synchronize the content to the disk, but first stores it in the buffer, and then the operating system determines when to synchronize to the disk.
There are the following synchronization options (synchronization frequency): alwayseach write command is synchronized; everyseconce per second; nolet the operating system decide when to synchronize.
everysecThe option is more appropriate, which can ensure that only about one second of data will be lost when the system crashes, and Redis will perform synchronization once a second with almost no impact on server performance;

Interview question 2: Why is Redis single-threaded?

Redis is single threaded. How can I exploit multiple CPU / cores?

It's not very frequent that CPU becomes your bottleneck with Redis, as usually Redis is either memory or network bound. For instance, using pipelining Redis running on an average Linux system can deliver even 1 million requests per second, so if your application mainly uses O(N) or O(log(N)) commands, it is hardly going to use too much CPU.

However, to maximize CPU usage you can start multiple instances of Redis in the same box and treat them as different servers. At some point a single box may not be enough anyway, so if you want to use multiple CPUs you can start thinking of some way to shard earlier.

You can find more information about using multiple Redis instances in the Partitioning page.

However with Redis 4.0 we started to make Redis more threaded. For now this is limited to deleting objects in the background, and to blocking commands implemented via Redis modules. For future releases, the plan is to make Redis more and more threaded.

Seriously answer:

The above is the explanation given by the Redis official website (the official document link ). After the translation, let's 因为Redis的瓶颈不是CPU的运行速度,而往往是网络带宽和机器的内存大小。say it briefly. Besides, single-threaded switching has low overhead and is easy to implement. Since single-threading is easy to implement, and the CPU will not become a bottleneck, it is logical to adopt a single-threaded solution, of course, to avoid many pitfalls in multi-threading. 对了,一个节点是一个单线程.

In-depth inquiry:

Follow-up 1: Single-thread uses only single-core CPU, which is too wasteful. Is there any way to use the performance of multi-core CPU?

We can open multiple Redis instances on a single machine. The single thread we have been emphasizing is that there is only one thread to process our network requests. In fact, when a formal Redis Server runs, there must be more than one thread, all in the form of a cluster, how many nodes, so you don't have to worry about this kind of problem in the actual environment.

Interview Question 3: Talk about the understanding of cache penetration, cache breakdown, and cache avalanche

Seriously answer:

Cache penetration: It means 缓存和数据库中都没有的数据that all requests are hit to the database, and then the database cannot be found (such as null), causing the number of threads in the database to be full for a short time, causing other services to be blocked, and ultimately causing online services to be unavailable. This situation usually comes from hacker classmates.

Cache breakdown: Refers to 缓存中没有但数据库中有的数据(usually the hot data cache time expires). At this time, due to the large number of concurrent users, the data is not read in the cache at the same time, and the database is checked at the same time, which causes the database pressure to increase instantaneously, and the online system is stuck live.

Cache avalanche: refers to an 缓存同一时间大面积的失效upgraded version of cache breakdown.

In-depth inquiry:

Follow-up 1: Then you talk about the solution for cache breakdown

Insert picture description here
  1. According to the actual business situation, maintain a hot data table in Redis, set the batch to never expire (such as top1000), and update the top1000 data regularly.
  2. Add a mutex key
After the mutex
  cache breakdown, multiple threads will query the data in the database at the same time, so we can use a mutex to lock it on the first request for data query.
At this point, other threads will wait if they can't get the lock, wait for the first thread to query the data, and then do the cache. Later threads come in and find that there is already a cache, so they go directly to the cache.
    static Lock reenLock = new ReentrantLock();
    public List<String> getData04() throws InterruptedException {
        List<String> result = new ArrayList<String>();
        // 从缓存读取数据
        result = getDataFromCache();
        if (result.isEmpty()) {
            if (reenLock.tryLock()) {
                try {
                    // 从数据库查询数据
                    result = getDataFromDB();
                    // 将查询到的数据写入缓存
                } finally {
                    reenLock.unlock();// 释放锁

            } else {
                result = getDataFromCache();// 先查一下缓存
                if (result.isEmpty()) {
                    Thread.sleep(100);// 小憩一会儿
                    return getData04();// 重试
        return result;

Daily summary

Today we reviewed the three Redis questions that were often tested in interviews. Have you been able to figure it out? 对了,如果你的朋友也在准备面试,请将这个系列扔给他,如果他认真对待,肯定会感谢你的!!Okay, let’s stop here today 记得在评论区留言:打卡。, to encourage students who have lost their studies .

Cache penetration, cache breakdown, and cache avalanche are hot topics, I believe some students will say it, why not finish it in one breath 🤣 🤣Don’t worry, separate it, the impression will be deeper! I can't remember the main too many. . In the second and third chapters of Redis, we will introduce some practical scenarios, after all, we can’t remember the theory~~