Summary of Redis related interview questions

1. What is Redis?

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

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

Redis supports data persistence. It can save the data in the memory to the disk and load it again for use when restarting.
Redis not only supports simple key-value type data, but also provides storage of data structures such as list, set, zset, and hash.
Redis supports data backup, that is, data backup in master-slave mode.

1.2 Redis advantages

Extremely high performance – Redis can read at 110,000 times/s and write at 81,000 times/s.
Rich data types – Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type operations for binary cases.
Atomic – All operations in Redis are atomic, meaning they are either executed successfully or not executed at all. Individual operations are atomic. Multiple operations also support transactions, that is, atomicity, wrapped by the MULTI and EXEC instructions.
Rich features – Redis also supports publish/subscribe, notifications, key expiration and other features.

1.3 How is Redis different from other key-value stores?

Redis has more complex data structures and provides atomic operations on them. This is an evolutionary path different from other databases. Redis's data types are based on basic data structures and are transparent to programmers, without the need for additional abstractions.

Redis runs in memory but can be persisted to disk, so there is a memory trade-off when performing high-speed reading and writing of different data sets, because the amount of data cannot be larger than the hardware memory. Another advantage of in-memory databases is that compared to the same complex data structures on disk, operating in memory is very simple, so Redis can do a lot of things with strong internal complexity. Also, in terms of disk format they are compact append-generated since they do not require random access.

2. What are the data types of Redis?

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

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

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

3. What are the benefits of using Redis?

1. It is fast 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. Supports rich data types , supports string, list, set, Zset, hash, etc.
3. Supports transactions and operations are atomic. The so-called atomicity means that all changes to the data are either executed or not executed at all< /span>
4. Rich features: can be used for caching, messages, set expiration time by key, and will be automatically deleted after expiration

4. What are the advantages of Redis compared to Memcached?

1. All values ​​in Memcached are simple strings. As its replacement, redis supports richer data types
2. Redis is much faster than Memcached< /span>
3. Redis can persist its data

5. What are the differences between Memcache and Redis?

1. Storage method Memecache stores all data in the memory. It will hang up after power failure. The data cannot exceed the memory size. Redis is partially stored on the hard disk, which ensures data persistence.
2. Data support types Memcache supports relatively simple data types. Redis has complex data types.
3. The underlying models used are different. The underlying implementation methods and application protocols for communication with the client are different. Redis directly builds its own VM mechanism, because if the general system calls system functions, it will waste a certain amount of time to move and request.

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

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

7. What is the maximum capacity that a string type value can be stored in?

Answer: 512M

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

Redis provides two persistence mechanisms RDB and AOF mechanisms:

8.1 RDB

RDBRedis DataBase) persistence mode: refers to the use of data set snapshots (semi-persistent mode) 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 persistence is completed, use this temporary The file replaces the last persisted file to achieve data recovery.

Advantages
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, and the main process will not perform any IO operations, ensuring the high performance of redis) 4. When the data set is large, 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. Therefore, this method is more suitable when the data requirements are not strict)
2. AOFAppend-only file) persistence method: means that all command line records are completely persisted in the format of the redis command request protocol Storage) is saved as an aof file.

8.2 AOF

Advantages
1. Data security, aof persistence can be configured with the appendfsync attribute, with always, each command operation will be recorded to the aof file.
2. Write files through append mode. Even if the server goes 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 rewritten (commands will be merged and rewritten when the file is too large), you can delete some of the commands (such as flushall by mistake))

Disadvantages
1. AOF files are larger than RDB files and the recovery speed is slow.
2. When the data set is large, the startup efficiency is lower than rdb.

9. Redis common performance problems and solutions

1. Master is best not to write memory snapshots. 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 impact on performance will be very large and it will be intermittent. Suspend the service
2. If the data is important, a Slave enables AOF backup data, and the policy is set to synchronize one time per second
3. For the speed of master-slave replication and the stability of the connection, it is best for Master and Slave to be on the same LAN
4. Try to avoid adding slaves to the master database that is under great pressure
5. Master Do not use a graph structure for slave replication. It is more stable to use a one-way linked list structure, that is: Master <- Slave1<- Slave2 <- Slave3... This structure is convenient to solve the problem of single point failure and realize the replacement of Master by Slave. If the Master hangs up, you can immediately enable Slave1 as the Master, leaving everything else unchanged.

10. What is the deletion strategy for redis expired keys?

1. Scheduled deletion: While setting the expiration time of the key, create a timer (timer). Let the timer immediately perform the deletion of the key when the expiration time of the key comes.
2. Lazy deletion: Let the key expire, but every time you get the key from the key space, check whether the obtained key has expired. If it has expired, delete the key; if it has not expired, , the key is returned.
3. Periodic deletion: Every once in a while, the program checks the database and deletes the expired keys in it. It's up to the algorithm to decide how many expired keys to delete and how many databases to check.

11. Redis recycling strategy (elimination strategy)?

volatile-lru: Select the least recently used data from the data set (server.db[i].expires) with expiration time set to eliminate
volatile-ttl: Select the data to be expired from the data set (server.db[i].expires) that has set expiration time
volatile-random: Randomly select data for elimination from the data set (server.db[i].expires) that has set expiration time
allkeys-lru a>: Select the least recently used data from the data set (server.db[i].dict) to eliminate
allkeys-random : Arbitrarily select data elimination from the data set (server.db[i].dict)
no-enviction (eviction): prohibit eviction of data

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

Use policy rules:
1. If the data shows a power law distribution, that is, some data have high access frequency and some data have low access frequency, 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 data in memory?

Answer: In order to achieve the fastest reading and writing speed, Redis reads all the data into the memory and writes the data to the disk asynchronously. So redis has the characteristics of fast speed and data persistence. If the data is not placed in memory, disk I/O speed will seriously affect the performance of redis. Today, when memory is getting cheaper and cheaper, redis will become more and more popular. If the maximum memory used 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 primary node performs a bgsave and records subsequent modification operations to the memory buffer. After completion, the entire rdb file will be synchronized to the replica node. After the replica node accepts the data, it will load the rdb image into the memory. After the loading is completed, the master node is notified to synchronize the operation records modified during the period to the replica 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 there is no causal correlation between instructions executed by the pipeline. When using redis-benchmark for stress testing, it can be found that an important factor affecting the QPS peak value of redis is the number of pipeline batch instructions.

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

1), Redis Sentinal focuses on high availability. When the master goes down, it will automatically promote the slave to the master and continue to provide services.
2). Redis Cluster focuses on scalability. When a single redis memory is insufficient, Cluster is used for shard storage.

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

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

17. What Java clients are supported by Redis? Which one is officially recommended?

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 implemented by Redis in Java, and its API provides relatively comprehensive support for Redis commands; Redisson implements a distributed and scalable Java data structure, and its functions are simpler than Jedis. , does not support string operations, does not support Redis features such as sorting, transactions, pipelines, and partitions.
The purpose of Redisson is to promote the separation of users' concerns from Redis, so that users can focus more on processing business logic.

19. Tell us about the concept of Redis hash slot?

Answer: Redis cluster does not use consistent hashing, but introduces the concept of hash slots. Redis cluster has 16384 hash slots. Each key is checked by CRC16 and modulo 16384 is used to determine which slot to place. Each key in the cluster Each node is responsible for a part of the hash slot.

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

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

21. How does Redis perform memory optimization?

Answer: Use hashes as much as possible. A hash table (that is, the number stored in a hash table is small) uses very small memory, so you should abstract your data model into a hash table as much as possible. For example, if there is a user object in your web system, do not set a separate key for the user's name, surname, email, and password. Instead, store all the user's information in a hash table.

22. What are some ways to reduce the memory usage of Redis?

Answer: If you are using a 32-bit Redis instance, you can make good use of collection type data such as Hash, list, sorted set, set, etc., because usually many small Key-Values ​​can be stored together in a more compact way.

23. What happens when Redis runs out of memory?

Answer: If the set upper limit is reached, the Redis write command will return an error message (but the read command can still return normally.) Or you can use Redis as a cache to use the configuration elimination mechanism. When Redis reaches the memory upper limit, the old ones will be flushed. content.

24. If there are 100 million keys in Redis, and 100,000 of them start with a fixed, known prefix, how to find them all?

Answer: Use the keys command to scan out the key list of the specified mode.

The other party then asked: If this redis is providing services to online businesses, what are the problems with using the keys command?

At this time you have to answer one of the key features of redis: redis is single-threaded. The keys instruction will cause the thread to block for a period of time, and the online service will pause. The service cannot be restored until the instruction is executed. At this time, you can use the scan command. The scan command can extract the key list of the specified mode without blocking, but there will be a certain probability of duplication. Just do it once on the client, but the overall time spent will be longer than using it directly. keys command length.

25. If there are a large number of keys that need to be set to expire at the same time, you generally need to pay attention.

What?
Answer: If the expiration time of a large number of keys is set too concentratedly, redis may experience a brief lag at the time of expiration. Generally, it is necessary to add a random value to the time to make the expiration time more spread out

I have also prepared complete interview materials for 2023 for students~
How to get it: Follow the official account below and reply: Interview

Insert image description here

The article is continuously updated. You can follow the official account below or search "Last Rosemary" on WeChat and reply "Interview" to obtain complete interview information.

Guess you like

Origin blog.csdn.net/qq_38374397/article/details/134952973
Recommended