In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the latest version of Redis interview questions in 2021". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn what are the 2021 latest version of the Redis interview questions.
1. What is Redis?
Redis is an open source high performance key-value non-relational cache database written in C language. It supports relatively more value types stored, including string (string), list (linked list), set (collection), zset (sorted set-ordered collection) and hash (hash type). Redis's data is cached, so it is fast, with more than 100000 read and write operations per second, making it the fastest Key-Value DB known. Redis can also write data to disk, ensuring the security of data loss, and the operation of Redis is atomic.
2. The data type of Redis?
Redis has five main data types, including String,List,Set,Zset,Hash, which meets most of the requirements.
3. What are the benefits of using Redis?
(1) it is fast, because the data is stored in memory, and the advantage similar to HashMap,HashMap is that it takes time to find and operate.
The spurs are all very low.
(2) supports rich data types and string,list,set,sorted set,hash
(3) transactions are supported, and all operations are atomic. the so-called atomicity means that changes to the data are either performed or not performed at all.
(4) rich features: can be used for caching, messages, set expiration time by key, and will be deleted automatically after expiration
4. What are the advantages of Redis over Memcached?
1. All values of Memcached are simple strings, and redis, as its replacement, supports richer data classes.
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 memory and will hang up when the power is off. The data cannot exceed the memory size. Part of the Redis is stored on the hard disk, which ensures the persistence of the data
2. Data support type Memcache is relatively simple to support data types. Redis has complex data types.
3. Using the underlying model, the underlying implementation between them and the application protocol for communicating with the client are different. Redis directly built its own VM mechanism, because 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 thread?
Answer: Redis is single-process and single-threaded. Redis uses queue technology to change concurrent access into serial access, which eliminates 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 respective advantages and disadvantages?
Redis provides two persistence mechanisms, RDB (default) and AOF:
RDB: is a Redis DataBase abbreviated snapshot
RDB is the default persistence method for Redis. According to a certain time, the data in memory is saved to the hard disk in the form of a snapshot, and the corresponding data file is dump.rdb. The period of the snapshot is defined by the save parameter in the configuration file.
Advantages:
There is only one file dump.rdb, which is convenient for persistence.
Disaster tolerance is good, a file can be saved to a secure disk.
Performance maximization, the fork child process to complete the write operation, let the main process continue to process commands, so IO is maximized. A single child process is used for persistence, and the main process does not perform any IO operations, which ensures the high performance of redis.
The startup efficiency is higher than that of AOF when the dataset is large.
Disadvantages:
Data security is low. 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 stringent)
AOF (Append-only fifile) persistence mode: means that all command line records are fully persisted in the format of redis command request protocol) saved as aof files.
AOF: persistence:
AOF persistence (that is, Append Only File persistence) records each write command executed by Redis to a separate log file
When Redis is restarted, the files in the persisted log will be restored to data. When both methods are enabled at the same time, the data recovery Redis will give priority to AOF recovery.
Advantages:
Data security, aof persistence can configure the appendfsync attribute, there is always, every command operation is recorded in the aof file.
By writing files in append mode, even if the server goes down, the data-causing problem can be solved through the redis-check-aof tool.
The rewrite mode of the AOF mechanism. Before the AOF file is rewrite (commands will be merged and rewritten when the file is too large), you can delete some of the commands (such as misoperated flflushall).
Disadvantages:
AOF files are larger and slower to recover than RDB files.
When the dataset is large, it is less efficient to start than rdb.
What are the advantages and disadvantages of two kinds of persistence?
AOF files are updated more frequently than RDB, and AOF is preferred to restore data.
AOF is safer and bigger than RDB.
RDB has better performance than AOF.
If both are equipped with priority loading AOF
9. Common performance problems and solutions of Redis:
10. Delete policy for Redis expired keys?
11. Redis recovery strategy (elimination strategy)?
Why does Redis need to put all the data in memory?
Answer: in order to achieve the fastest read and write speed, Redis reads all the data into memory and writes the data to disk asynchronously. So redis has the characteristics of high speed and data persistence. If the data is placed in memory, the speed of the disk IZP O 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 used memory is set, the new value cannot be inserted after the number of records of the data has reached the memory limit.
13. Do you know the synchronization mechanism of Redis?
Answer: Redis can use master-slave synchronization and slave synchronization. In the first synchronization, the master node does a bgsave, and records the subsequent modification operations to the memory buffffer. After the completion, the rdb files are synchronized to the replication node. After the replication node accepts, the rdb image is loaded into memory. After the loading is completed, the synchronization process is completed by informing the master node to synchronize the operation records modified during the period to the replication node for playback.
14. What are the benefits of Pipeline, and why use pipeline?
A: the time for multiple IO round trips can be reduced to one, as long as there is no causal correlation between the instructions executed by pipeline. When using redis-benchmark for pressure testing, it can be found that one of the important factors affecting the QPS peak of redis is the number of pipeline batch instructions.
15. Have you ever used Redis clustering? what is the principle of clustering?
Redis Sentinal, focusing on high availability, will automatically upgrade slave to master in case of master downtime, and continue to provide services.
Redis Cluster focuses on scalability and uses Cluster for sharding storage when a single redis runs out of memory.
16. Under what circumstances will the Redis cluster solution cause the entire cluster to become unavailable?
A: for a cluster with three nodes A, B, and C, if node B fails without a replication model, the whole cluster will think that there is a lack of slots in the range of 5501-11000.
17. What are the Java clients supported by Redis? Which one is officially recommended?
Answer: Redisson, Jedis, lettuce and so on. Redisson is officially recommended.
18. What are the advantages and disadvantages of Jedis and Redisson?
Answer: Jedis is the client of Redis's Java implementation, and its API provides comprehensive support for Redis commands. Redisson implements a distributed and scalable Java data structure. Compared with Jedis, the function is relatively simple, does not support string operation, does not support sorting, transactions, pipes, partitions and other Redis features. The purpose of Redisson is to promote the separation of users' attention from Redis, so that users can focus more on dealing with business logic.
19. How does Redis set and verify passwords?
Set password: confifig set requirepass 123456 authorization password: auth 123456
20. What is 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. Each key uses a module of 16384 after CRC16 verification to decide which slot to place. Each node of the cluster is responsible for a part of the hash slot.
21. What is the master-slave replication model of Redis cluster?
A: 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, and each node will have a copy of Nmuri.
22. Will any writes be lost in the Redis cluster? Why?
A: Redis does not guarantee strong consistency of data, which means that in practice, clusters may lose writes under certain conditions.
23. How are Redis clusters replicated?
Answer: asynchronous replication
24. What is the maximum number of nodes in a Redis cluster?
A: 16384.
25. How does the Redis cluster select the database?
A: currently, Redis cluster cannot make database selection. Default is 0 database.
26. How to test the connectivity of Redis?
Answer: use ping command.
27. How to understand Redis transactions?
A:
1) A transaction is a separate isolation operation: all commands in the transaction are serialized and executed sequentially. In the course of execution, the transaction will not be interrupted by command requests sent by other clients.
2) A transaction is an atomic operation: either all or none of the commands in the transaction are executed.
28. What are the commands related to Redis transactions?
Answer: MULTI, EXEC, DISCARD, WATCH
29. How to set the expiration time and permanent validity of Redis key?
Answer: EXPIRE and PERSIST commands.
30. How does Redis optimize memory?
A: use a hash table (hashes) as much as possible. A hash table (that is, a small amount of memory stored in a hash table) 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, last name, mailbox, and password. Instead, store all the user's information in a hash table.
31. How does the Redis recycling process work?
A: a client runs a new command and adds new data. Redi checks the memory usage, and if it is greater than the limit of maxmemory, it will be reclaimed according to the set policy. A new order is executed, and so on. So we constantly cross the boundary of the memory limit, by constantly reaching the boundary and then constantly recycling back below the boundary. If the result of a command results in a large amount of memory being used (for example, the intersection of large sets is saved to a new key), it will not be long before the memory limit is exceeded by this memory usage.
32. What are the ways to reduce the memory usage of Redis?
A: if you are using 32-bit Redis instances, you can make good use of collection type data such as Hash,list,sorted set,set, because usually many small Key-Value can be stored together in a more compact way.
33. What happens when Redis runs out of memory?
A: if the set limit is reached, Redis's write command will return an error message (but the read command can return normally. Or you can use Redis as a cache to use the configuration elimination mechanism, which will wash out the old content when the Redis reaches the memory upper limit.
34. How much keys can be stored in a Redis instance? List, Set, Sorted Set, how many elements can they store at most?
A: theoretically, Redis can handle up to 232 keys, and it has been tested in practice, with at least 250 million keys stored in each instance. We are testing some larger values. Any list, set, and sorted set can put 232 elements. In other words, the storage limit of Redis is the available memory values in the system.
35. There is 2000w data in MySQL and only 20w data in redis. How to ensure that the data in redis are all hot data?
A: when the Redis in-memory dataset size 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 elimination from a dataset (server.dbi.expires) with an expiration time set
Volatile-ttl: select the expired data from the dataset (server.dbi.expires) that has been set for expiration
Volatile-random: select data elimination from any dataset with an expiration time set (server.dbi.expires)
Allkeys-lru: select the least used data from the data set (server.dbi.dict) to be eliminated
Allkeys-random: data elimination from any selection of data sets (server.dbi.dict)
No-enviction (expulsion): prohibition of eviction data
36. What is the most suitable scene for Redis?
1. Session caching (Session Cache)
One of the most common scenarios where Redis is used is session caching (session cache). The advantage of caching sessions with Redis over other stores, such as Memcached, is that Redis provides persistence. When maintaining a cache that is not strictly consistent, most people will be unhappy if all the users' shopping cart information is lost. Will they still do so now? Fortunately, as Redis has improved over the years, it's easy to find out how to properly use Redis to cache session documents. Even the well-known business platform Magento provides plug-ins for Redis.
2. Full-page cache (FPC)
In addition to the basic session token, Redis provides a very simple FPC platform. Back to consistency, even if the Redis instance is restarted, users will not see a drop in page loading speed because of disk persistence, which is a great improvement, similar to PHP native FPC. Again, take Magento as an example. Magento provides a plug-in to use Redis as the full-page cache backend. In addition, for WordPress users, Pantheon has a very good plug-in wp-redis, which can help you load the pages you have visited as quickly as possible.
3. Queue
One of the advantages of Reids in the field of memory storage engines is that it provides list and set operations, which makes Redis a good message queuing platform to use. The operation used by Redis as a queue is similar to the push/pop operation of list by a native program language such as Python. If you quickly search for "Redis queues" in Google, you will immediately find a large number of open source projects designed to use Redis to create very good back-end tools to meet a variety of queue needs. For example, Celery has a background that uses Redis as broker, which you can check from here.
4, ranking / counter
Redis does a great job of incrementing or decrementing numbers in memory. Set and Sorted Set also make it easy for us to perform these operations, and Redis just happens to provide these two data structures. So, we need to get the top 10 users from the sorted set-we call it "user_scores", and we just need to do something like this: of course, this assumes that you are sorting incrementally based on your user's score. If you want to return users and their scores, you need to do this: ZRANGEuser_scores 010 WITHSCORES Agora Games is a good example, implemented in Ruby, and its ranking uses Redis to store data, as you can see here.
5. Publish / subscribe
Last (but certainly not least) is Redis's publish / subscribe capabilities. There are indeed a lot of publish / subscribe usage scenarios. I've seen people use it in social networking connections, act as publish / subscribe based scripting triggers, and even use Redis's publish / subscribe feature to set up chat systems!
37. Suppose there are 100 million key in the Redis, of which 10w key start with a fixed known prefix. What if all of them are found?
38. If there are a large number of key that need to expire at the same time, what do you need to pay attention to?
A: if a large number of key expiration times are set too centrally, redis may experience temporary stutters at that point of expiration. Generally, it is necessary to add a random value to the time so that the expiration time is scattered.
39. Have you ever used Redis as an asynchronous queue? how do you use it?
40. Have you ever used a Redis distributed lock? what is it?
41. How to implement session shared storage in the cluster?
42. What is the difference between memcached and redis?
43. What command is used to view Redis usage and status information?
Info
44. What happens when Redis runs out of memory?
If the upper limit is reached, Redis's write command returns an error message (but the read command returns normally. ) or you can use Redis as a cache to use the configuration elimination mechanism, which will wash out the old content when the Redis reaches the upper limit of memory.
45. Redis is single-threaded, how to improve the utilization of multicore CPU?
You can deploy multiple Redis instances on the same server and use them as different servers. At some point, one server is not enough anyway, so if you want to use multiple CPU, you can consider shard.
46. How many keys can be stored in a Redis instance? List, Set, Sorted Set, how many elements can they store at most?
47. Redis common performance problems and solutions?
(1) Master is best not to do any persistence work, such as RDB memory snapshots and AOF log files.
(2) if the data is important, a Slave enables AOF to back up data, and the policy is set to synchronize once per second.
(3) for the speed of master-slave replication and the stability of connection, Master and Slave should be in the same local area network.
(4) try to avoid adding slave libraries to the stressed master libraries.
(5) Master-slave replication does not use graphic structure, but one-way linked list structure is more stable, that is, Master
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.