In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
9 common "difficult" questions in Redis interview
What are the data structures of 1 Redis?
String String, dictionary Hash, list List, collection Set, ordered collection SortedSet.
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've played Redis Module, like BloomFilter,RedisSearch,Redis-ML, the interviewer's eyes start to shine.
2 have you ever used a Redis distributed lock? what is it?
First use setnx to scramble for the lock, and then use expire to add an expiration time to the lock to prevent the lock from forgetting to release.
At this point, the other person will tell you that you have a good answer, and then ask what happens if the process accidentally crash or restarts maintenance before expire is executed after setnx.
At this point you should give surprising feedback: Oh, yes, this lock will never be released. Then you need to scratch your head, pretend to think for a moment, as if the next result is your own initiative, and then answer: I remember that the set instruction has very complex parameters, which should be able to combine setnx and expire into one instruction at the same time! At this time, the other party will show a smile, the heart began to say: press, this boy is not bad.
3 if 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?
Use the keys directive to scan out the key list for the specified mode.
The other party then asked: if this redis is providing services to online business, what is the problem with using the keys instruction?
At this point you have to answer one of the key features of redis: the single-threaded nature of redis. The keys instruction will cause the thread to block for a period of time, and the online service will stop until the instruction has been executed before the service can resume. At this time, you can use the scan instruction. The scan instruction can extract the key list of the specified mode without blocking, but there will be a certain probability of repetition. It can be deduplicated on the client side, but the overall time will be longer than using the keys instruction directly.
4 have you ever used Redis as an asynchronous queue? how do you use it?
Generally, list structure is used as a queue, rpush produces messages, and lpop consumes messages. When there is no message from lpop, sleep appropriately and try again later.
If the other party asks whether it is possible not to use sleep? List also has an instruction called blpop, which blocks until the message arrives when there is no message.
What if the other party asks if it can be produced and consumed more than once? Using the pub/sub topic subscriber mode, you can implement message queuing for 1VR N.
If the other party asks what are the shortcomings of pub/sub? In the case of consumers offline, the produced messages will be lost, and professional message queues such as rabbitmq will have to be used.
If the other party asks redis how to implement the delay queue? I guess now you really want to beat the interviewer to death. If you have a baseball bat in your hand, why do you ask in such detail? But you are restrained, and then reply calmly: use sortedset, take the timestamp as score, the message content as key to call zadd to produce the message, and the consumer uses the zrangebyscore instruction to get the 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 the moment you have your middle finger up, behind the chair.
5 if there are a large number of key that need to expire at the same time, what do you generally need to pay attention to?
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.
6 how does Redis persist?
Bgsave persists the image in full, and aof does incremental persistence. Because bgsave will take a long time, not real-time, and lead to a lot of data loss during downtime, aof is needed to cooperate with it. When the redis instance is restarted, aof is preferred to restore the state of memory, and if there is no aof log, the rdb file is used to restore it.
What if the aof file is too large and the recovery time is too long? You tell the interviewer that Redis will do aof rewrites on a regular basis to compress the aof file log size. If the interviewer is not satisfied, and then come up with the killer answer, Redis4.0 has the function of mixed persistence, which combines the total amount of bgsave and the increment of aof, which ensures both the efficiency of recovery and the security of data. This feature is not even known to many interviewers, and they are sure to be impressed by you.
If the other party asks, what happens if the machine suddenly loses power? Depending on the configuration of the aof log sync property, if performance is not required, sync the disk as each instruction is written, and no data will be lost. However, it is not realistic to sync every time under the requirement of high performance. Timing sync is generally used, such as 1s1 times. At this time, a maximum of 1 second of data will be lost.
What are the benefits of 7 Pipeline, and why use pipeline?
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.
Do you know the synchronization mechanism of 8 Redis?
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 buffer. 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.
9. Have you ever used Redis clustering? what is the principle of clustering?
Redis Sentinal focuses on high availability and automatically promotes slave to master in case of master downtime and continues to provide services.
Redis Cluster focuses on scalability and uses Cluster for sharding storage when a single redis runs out of memory.
Redis Cluster principle
Redis Cluster is the cluster implementation of Redis, with built-in automatic data slicing mechanism. All key in the cluster is mapped to 16384 Slot, and each Redis Instance in the cluster is responsible for reading and writing a part of the Slot. The cluster client can send commands when connecting to any Redis Instance in the cluster. When the Redis Instance receives a request from a Slot that it is not responsible for, it will return the Redis Instance address of the Slot in charge of the request Key to the client. After receiving it, the client will automatically resend the original request to this address, which is transparent to the outside world. Which Slot a Key belongs to is determined by crc16 (key)% 16384.
With regard to load balancing, data can be migrated between the Redis Instance of the cluster, in units of Slot, but not automatically, and needs to be triggered by external commands.
With regard to the management of cluster members, the node (Redis Instance) of the cluster and the nodes in the cluster regularly exchange and update the node information in the cluster. From the point of view of the sending node, the information includes: which nodes are in the cluster, what is IP and PORT, what is the name of the node, what is the status of the node (such as OK,PFAIL,FAIL, detailed later), including node roles (master or slave), and so on.
With regard to availability, the cluster consists of N-group master-slave Redis Instance. The master can have no slave, but no slave means that the Slot read and write service that the master is responsible for is not available after the master downtime. A master can have multiple slaves. When a master goes down, a slave will be promoted as a master, and which slave will be promoted as a master. The protocol is similar to Raft, see here. How to detect master downtime? Redis Cluster adopts the mechanism of quorum+ heartbeat. From the point of view of the node, if the node regularly sends Ping,cluster-node-timeout (configurable, seconds) to all other nodes and does not receive a reply from the other side, it unilaterally considers the opposite end node to be down and marks the node as PFAIL. Collected through the exchange of information between nodes, quorum nodes think that this node is PFAIL, then mark the node as FAIL and send it to all other nodes, and all other nodes immediately think that the node is down. It can be seen here that after the master downtime, at least the read and write service of the Slot that the master is responsible for will not be available for cluster-node-timeout time.
Article source: highly available architecture
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.