Java interview - beating and hanging interviewer series Redis basis

 

The more you know, the more you do not know

Thumbs up again, a habit

Foreword

Redis such a broad use of Internet technology in storage, almost all of the back-end technology interviewer should make things difficult for small partners to carry out 360 ° in the use of terms and principles of Redis. As a first Internet companies to take on a plane surface Pa offer of ( 请允许我使用一下夸张的修辞手法), beat numerous competitors, each time only to see the countless lonely figure left disappointed, somewhat guilty, at night a lonely days, I learn from their mistakes , decided to start to write 吊打面试官系列, want to help you readers like gangbusters after the interview, the interviewer counterattack of 360 °, beating and hanging ask your interviewer, beating and hanging interviews with colleagues ( 好像不太好), crazy harvesting manufacturers offer!

Beginning of the interview

A paunchy, middle-aged man wearing a plaid shirt, holding a full scratch mac come to you, looking at the fast balding hair, thinking it is certainly Nima's top architects! But we abdominal poetry and gas from China, not virtual virtual.

 

 

 

Hello boys, see you write your resume on the project which used Redis, why you use Redis?

My heart could not help but cursed, Jiaosha this problem, we are not used to this thing, but you can not say it.

Seriously replied: Hello handsome charming interviewer, because traditional relational database such as Mysql has been unable to apply all of the scenes, such as spike inventory deductions, APP Home traffic peaks, etc., are very easy to hit the database collapse, so the introduction of caching middleware, currently on the market commonly used caching middleware has Redis and Memcached but in consideration of their advantages and disadvantages and, finally chose Redis.

至于更细节的对比朋友们记得查阅Redis 和 Memcached 的区别,比如两者的优缺点对比和各自的场景,后续我有时间也会写出来。

The young man, I ask you, what data structures Redis, you know?

String String, Dictionary Hash, List List, a collection of Set, an ordered set of SortedSet.

这里我相信99%的读者都能回答上来Redis的5个基本数据类型。如果回答不出来的小伙伴我们就要加油补课哟,大家知道五种类型最适合的场景更好。

However, if you are Redis advanced user, and you want to highlight different you and other candidates in the interview, also need to add several data structures HyperLogLog below, Geo, Pub / Sub.

If you want extra points, you say also played Redis Module, like BloomFilter, RedisSearch, Redis-ML, was this time the interviewer eyes began to glisten, I thought this guy a little thing ah.

注:本人在面试回答到Redis相关的问题的时候,经常提到BloomFilter(布隆过滤器)这玩意的使用场景是真的多,而且用起来是真的香,原理也好理解,看一下文章就可以在面试官面前侃侃而谈了,不香么?下方传送门 ↓

Avoid BloomFilter of weapon caches breakdown

If a large number of key needs to be set at the same time has expired, the general needs attention?

If a large number of key expiration time set too concentrated, at that point in time expired, redis may momentarily Caton phenomenon. The worst case, there will be an avalanche cache, we generally need to add a random value over time, so that disperse some of the expiration time.

电商首页经常会使用定时任务刷新缓存,可能大量的数据失效时间都十分集中,如果失效时间一样,又刚好在失效的时间点大量用户涌入,就有可能造成缓存雪崩

Then you used Redis distributed lock it, which is what's going on?

Setnx scramble to acquire a lock, then grabbed, and then add a lock to expire expiration prevent lock forget released.

This time the other will tell you, you answer quite well, and then went on to ask if accidental crash before execution expire after setnx process or to restart maintained, then what happens?

This time you have to give feedback surprise: Well, is oh, this lock will never be released. Then you need to 抓一抓自己得脑袋,故作思考片刻, as if the next result is that you take the initiative to think out, then answered: I remember a very complex instruction set of parameters, this should be able to setnx and expire at the same time a synthesis instruction to use!

Then the other side will reveal a smile, and my heart began to recite: ah, this kid is pretty good, I started a bit mean.

If there are 100 million Redis key, which has a key 10w begins with a fixed known prefix, how will they all find out?

Use keysinstructions may sweep out a list of the specified pattern key.

Then ask each other: If this redis is providing services to the business line, use keys that instruction will be a problem?

This time you have to answer a key characteristic redis: redis single-threaded. keys instruction causes the thread blocked for some time, online services will be stalled until the instruction is finished, the service can be restored. This time can be used scaninstructions, scaninstruction can extract the key non-blocking list specified pattern, but there will be some duplication of probability, on the client side deduplication can be done once, but the overall time it takes to command than the direct use of keys long.

不过,增量式迭代命令也不是没有缺点的: 举个例子, 使用 SMEMBERS 命令可以返回集合键当前包含的所有元素, 但是对于 SCAN 这类增量式迭代命令来说, 因为在对键进行增量式迭代的过程中, 键可能会被修改, 所以增量式迭代命令只能对被返回的元素提供有限的保证 。

Used Redis what to do asynchronous queue, how do you use?

It is generally used as a queue list structure, rpushproduction information, lpopconsumer information. Lpop when no message to a proper sleep will try again.

If the person asking it Can not sleep?

There is a list of instructions called blpop, in the absence of information, it will block until the message arrives.

If the person then asked many times a consumer can not produce it?

Use pub / sub topic subscriber mode, you can achieve the 1: N message queue.

If you continue to ask pub / sub What are the disadvantages?

In the case of the consumer off the assembly line, production will be lost messages, message queues have to use professional as RocketMQ and so on.

What happens if the other party asked redis achieve the ultimate TM delay queue?

This set even move down, I guess now you want to put on a stick and killed the interviewer ( 面试官自己都想打死自己了怎么问了这么多自己都不知道的), if you have got a baseball bat, then, but you are restrained. Calm down about the excitement of the heart and calm demeanor replied: Use sortedset, take a timestamp as the score, the message content as key to call zadd to produce news, consumer access to polling data N seconds before treated with zrangebyscore instruction.

到这里,面试官暗地里已经对你竖起了大拇指。并且已经默默给了你A+,但是他不知道的是此刻你却竖起了中指,在椅子背后。

Redis is how persistent? Service Master Data from how to interact?

RDB to mirror the full amount of persistence, AOF do incremental persistence. Because RDB will take a long time, not real time, when a lot of downtime can result in loss of data, so it is necessary to use with AOF. When redis instance restart, use RDB persistent file to reconstruct the memory, then use the AOF replay with recent operating instructions to cause a state of complete recovery before restarting.

这里很好理解,把RDB理解为一整个表全量的数据,AOF理解为每次操作的日志就好了,服务器重启的时候先把表的数据全部搞进去,但是他可能不完整,你再回放一下日志,数据不就完整了嘛。不过Redis本身的机制是 AOF持久化开启且存在AOF文件时,优先加载AOF文件;AOF关闭或者AOF文件不存在时,加载RDB文件;加载AOF/RDB文件城后,Redis启动成功; AOF/RDB文件存在错误时,Redis启动失败并打印错误信息

The other asked that if a sudden power failure the machine will happen?

Depending on the configuration AOF log sync attributes, if performance is not required, at the time of each disk write instruction sync it, they will not lose data. But in the high performance requirements of every sync it is not realistic, generally using a timed sync, such as 1s1 times, this time it will lose most of the data 1s.

RDB ask each other what is the principle?

You are given two words on it, fork and cow. fork means redis to RDB by creating child processes, cow means that the copy is created on write, the child, the parent and child share the data segment, the parent process continues to provide literacy services to write dirty page data will gradually and sub-process separation off.

注:回答这个问题的时候,如果你还能说出AOF和RDB的优缺点,我觉得我是面试官在这个问题上我会给你点赞(暗示点赞),两者其实区别还是很大的,而且涉及到Redis集群的数据同步问题等等。想了解的伙伴也可以留言,我会专门写一篇来介绍的。

Pipeline What are the benefits, why should the pipeline?

Multiple IO round-trip time can be reduced to one, provided that no causal correlation between the instruction execution pipeline. Redis-benchmark used for pressure measurement can be found when a peak value of QPS redis important factor is the number of batch instruction pipeline.

Redis synchronization mechanism to understand it?

Redis master-slave synchronization may be used, from the synchronization. The first synchronization, the master node do a bgsave, while recording subsequent modification operations to the memory buffer, after completion of RDB be synchronized to the total amount of file copy node, after the completion of copying RDB node accepts image loaded into memory. After loading is complete, and then notify the master node during operation of the modified copy to the node record synchronization reproduction synchronization process is completed. Subsequent incremental data synchronization can AOF through the log, somewhat similar binlog database.

Whether used Redis cluster, a cluster of how to ensure high availability, clustering principle is what?

Redis Sentinal focus on high availability, downtime in the master slave automatically promoted to master, continue to provide services.

Cluster redis focus on scalability, while a single redis insufficient memory, memory fragmentation using Cluster.

End of the interview

Boy you can, and when there is time to go to work, ah, or else come tomorrow?

You put a calm, so I need to rent Jia, to no less than Monday it.

NB so good I thought this kid is not a lot Offer on the hand, I have to call hr not give him money.

Sustain to the end, you could not help but give yourself one point praised ( 暗示该点赞了).

to sum up

在技术面试的时候,不管是Redis还是什么问题,如果你能举出实际的例子,或者是直接说自己开发过程的问题和收获会给面试官的印象分会加很多,回答逻辑性也要强一点,不要东一点西一点,容易把自己都绕晕的。

Another point is that I ask you why you do not use Redis one up directly answer questions, you can reply:

Hello handsome interviewer, first our project DB encountered a bottleneck, this scene in particular hot spot data DB spike and basically could not carry, it would need to add caching middleware, and currently on the market some cache middleware has Redis and Memcached, their strengths and weaknesses ......, and then combined with our combination of these characteristics of the project, who in the end we chose the technology selection time.

If you do this in an orderly, well-founded answer to my question but uttered so many problems outside my knowledge, I feel that you're not just a person would write the code, you clear logic, your technology selection, right right project has its own understanding and thinking middleware, it means you are Me and offer.

Well, that's the entire contents of this article, I will continue to update Java later interview easing the bit series and Java technology stack related articles. If you want to know what, you can also leave a message after the thumbs up to me, I will have time to write, we progress together.

Thank you very much to see here, if this is not bad at writing, then   your support and recognition, is the biggest driving force of my work, OK you see our next issue!求点赞 求关注 求分享 求留言


Back will be updated weekly "Java suspended or beaten interview series" Follow my first time to the public No. Family reading, what problems can be directly micro-channel bit me, I was a rookie, but does not affect our progress.

 

Ao propane | Wen

Guess you like

Origin www.cnblogs.com/aobing/p/11791039.html