Redis cache: kill interviewer 25 asked

Micro Technology 2021-10-14 07:57:25
redis cache kill interviewer


Hello everyone , I am a Tom Brother ~


In order to facilitate everyone to find the problem , Know the whole picture , Organize a directory , We can get a quick overview of Redis cache , What questions do interviewers usually like to ask ?


 picture


Next , Let's look at each question and answer one by one


Redis What are the characteristics ?

answer :

  • High performance , The speed of reading is zero 100000 Time /s, The speed of writing is 80000 Time /s
  • Data persistence , Support RDB 、AOF
  • Support transactions . adopt MULTI and EXEC Package the instructions .
  • Multiple data structure types
  • Master slave copy
  • Other features : Release / subscribe 、 notice 、key Overdue, etc


Redis Why so soon? ?

answer :

  • Completely based on memory , No disk IO Overhead on , Except asynchronous persistence
  • Single thread , Avoid the performance loss of multiple thread switching
  • Non blocking IO Multiplexing mechanism
  • Optimization of underlying data storage structure , Use native data structures to improve performance .


Redis What are the underlying basic data structures ?

answer :

  • character string . There is no C The traditional string of language , But a simple dynamic string implemented by yourself SDS Abstract type of , And save the length information .
  • Linked list (linkedlist). Bidirectional acyclic linked list structure , Each node of a linked list consists of a listNode Structure to express , Each node has pointers to pre - and post nodes
  • Dictionaries (hashtable). An abstract data structure that holds key value pairs , Bottom use hash surface , Each dictionary has two hash surface , For normal use and rehash When using .
  • Skip list (skiplist). A jump table is one of the underlying implementations of an ordered set .redis Jump watch by zskiplist and zskiplistNode form ,zskiplist Used to save jump table Information ( Header 、 Tail node 、⻓ Degree, etc ),zskiplistNode Used to represent a table hop node , The story height of each jump table is 1- 32 The random number , In the same jump table , Multiple nodes can contain the same score , But the member object of each node must be unique , The nodes are sorted according to the score size , If the scores are the same , The members are sorted according to the size of the objects .
  • Set of integers (intset). Set abstract data structure used to hold integer values , There will be no repeating elements , The bottom layer is implemented as an array .
  • Compressed list (ziplist). Sequential data structures developed to save memory , Can contain multiple nodes , Each node can hold a byte array or integer value .


Redis What data types are supported ?

answer : Five common data types :StringHashSetListSortedSet. Three special data types :BitmapHyperLogLogGeospatial, among Bitmap 、HyperLogLog The bottom floor is String data type ,Geospatial The bottom is Sorted Set data type .

  • String object string:int Integers 、embstr Encoded simple dynamic string 、raw Simple dynamic string
  • List objects list:ziplist、linkedlist
  • Hash object hash:ziplist、hashtable
  • A collection of objects set:intset、hashtable
  • An orderly collection of objects zset:ziplist、skiplist


Redis frequently-used 5 Data structures and application scenarios ?

answer :

  • String: cache 、 Counter 、 Distributed locks, etc
  • List: Linked list 、 queue 、 Weibo follows people's timeline list, etc
  • Hash: User information 、Hash Table, etc
  • Set: duplicate removal 、 Fabulous 、 Step on 、 Common friends, etc
  • Zset: Traffic charts 、 Hits, charts, etc


Why single threaded ?

answer : Official reply ,CPU Will not be Redis The bottleneck ,Redis Mainly affected by memory 、 Network restrictions . for example , In an ordinary Linux On the system , Use pipelining Can deliver... Per second 100 Million requests , So if your application mainly uses O(N) or O(log(N)) command , You will hardly use too much CPU, Belong to IO Intensive systems .


Redis 6.0 After that, we switched to multithreading ?

answer :Redis Multithreading is mainly used to process data reading and writing 、 Protocol analysis . Execute commands in a single threaded sequence .

Mainly because redis The performance bottleneck is the network IO Instead of CPU, Use multithreading for some peripheral preprocessing , Promoted IO Reading and writing efficiency of , Thus, the overall throughput is improved .antirez stay RedisConf 2019 Mentioned during sharing ,Redis 6 The introduction of multithreading IO At least double the performance .


Expiration key Key What are the deletion strategies for ?

answer : Yes 3 This is an expired deletion policy . Lazy deletion 、 Delete periodically 、 Delete regularly

  • Lazy deletion . Use key Check only when , If it has expired , Delete . shortcoming : overdue key If not accessed , Can't delete , Memory hogging , Waste of space .
  • Delete periodically . Check every once in a while , Delete expired key, Just take some at random each time key Go check .
  • Delete regularly . For each key Set expiration time , Also create a timer . Once due , Delete now . shortcoming : If there are many expired keys , Occupy CPU More , It has a great impact on the performance of the service .


If Redis Not enough memory space , Elimination mechanism ?

answer :

  • volatile-lru: From the set expiration time of key in , Remove the least recently used key To eliminate
  • allkeys-lru: When the memory is not enough to hold the newly written data , In key space , Remove the least recently used key( This is the most commonly used )
  • volatile-ttl: From the set expiration time of key in , Move out of... Which is about to expire key
  • volatile-random: From the set expiration time of key in , Random selection key Eliminate
  • allkeys-random: from key Choose... At random key To eliminate
  • no-eviction: Ban data obsolescence . When memory reaches the threshold , New write error
  • volatile-lfu: From the set of data for which the expiration time has been set (server.db[i].expires) Select the least frequently used data to eliminate (LFU(Least Frequently Used) Algorithm , That is to say, the most frequently accessed data is most likely to be accessed in the future )
  • allkeys-lfu: When the memory is not enough to hold the newly written data , In key space , Remove the least frequently used key.


Redis How to solve the problem of suddenly hanging up ?

answer :1、 Think from the perspective of system availability ,Redis Cluster Introduce the active / standby mechanism , When the master node is down , Automatically switch to standby node , Continued provision of services .2、Client Introduce local cache on the client side , By switching , avoid Redis All of a sudden , High concurrent traffic hangs the database .


Redis What are the ways of persistence ?

answer :

1、 snapshot RDB. Save the database state at a certain point in time to RDB file in ,RDB File is a compressed binary , Save on disk . When Redis Crash Time , Can be used to recover data . adopt SAVE or BGSAVE To generate RDB file .

  • SAVE: It will block redis process , until RDB File creation complete , During process blocking ,redis Can't process any command requests .
  • BGSAVE: Meeting fork Make a sub process , Then the child process is responsible for generating RDB file , The parent process can also continue to process command requests , Does not block the process .

2、 Just append files AOF. Log every write operation ( Non read operation ). When different nodes synchronize data , Read the contents of the log file and execute the write instruction from front to back , Data recovery can be completed .


Redis Common scenarios

answer :

  • 1、 cache , There's a good saying ,「 Not enough performance , Cache to gather 」
  • 2、 Distributed lock , utilize Redis Of setnx
  • 3、 Distributed session
  • 4、 Counter , adopt incr command
  • 5、 Ranking List ,Redis Of Ordered set
  • 6、 other


Redis Seven classic problems to pay attention to cache ?

answer : List 100 million level systems , In case of high traffic Redis What problems might cache encounter ? And the corresponding solution .

  • 1、 Cache set failure
  • 2、 Cache penetration
  • 3、 Cache avalanche
  • 4、 Cache hotspot
  • 5、 Cache size Key
  • 6、 Cache data consistency
  • 7、 Data concurrency competition heats up

Detailed solutions to each problem , Please check out   A billion level system Redis How the cache is designed ???


Redis What are the cluster schemes ?

answer :

  • Master slave replication mode
  • Sentinel( sentry ) Pattern
  • Redis Cluster Pattern


Redis Master slave data synchronization ( Master slave copy ) The process of ?

answer :

  • 1、slave After starting , towards master send out sync command
  • 2、master received sync after , perform bgsave Save snapshots , Generate RDB Full file
  • 3、master hold slave Write commands to the cache
  • 4、bgsave After execution , send out RDB File to slave,slave perform
  • 5、master Send buffer write command to slave,slave Receive commands and execute , Complete replication initialization .
  • 6、 thereafter ,master Every time a write command is executed, it will be sent to slave, keep master And slave Data consistency between


Advantages and disadvantages of master-slave replication ?

answer :

1、 advantage :

  • master It can automatically synchronize data to slave, Can be read-write separation , Share responsibility master Reading pressure
  • master、slave The synchronization between is performed in a non blocking manner , Synchronization period , Clients can still submit queries or update requests

shortcoming :

  • No automatic fault tolerance and recovery ,master After node downtime , You need to manually specify a new master
  • master Downtime , If the data is not synchronized before the outage , Then switch IP There will be data inconsistency after
  • It is difficult to support online capacity expansion ,Redis The capacity of is limited by a single machine configuration


Sentinel( sentry ) The advantages and disadvantages of the model ?

answer : Sentinel mode is based on master-slave replication mode , Added Sentinels to monitor And Automatically handle faults .

1、 advantage :

  • Sentinel mode is based on master-slave replication mode , There are advantages of copying from master to master , Sentinel mode also has
  • master If you hang up, you can switch automatically , Higher system availability

2、 shortcoming :

  • Redis The capacity of is limited by a single machine configuration
  • Additional resources are needed to start sentinel process


Redis Cluster The advantages and disadvantages of the model ?

answer : Realized Redis Distributed storage , Each node stores different content , To solve the problem of online expansion .

1、 advantage :

  • No central Architecture , Data according to slot Distributed over multiple nodes
  • Every node in the cluster is equal , Each node holds its own data and the state of the entire cluster . Each node is connected to all other nodes , And those connections stay active , This ensures that we only need to connect to any node in the cluster , You can get data from other nodes .
  • It can be linearly extended to 1000 Multiple nodes , Nodes can be added or removed dynamically
  • The ability to achieve automatic failover , Nodes pass through gossip agreement Exchange status information , By voting slave To master Role transformation of

shortcoming :

  • Data is replicated asynchronously , Strong consistency of data is not guaranteed
  • slave act as “ Cold standby ”, No external reading is provided 、 Writing service , For failover purposes only .
  • Batch operation limits , Only the same... Is currently supported slot It's worth it key Perform bulk operations , Yes mset、mget、sunion Operation support is not friendly
  • key Limited transaction operation support , Only support more key Transaction operations at the same node , many key The transaction function cannot be used when distributed in different nodes
  • Multiple database spaces are not supported , a redis Can support 16 individual db, Only one can be used in cluster mode , namely db 0 .Redis Cluster Mode is not recommended pipeline and multi-keys operation , Reduce max redirect The resulting scene .


Redis How to expand capacity ?

answer : In order to avoid data migration failure , Usually use Consistent Hashing Realize dynamic capacity expansion and reduction , Effectively reduce the need for migration Key Number .

however Cluster Pattern , Use fixed Slot Slot mode (16384 individual ), For each key Calculation CRC16 value , Then on 16384 modulus , And then according to slot Value to find the target machine , Add capacity , We only need to migrate part of slot Go to the new node .


Redis The principle of clustering ?

answer : One redis A cluster consists of multiple nodes node form , The multiple node Through between cluster meet Command to connect , Form a cluster .

Data is stored in pieces , The whole cluster is divided into 16384 individual slot, Each node is responsible for some slots . The information of the whole slot will be synchronized to all nodes .

key And slot The mapping relation of :

  • The health value is right key, Conduct CRC16 Calculation , Work out a 16 bit Value
  • take 16 bit The value of is right 16384 modulus , obtain 0 ~ 16383 The number of represents key The corresponding hashico


Redis How to be highly available ?

answer : Sentinel mechanism . With automatic fail over 、 Cluster monitoring 、 Message notification and other functions .

Sentinels can monitor all masters at the same time 、 From the server , When a master When offline , Automatically lift the corresponding slave by master, Then by the new master External services .


What is? Redis Business ?

answer :Redis A transaction is a collection of commands , Pack multiple commands , Then add these commands to the queue in order , And execute these commands in order .

Redis There is nothing like Mysql The concept of transaction isolation level in relational database , Atomic operation cannot be guaranteed , There is no image. Mysql In that case, if the transaction fails, the rollback operation will be performed


Redis Transaction execution process ?

answer : adopt MULTIEXECWATCH Wait for the command to implement the transaction mechanism , The transaction execution process executes a series of commands at one time in sequence , During execution , The transaction will not be interrupted , It doesn't perform other requests from the client , Until all the commands are executed .

The specific process :

  • The server receives a client request , Business to MULTI Start
  • If you are in a transactional state , Then the subsequent commands will be put in the queue and returned to the client QUEUED , On the contrary, execute this... Directly An order
  • When received from client EXEC On command , Will take the command out of the queue 、 Sequential execution , After execution, change the current state from transaction state to non transaction state
  • If you receive DISCARD command , Give up executing the command in the queue , It can be understood as Mysql Rollback operation for , And change the current state from transaction state to non transaction state

WATCH Monitoring certain key, This command can only be used in MULTI Execute before order . If you watch key Modified by other clients ,EXEC All commands in the execution queue will be discarded .UNWATCH Pass... Before canceling monitoring WATCH Command surveillance key. Through execution EXEC 、DISCARD Two commands were monitored before key Will also be removed from surveillance .


Redis And Guava 、Caffeine What's the difference? ?

answer : Cache is divided into local cache and distributed cache .

1、Caffeine、Guava, Belongs to the local cache , characteristic :

  • Direct access to memory , Fast , Limited by memory , Unable to store big data .
  • No network communication overhead , Higher performance .
  • Only local application process access is supported , The cost of synchronously updating the local cache data of all nodes is high .
  • Application process restart , Data will be lost .

therefore , Local cache is suitable for storing some high hotspot data that is not easy to change or low-frequency change .

2、Redis It belongs to distributed cache , characteristic :

  • Cluster pattern , Support mass data storage
  • Centralized data storage , Ensure data consistency
  • Data is transmitted across the network , Performance is lower than local caching . But the same computer room , A round-trip request between two servers also requires 500 Microsecond , Compared with its advantages , This loss is completely negligible , This is also why distributed caching is popular .
  • Support replica mechanism , Effectively ensure high availability .


How to implement a distributed lock ?

answer :

  • 1、 Database table , Poor performance
  • 2、 Use Lua Script ( contain SETNX + EXPIRE Two instructions )
  • 3、SET The extended command for (SET key value [EX][PX] [NX|XX])
  • 4、Redlock frame
  • 5、Zookeeper Curator The framework provides ready-made distributed locks




About me : Former Ali P7 technician , I've published patents , I won a prize in the competition ,CSDN Blogger , Responsible for e-commerce transactions 、 Community fresh 、 Internet Finance and other businesses , Years of team management experience .

Official account 「 Micro Technology 」, The background to reply “ Algorithm ” , Get information for free


 picture


Recommended reading

MYSQL That shit ! Indexes 、SQL tuning 、 Business 、B+ Trees 、 table ....

TCP What's wrong with the Internet ! Three handshakes 、 Four waves 、TIME-WAIT ....

Interviewer asked : How to ensure MQ The message is orderly ?

What is a bloon filter ? How to solve the problem of high concurrency cache penetration ?

Learn this 10 A design principle , One step closer to the architect !!!

版权声明
本文为[Micro Technology]所创,转载请带上原文链接,感谢
https://javamana.com/2021/10/20211002145702007N.html

  1. L'apprentissage le plus détaillé de springboot sur le Web - day08
  2. Introduction à la page Web de rabbitmq (3)
  3. No Converter found for return value of type: class java.util.arraylist Error Problem
  4. (16) , spring cloud stream message driven
  5. Que faut - il apprendre de l'architecture des microservices Spring Cloud?
  6. Résolution: erreur: Java: distribution cible invalide: 11problème d'erreur
  7. Springboot démarre en une minute et sort de l'enfer de la configuration SSM!
  8. Maven - un outil de gestion essentiel pour les grands projets d'usine, de l'introduction à la maîtrise![️ Collection recommandée]
  9. ️ Push to interview in Large Factory ᥧ - - Spring Boot Automatic Assembly Principle
  10. [️ springboot Template Engine] - thymeleaf
  11. Springboot - MVC Automatic configuration Principle
  12. Mybatis reverse engineering and the use of new version mybatisplus 3.4 reverse engineering
  13. Base de données MySQL - transactions et index
  14. Sécurité du printemps - [authentification, autorisation, déconnexion et contrôle des droits]
  15. Moteur de base de données InnoDB diffère de myisam
  16. Swagger - [springboot Integrated Swagger, configure Swagger, configure scan Interface, configure API Group]
  17. Cadre de sécurité Shiro - [QUICKstart, login Block, User Authentication, request Authorization]
  18. [Introduction à Java] installation de l'environnement de développement - Introduction à Java et construction de l'environnement
  19. 【 linux】 notes d'utilisation tmux
  20. MySQL + mybatis paging query - database series learning notes
  21. Usage relations and differences of count (1), count (*) and count (a field) in MySQL
  22. 2021 Ali Java advanced interview questions sharing, Java Architect interview materials
  23. Mybatis - dynamic SQL statement - if usage - MySQL series learning notes
  24. [go to Dachang series] deeply understand the use of where 1 = 1 in MySQL
  25. [secret room escape game theme ranking list] Based on spring MVC + Spring + mybatis
  26. Redis log: the killer mace of fearless downtime and rapid recovery
  27. 5 minutes to build redis cluster mode and sentinel mode with docker
  28. Java小白入门200例106之遍历ArrayList的几种方式
  29. Java小白入门200例105之Java ArrayList类
  30. Java小白入门200例104之JDK自带记录日志类logging
  31. Practice of high availability architecture of Tongcheng travel network based on rocketmq
  32. Chapter 9 - Linux learning will - file archiving and compression tar --- zip
  33. Java小白入門200例104之JDK自帶記錄日志類logging
  34. JDK avec journalisation de classe dans 200 cas 104
  35. Java ArrayList Class for Introduction to Java LITTLE WHITE 200 example 105
  36. Plusieurs façons de traverser ArrayList à partir de 200 exemples 106
  37. Provectus / Kafka UI: open source Apache Kafka's Web GUI Graphical interface management tool
  38. Design pattern series: Singleton pattern
  39. Java小白入門200例105之Java ArrayList類
  40. Understanding Java record types
  41. Five load balancing algorithms implemented in Java
  42. Data structure must be an example to understand dynamic programming (with universal Python code)
  43. The idea and implementation of recursion in data structure (Python)
  44. The idea and implementation of linked list (Python)
  45. Data structure must be queue and double ended queue (Python)
  46. Idea and implementation of data structure must be able stack (Python)
  47. Data structure | time complexity (with video explanation)
  48. 20 flutter libraries you should know
  49. Case sharing: Online failure caused by Dubbo 2.7.12 bug
  50. Open source | didi open source, general functional components for Java authentication, authentication, management and task scheduling
  51. Flutter multi engine supports platformview and thread merging solution
  52. In depth understanding of netty: viewing netty traffic control from occasional downtime
  53. Spring AOP internal skill cultivation
  54. Interviewer: is Tomcat a symbolic parent delegation mechanism?
  55. Expérimentez la première tablette de consommation Linux. La puce et le système d'origine sont tous faits maison
  56. 2021 summary of the latest Java common open source libraries, Java interview handwritten code
  57. 2021 latest Java factory interview true questions, Kafka introduction video
  58. 01 javase - première connaissance de l'installation de Java et de l'environnement de développement
  59. The sales volume in September broke the record: Weilai and Xiaopeng both exceeded 10000, with an ideal month on month decrease of 24.7%
  60. Redis core principle and practice: implementation principle of hash type and dictionary structure