preface

Redis is so widely used in Internet technology storage that almost all back-end technology interviewers have to give their friends a hard time about how Redis is used and how it works.

So what is Redis?

Redis is a high performance memory based key-value database.

Why use Redis?

Because the traditional relational database such as Mysql can no longer be applied to all scenarios, such as the inventory deduction of instant killing, the peak of APP homepage access traffic and so on, which are easy to crash the database, the cache middleware is introduced. Currently, there are two commonly used cache middleware in the market, Redis and Memcached. However, Considering their advantages and disadvantages, We finally chose Redis.

For a more detailed comparison, please check out the differences between Redis and Memcached, such as the pros and cons of the two and their respective scenarios, which I will write later.

What data types are supported by Redis?

Redis is distinguished by the types of single key-values. The following types are supported: Strings Lists Sets, Sorted Set hashes

However, if you are a Redis power user and you want to distinguish yourself from other candidates in this interview, you need to add the following data structures: HyperLogLog, Geo, Pub/Sub.

If you want to add extra points, say you’ve played The Redis Module, such as BloomFilter, RedisSearch, or Redis-ML, and the interviewer’s eyes begin to light up, thinking, “This guy has something.”

Note: When I answer questions related to Redis in the interview, I often mention that BloomFilter is used in many scenarios, and it is really sweet to use, the principle is easy to understand, read the article can talk in front of the interviewer.

Redis is single process, single threaded

Redis makes use of queue technology to change the concurrent access to serial access, eliminating the overhead of traditional database serial control.

Redis recycle policy

Volatile – lru: from has set the expiration time data set (for server db [I] expires) choose the least recently used data out of volatile – TTL: From has set the expiration time data set (for server db [I] expires) selected will be expired data out volatile – random: from has set the expiration time data set (for server db [I] expires) select data in eliminating allkeys – lru: Dict dataset (server.db[I].dict) : dict dataset (server.db[I].dict) : dict dataset (server.db[I].dict) If a large number of keys need to be set to expire at the same time, what generally need to be paid attention to?

If a large number of key expiration times are set too centrally, Redis may experience a temporary lag at the expiration point. If there is a serious cache avalanche, we usually need to add a random value to the time to spread out the expiration time.

The home page of e-commerce often uses scheduled tasks to refresh the cache. It may be that a large number of data failure time is very concentrated. If the failure time is the same and a large number of users flood in at the failure time, it may cause cache avalanche

Redis distributed locks

To ensure atomicity, you need to use setnx and EXPIRE in one directive (Lua script)

Get the Redission that uses a third party

How is Redis persisted? How does service master/slave data interact?

RDB implements full mirroring persistence, and AOF implements incremental persistence. Because RDB takes a long time and is not real-time enough, it will cause a large amount of data to be lost during downtime, so AOF is needed to cooperate with it. When the Redis instance is restarted, the memory is rebuilt using RDB persistent files, and the AOF is used to replay recent operations to fully restore the state before the restart.

RDB is the entire table of data, and AOF is the log of each operation. When the server restarts, you need to get the entire table of data, but it may not be complete. However, the mechanism of Redis itself is that if the AOF persistence is enabled and the AOF file exists, the AOF file is loaded first. If AOF is closed or the AOF file does not exist, the RDB file is loaded. After the AOF/RDB file is loaded, Redis starts successfully. When an error occurs in the AOF/RDB file, the Redis fails to start and error information is displayed

conclusion

In a technical interview, whether it’s a Redis question or any other question, you’ll be more likely to give the interviewer a better impression if you give an actual example, or if you answer a question directly from your development process.

If you need to learn more about Java, you can follow my official account :Garnett’s Java Road, looking forward to your arrival!