40 Redis operation and maintenance interview questions

In order to be a good helper for everyone on the way to the interview, for students who have no idea about Redis, we have compiled 40 common Redis interview questions, so that you don’t panic in the interview and strive for Offer to get soft hands!

1. What is Redis?

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

Compared with other key-value cache products, Redis has the following three characteristics:

  • Redis supports data persistence, which can save the data in memory to the disk, and can be loaded 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.

Advantages of Redis:

  • Extremely high performance: Redis can read at a speed of 110,000 times/s, and write at a speed of 81,000 times/s.

  • Rich data types: Redis supports Strings, Lists, Hashes, Sets, and Ordered Sets data types in binary cases.

  • Atomic: All operations of Redis are atomic, which means that they are either executed successfully or not executed at all if they fail. Individual operations are atomic. Multiple operations also support transactions, that is, atomicity, wrapped 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. The data types of Redis 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 the hardware memory. Another advantage of in-memory databases is that it is very simple to operate in memory compared to the same complex data structures on disk, so that Redis can do many things with high internal complexity. Also, they are compact in terms of on-disk format and are append-generated, since they do not require random access.

2. What is the data type of Redis?

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

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

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

3. What are the benefits of using Redis?

  • The speed 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)

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

  • Transactions are supported, and operations are atomic. The so-called atomicity means that all changes to data are either executed or not executed at all.

  • Rich features, can be used for caching, messages, set expiration time by key, it will be deleted automatically after expiration

4. What advantages does Redis have over Memcached?

  • All Memcached values ​​are simple strings, and Redis, as its replacement, supports richer data classes

  • Redis is much faster than Memcached

  • Redis can persist its data

5. What are the differences between Memcache and Redis?

  • Storage method Memecache stores all the data in the memory, and it will hang up after power failure, and the data cannot exceed the size of the memory. Part of Redis is stored on the hard disk, which ensures data persistence.

  • Data Support Types Memcache supports relatively simple data types. Redis has complex data types.

  • Different underlying models are used, and the underlying implementation methods and application protocols for communicating with clients are different. Redis directly builds the VM mechanism by itself, 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?

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

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

512M。

8. What is the persistence mechanism of Redis? Pros and cons of each?

Redis provides two persistence mechanisms RDB and AOF mechanisms:

RDB (Redis DataBase) persistence method: refers to the semi-persistent mode of recording all key-value pairs of the Redis database in the form of a snapshot of the dataset, and writes the data to a temporary file at a certain point in time. After the persistence is over, use this The temporary file replaces the last persistent file to achieve data recovery.

advantage:

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

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

  • To maximize performance, fork the child process to complete the write operation, and let the main process continue to process commands, so the IO is maximized. Use a separate child process for persistence, and the main process will not perform any IO operations, ensuring the high performance of Redis.

  • When the data set is large, it is more efficient than AOF.

Disadvantages: low data security. RDB is persisted at intervals. If Redis fails during persistence, data loss will occur. So this method is more suitable when the data requirements are not rigorous

AOF (Append-only file) persistence mode: It means that all command line records are fully persistently stored and saved as aof files in the format of the Redis command request protocol.

advantage:

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

  • Write files through the 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.

  • 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), some of the commands can be deleted (such as the misoperated flushall)

shortcoming:

  • AOF files are larger than RDB files and are slower to restore.

  • When the data set is large, it is less efficient than RDB startup.

9. Redis common performance problems and solutions

  • It is best for the master not to write a memory snapshot. If the master writes a memory snapshot, the save command will schedule the rdbSave function, which will block the work of the main thread. When the snapshot is relatively large, it will have a great impact on performance, and the service will be suspended intermittently.

  • If the data is more important, a Slave turns on AOF backup data, and the policy is set to synchronize one per second.

  • For the speed of master-slave replication and the stability of the connection, it is better for Master and Slave to be in the same local area network.

  • Try to avoid adding slaves to the master library under high pressure.

  • Do not use a graph structure for master-slave replication. It is more stable to use a one-way 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 immediately enable Slave1 to be the Master, and the others remain unchanged.

10. Redis expired key deletion strategy?

  • Timed deletion: Create a timer timer while setting the expiration time of the key. Let the timer delete the key immediately when the key's expiration time comes.

  • Lazy deletion: Let the key expire regardless, but every time you get a key from the key space, check whether the obtained key is expired, if it is expired, delete the key; if it is not expired, return the key.

  • Periodic deletion: The program checks the database every once in a while and deletes the expired keys in it. How many expired keys to remove, and how many databases to check, 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) with an expiration time set to eliminate

  • volatile-ttl: Select the data that will expire from the data set (server.db[i].expires) with the expiration time set to be eliminated

  • volatile-random: Randomly select data elimination from the data set (server.db[i].expires) with an expiration time set

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

  • allkeys-random: Randomly select data elimination from the dataset (server.db[i].dict)

  • no-enviction (eviction): Disable data eviction

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

Use policy rules:

  • If the data presents a power-law distribution, that is, some data access frequency is high, and some data access frequency is low, use allkeys-lru

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

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

In order to achieve the fastest read and write speed, Redis reads data into memory and writes data to disk asynchronously. So Redis has the characteristics of fast and data persistence. If the data is not kept in memory, the disk I/O speed is seriously affecting 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?

Redis can use master-slave synchronization and slave-slave synchronization. During the first synchronization, the master node performs a bgsave, and records the subsequent modification operations to the memory buffer at the same time. After the completion, the rdb file is fully synchronized to the replication node, and the replication node loads the rdb image into the memory after receiving it. After the loading is complete, notify the master node to synchronize the modified operation records to the copy node for replay to complete the synchronization process.

14. What are the benefits of Pipeline, why use Pipeline?

It is possible to reduce the time of multiple IO round trips to one, provided that there is no causal correlation between the instructions executed by the Pipeline. When using redis-benchmark for pressure 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 a Redis cluster? What is the principle of the cluster?

Redis Sentinal focuses on high availability. When the Master is down, it will automatically promote the slave to the master and continue to provide services.

Redis Cluster focuses on scalability, and uses Cluster for shard storage when a single Redis memory is insufficient.

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

In a cluster with three nodes A, B, and C, if there is no 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 officially recommended?

Redisson, Jedis, lettuce, etc. are officially recommended to use Redisson.

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

Jedis is the client of Redis implemented in Java, and its API provides comprehensive support for Redis commands; Redisson implements a distributed and scalable Java data structure. Compared with Jedis, it has simpler functions and does not support string operations. Redis features such as sorting, transactions, pipelines, and partitions are not supported.

The purpose of Redisson is to promote the separation of users' concerns about Redis, so that users can focus more on processing business logic.

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

Set password: config set requirepass 123456

Authorization password: auth 123456

20. Tell me about the concept of Redis hash slot?

The Redis cluster does not use consistent hash, but introduces the concept of hash slots. The Redis cluster has 16384 hash slots. After each key passes the CRC16 check, it takes the modulus of 16384 to determine which slot to place. Each node of the cluster Responsible for some hash slots.

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

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

22. Will the write operation be lost in the Redis cluster? Why?

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

23. How are Redis clusters replicated?

Asynchronous replication.

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

16384.

25. How does the Redis cluster choose a database?

Currently, the Redis cluster cannot select the database, and the default is 0 database.

26. How to test the connectivity of Redis?

Use the ping command.

27. How to understand Redis transactions?

A transaction is a single, isolated operation: all commands within a transaction are serialized and executed sequentially. During the execution of the transaction, it will not be interrupted by command requests sent by other clients.

A transaction is an atomic operation: either all of the commands in the transaction are executed, or none of them are executed.

28. What are the commands related to Redis transactions?

MULTI、EXEC、DISCARD、WATCH。

29. How to set the expiration time and permanent validity of Redis key?

EXPIRE and PERSIST commands.

30. How does Redis optimize memory?

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

31. How does the Redis recycling process work?

A client runs new commands and adds new data. Redis checks the memory usage, if it is greater than the limit of maxmemory, it will be recycled according to the set strategy. A new command is executed, etc. So we are constantly crossing the boundary of the memory limit, by constantly reaching the boundary and then continuously recycling back below the boundary. If the result of a command results in a large amount of memory being used (such as saving the intersection of very large collections to a new key), it won't take long for the memory limit to be exceeded by this memory usage.

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

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.

33. What happens when Redis runs out of memory?

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 limit, it will flush out old content.

34. How many keys can a Redis instance store at most? List, Set, Sorted Set, how many elements can they store at most?

Theoretically, Redis can handle up to 232 keys, and it has been tested in practice, and each instance stores at least 250 million keys. We are testing some larger values. Any list, set, and sorted set can hold 232 elements. In other words, the storage limit of Redis is the amount of memory available in the system.

35. There are 20 million data in MySQL, but only 20 million data in Redis. How to ensure that the data in Redis are all hot data?

When the size of the Redis memory data set rises to a certain size, the data elimination strategy will be implemented.

Related knowledge: Redis provides 6 data elimination strategies:

  • volatile-lru: Select the least recently used data from the data set (server.db[i].expires) with an expiration time set to eliminate

  • volatile-ttl: Select the data that will expire from the data set (server.db[i].expires) with the expiration time set to be eliminated

  • volatile-random: Randomly select data elimination from the data set (server.db[i].expires) with an expiration time set

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

  • allkeys-random: Randomly select data elimination from the dataset (server.db[i].dict)

  • no-enviction (eviction): Disable data eviction

36. What is the most suitable scenario for Redis?

Session cache (Session Cache), the most commonly used scenario for using Redis is session cache (session cache). The advantage of using Redis to cache sessions over other storage (such as Memcached) is that Redis provides persistence. When maintaining a cache that is not strictly consistent, most people would be unhappy if all of the user's shopping cart information was lost. Now, would they? Fortunately, as Redis has improved over the years, it's easy to find documentation on how to properly use Redis to cache sessions. Even the well-known business platform Magento offers a plugin for Redis.

Full page cache (FPC), in addition to the basic session token, Redis also provides a very simple FPC platform. Back to the issue of consistency, even if the Redis instance is restarted, users will not see a drop in page loading speed due to disk persistence. This is a great improvement, similar to PHP's local FPC. Taking Magento as an example again, Magento provides a plugin to use Redis as a full page cache backend. In addition, for WordPress users, Pantheon has a very good plugin wp-redis, which can help you load the pages you have visited as fast as possible.

Queue, one of the advantages of Reids in the field of memory storage engine is to provide list and set operations, which makes Redis can be used as a good message queuing platform. The operation that Redis uses as a queue is similar to the push/pop operation of a local programming language (such as Python) on a list. If you do a quick Google search for "Redis queues" you'll immediately find a ton of open source projects that aim to leverage Redis to create really nice backend tools for a variety of queuing needs. For example, Celery has a background that uses Redis as a broker, you can check it from here.

Leaderboards/Counters, Redis does a great job of incrementing or decrementing numbers in memory. Sets and Sorted Sets also make it very simple for us to perform these operations, and Redis just provides these two data structures. So, we want to get the top 10 users from the sorted set - let's call it "user_scores", we just need to execute as follows: Of course, this assumes that you are based on your user's score Do an ascending sort. If you want to return the user and the user's score, you need to execute it like this: ZRANGE user_scores 0 10 WITHSCORES Agora Games is a good example, implemented in Ruby, and its leaderboard uses Redis to store data, you can find it here See.

Publish/Subscribe, and last (but certainly not least) is Redis' pub/sub functionality. There are indeed many use cases for publish/subscribe. I've seen people use it in social networking connections, as triggers for pub/sub based scripts, and even use Redis' pub/sub functionality to build chat systems!

37. Suppose there are 100 million keys in Redis, among which 10w keys start with a fixed and known prefix, how to find them all?

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 for online businesses, what will be the problem with using the keys command?

At this time, you have to answer a key feature of Redis: Redis's single thread. The keys instruction will cause the thread to block for a period of time, and the online service will be suspended until the instruction is executed before the service can be resumed. At this time, the scan command can be used. The scan command can extract the key list of the specified mode without blocking, but there will be a certain probability of repetition. It is enough to do a deduplication on the client side, but the overall time spent will be longer than the direct use keys Command length.

38. If there are a large number of keys that need to be set to expire at the same time, what should be paid attention to?

If the expiration time of a large number of keys is set too intensively, Redis may experience short-term lag when it expires. Generally, it is necessary to add a random value to the time to make the expiration time more dispersed.

39. Have you ever used Redis as an asynchronous queue? How did you use it?

Answer: Generally, the list structure is used as the queue, rpush produces messages, and lpop consumes messages. When there is no message from lpop, please sleep for a while and try again. If the other party asks if you can use sleep? list also has an instruction called blpop, when there is no message, it will block until the message arrives. If the other party asks if it can be produced once and consumed multiple times? Using the pub/sub topic subscriber pattern, a 1:N message queue can be implemented.

If the other party asks what are the disadvantages of pub/sub?

When the consumer goes offline, the produced messages will be lost, and a professional message queue such as RabbitMQ must be used.

If the other party asks how Redis implements the delay queue?

I guess now you really want to beat the interviewer to death. If you have a baseball bat in your hand, how can you ask such detailed questions. But you are very restrained, and then replied calmly: Use sortedset, use timestamp as score, message content as key, call zadd to produce messages, and consumers use zrangebyscore command to obtain data polling before N seconds for processing.

At this point, the interviewer has secretly given you a thumbs up. But what he doesn't know is that at this moment you put up your middle finger, behind the chair.

40. Have you ever used Redis distributed locks? What is it about?

First use setnx to compete for the lock, and then use expire to add an expiration time to the lock to prevent the lock from being forgotten to be released.

At this time, the other party will tell you that your answer is good, and then ask what happens if the process crashes unexpectedly after setnx is executed before expire or needs to be restarted for maintenance? At this time, you have to give surprising feedback: Oh, yes, this lock will never be released. Then you need to scratch your head, pretend to think for a while, as if the next result is your own initiative, and then answer: I remember that the set command has very complicated parameters, this should be able to set the setnx and expire at the same time Synthesize a command to use!

Guess you like

Origin blog.csdn.net/LinkSLA/article/details/130258397