Summary of the latest version of Redis frequently asked interview questions in 2020

Many friends only read the interview questions before the interview, which is normal. Because it takes time to collect interview questions and practice, I collected some popular interview questions from many interviewers. I hope it will help you in your future work and interview!

There are too many interview questions and space is limited, so only some of the frequently asked Redis interview questions are shown here. Friends who need the full version and more relevant interview materials can click the link below to get them for free!
Link: Click here! ! ! Password: CSDN

Insert picture description here

1. What is Redis?

Redis is completely open source and free, complies with the BSD protocol, and is a high-performance key-value database.

Redis and other key-value caching products have the following three characteristics:

Redis supports data persistence. The data in the memory can be saved on the disk and can be loaded again for use when restarting.

Redis not only supports simple key-value type data, but also provides storage for list, set, zset, hash and other data structures.

Redis supports data backup, that is, data backup in master-slave mode.

Redis advantage

Very high performance-Redis can read 110,000 times/s, and write 81,000 times/s.

Rich data types-Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type operations for binary cases.

Atomic-All Redis operations are atomic, which means that they are executed successfully or not executed at all. A single operation is atomic. Multiple operations also support transactions, that is, atomicity, packaged by MULTI and EXEC instructions.

Rich features-Redis also supports publish/subscribe, notification, key expiration and other features.

How is Redis different from other key-value stores?

Redis has more complex data structures and provides atomic operations on them, which is an evolutionary path different from other databases. Redis data types are based on basic data structures and are transparent to programmers without additional abstraction.

Redis runs in memory but can be persisted to disk, so memory needs to be weighed when reading and writing different data sets at high speed, because the amount of data cannot be larger than hardware memory. Another advantage of the in-memory database is that compared to the same complex data structure on disk, it is very simple to operate in memory, so that Redis can do a lot of internally complex things. At the same time, in terms of disk format, they are generated in a compact manner because they do not require random access.

Second, the data type of Redis?

Answer: Redis supports five data types: string (string), hash (hash), list (list), set (collection) and zsetsorted set: ordered set).

The most commonly used in our actual projects is string and hash. If you are an advanced user of Redis, you need to add the following data structures HyperLogLog, Geo, Pub/Sub.

If you say that you have played Redis Module, such as BloomFilter, RedisSearch, Redis-ML, the interviewer's eyes will start to shine.

3. What are the benefits of using Redis?

1. Fast speed, because the data is stored in memory, similar to HashMap, the advantage of HashMap is that the time complexity of search and operation is O1)

2. Support rich data types, support string, list, set, Zset, hash, etc.

3. Support transactions, operations are all atomic. The so-called atomicity means that all data changes are executed or not executed at all

4. Rich features: can be used for caching, message, set expiration time according to key, it will be deleted automatically after expiration

4. What are the advantages of Redis over Memcached?

1. All values ​​of Memcached are simple strings, redis as its replacement, supports richer data types

2. Redis is faster than Memcached 3. Redis can persist its data

5. What are the differences between Memcache and Redis?

1. Storage method Memecache stores all the data in the memory, and it will hang up after a power failure. The data cannot exceed the memory size. Part of Redis is stored on the hard disk, which can ensure the durability of the data

2. Data support type Memcache supports relatively simple data types. Redis has complex data types.

3. The use of the underlying model is different between the underlying implementation methods and the application protocol for communication with the client. Redis directly built the VM mechanism itself, because the general system calls system functions, it will waste a certain amount of time to move and request.
Insert picture description here

6. Is Redis single-process and single-threaded?

Answer: Redis is single-process and single-threaded. Redis uses queue technology to turn concurrent access into serial access, eliminating the overhead of traditional database serial control.

7. What is the maximum storage capacity of a string type value?

Answer: 512M

8. What is the persistence mechanism of Redis? Their advantages and disadvantages?

Redis provides two persistence mechanisms, RDB and AOF mechanisms:

1. RDBRedis DataBase) Persistence mode: refers to the semi-persistent mode using a snapshot of the data set) to record all key-value pairs of the redis database, and write the data to a temporary file at a certain point in time. After the end of persistence, use This temporary file replaces the last persistent file to achieve data recovery.

advantage:

1. There is only one file dump.rdb, which is convenient for persistence.

2. Good disaster tolerance, a file can be saved to a safe disk.

3. To maximize performance, fork the child process to complete the write operation and let the main process continue to process commands, so IO is maximized. Use a separate sub-process for persistence, the main process will not perform any IO operations, to ensure the high performance of redis) 4. Compared with the large data set, the startup efficiency is higher than AOF.

Disadvantages:

1. Low data security. RDB is persisted at intervals. If redis fails between persistence, data loss will occur. So this method is more suitable when the data requirements are not rigorous)

2. AOFAppend-only file) Persistence mode: It means that all command line records are completely persistently stored in the format of redis command request protocol) and saved as an aof file.

advantage:

1. Data security, aof persistence can be configured with appendfsync attribute, there is always, every command operation is recorded in the aof file once.

2. Write files in append mode, even if the server is down in the middle, you can use the redis-check-aof tool to solve the data consistency problem.

3. The rewrite mode of the AOF mechanism. Before the AOF file is rewrite (when the file is too large, the command will be merged and rewritten), some of the commands can be deleted (such as the incorrectly operated flushall))

Disadvantages:

1. AOF file is larger than RDB file, and the recovery speed is slow.

2. When the data set is large, the startup efficiency is lower than that of rdb.

Nine, Redis common performance problems and solutions:

1. It is best not to write memory snapshots for Master. If Master writes memory snapshots, the save command schedules the rdbSave function, which will block the work of the main thread. When the snapshot is relatively large, the performance impact will be very large and the service will be suspended intermittently.

2. If the data is more important, a certain Slave enables AOF to back up the data, and the policy is set to synchronize once per second.

3. For the speed of master-slave replication and the stability of the connection, Master and Slave should preferably be in the same LAN

4. Try to avoid adding slaves to the stressed master library

5. Do not use a graph structure for master-slave replication. It is more stable to use a singly linked list structure, namely: Master <- Slave1

<- Slave2 <- Slave3... This structure is convenient to solve the single point of failure problem and realize the replacement of Slave to Master. If the master hangs up, you can start Slave1 as the master immediately, and the others remain unchanged.
Insert picture description here

10. How to delete redis expired keys?

1. Timing deletion: While setting the key expiration time, create a timer timer). Let the timer execute the key deletion operation immediately when the key expiration time comes.

2. Lazy deletion: Let the key expire regardless, but every time you get a key from the key space, check whether the obtained key expires. If it expires, delete the key; if it does not expire, return the key.

3. Periodic deletion: The program will check the database at regular intervals and delete the expired keys. As for how many expired keys to delete and how many databases to check, it is up to the algorithm.

11. Redis recycling strategy (elimination strategy)

Volatile-lru: Select the least recently used data from the data set (server.db[i].expires) that has set expiration time

volatile-ttl: Select the data to be expired from the data set (server.db[i].expires) that has set expiration time

Volatile-random: arbitrarily select data to be eliminated from the data set that has set expiration time (server.db[i].expires)

allkeys-lru: Select the least recently used data from the data set (server.db[i].dict) to eliminate

allkeys-random: arbitrarily select data from the data set (server.db[i].dict) to eliminate

no-enviction (eviction): prohibit eviction of data

Pay attention to the six mechanisms here. Volatile and allkeys specify whether to eliminate data from a data set with an expiration time set or eliminate data from all data sets. The following lru, ttl, and random are three different elimination strategies, plus one A no-enviction strategy of never recycling.

Use policy rules:

1. If the data presents a power-law distribution, that is, part of the data is accessed frequently, and part of the data is accessed less frequently, then use allkeys-lru

2. If the data is equally distributed, that is, all data access frequencies are the same, use allkeys-random

12. Why does edis need to put all the data in memory?

Answer: In order to achieve the fastest read and write speed, Redis reads the data into the memory and writes the data to the disk asynchronously. So redis has the characteristics of fast and data persistence. If you don't put the data in memory, the disk I/O speed will seriously affect the performance of redis. Today, when memory is getting cheaper, redis will become more and more popular. If the maximum memory usage is set, new values ​​cannot be inserted after the number of existing data records reaches the memory limit.

13. Do you understand the synchronization mechanism of Redis?

Answer: Redis can use master-slave synchronization and slave-slave synchronization. During the first synchronization, the master node performs a bgsave and records subsequent modification operations to the memory buffer at the same time. After completion, the RDB file is fully synchronized to the replication node. After the replication node accepts the completion, the RDB image is loaded into the memory. After the loading is completed, the master node is notified to synchronize the operation record modified during the period to the replication node for replay, and the synchronization process is completed.

14. What are the benefits of Pipeline? Why use pipeline?

Answer: The time of multiple IO round trips can be reduced to one, provided that there is no causal correlation between the instructions executed by the pipeline. When using redis-benchmark for stress testing, it can be found that an important factor that affects the peak QPS of redis is the number of pipeline batch instructions.

15. Have you ever used Redis cluster? What is the principle of cluster?

Redis Sentinal 着眼于高可用, 在 master 宕机时会自动将 slave 提升为master, 继续提供服务。
Redis Cluster 着眼于扩展性, 在单个 redis 内存不足时, 使用 Cluster 进行分片存储。

16. Under what circumstances will the Redis cluster solution cause the entire cluster to be unavailable?

Answer: A cluster with three nodes A, B, and C. Without a replication model, if node B fails, the entire cluster will think that it lacks slots in the range of 5501-11000 and is unavailable.

17. What are the Java clients supported by Redis? Which one is the official recommendation?

Answer: Redisson, Jedis, lettuce, etc., Redisson is officially recommended.

18. What are the advantages and disadvantages of Jedis and Redisson?

Answer: Jedis is a client of Redis's Java implementation. Its API provides a more comprehensive support for Redis commands; Redisson implements a distributed and extensible Java data structure. Compared with Jedis, it has simpler functions and does not support strings. Operation, does not support Redis features such as sorting, transactions, pipes, and partitions. The purpose of Redisson is to promote the separation of concerns from users to Redis, so that users can focus more on processing business logic.
Insert picture description here

19. How to set password and verify password in Redis?

Set password: config set requirepass 123456 Authorization password: auth 123456

Twenty, talk about the concept of Redis hash slot?

Answer: Redis cluster does not use consistent hash, but introduces the concept of hash slot. Redis cluster has 16384 hash slots. After CRC16 check, each key modulates 16384 to determine which slot to place. Each node is responsible for part of the hash slot.

21. What is the master-slave replication model of Redis cluster?

Answer: In order to make the cluster available even when some nodes fail or most of the nodes cannot communicate, the cluster uses a master-slave replication model, and each node will have N-1 replicas.

22. Will there be write operation loss in Redis cluster? why?

Answer: Redis does not guarantee strong data consistency, which means that in practice, the cluster may lose write operations under certain conditions.

23. How are Redis clusters replicated?

Answer: Asynchronous replication

24. What is the maximum number of nodes in a Redis cluster?

Answer: 16,384.

25. How to choose database for Redis cluster?

Answer: Redis cluster is currently unable to select database, the default is 0 database.

26. How to test the connectivity of Redis?

Answer: Use the ping command.

27. How to understand Redis transaction?

Answer:
1. The transaction is a separate isolated operation: all commands in the transaction will be serialized and executed in order. During the execution of the transaction, it will not be interrupted by command requests sent by other clients.
2. The transaction is an atomic operation: the commands in the transaction are either all executed or none of them are executed.

28. What are the commands related to Redis transactions?

答: MULTI、EXEC、DISCARD、WATCH

At last

In order to help everyone better interview and study, here is a complete set of video tutorials for architects and systematic materials on java. From Javase-ssm-springcloud, including java core knowledge points, interview topics and 20 years of the latest Internet There are real questions, e-books, etc., which are very useful for friends and college students who want to learn Java or want to change careers. They are free to share with everyone~ Friends in need can click the link below to get them for free!
Link: Click here! ! ! Password: CSDN

Insert picture description here

Guess you like

Origin blog.csdn.net/XingXing_Java/article/details/108456574