编程知识 cdmana.com

Redis cache: kill interviewer 25 asked

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://cdmana.com/2021/10/20211002145702007N.html

Scroll to Top