当前位置:网站首页>IDS interview questions collection data structure + penetration avalanche + persistence + memory elimination strategy + database double write + Sentinel
IDS interview questions collection data structure + penetration avalanche + persistence + memory elimination strategy + database double write + Sentinel
2022-06-22 04:18:00 【Dull wooden chicken】
problem 1 You are right about redis What do you know about ?
In a nutshell redis Just a database. The difference is redsi The data of is stored in memory , So the reading and writing speed is very fast, so ,redis It is widely used in caching , in addition redis Also used for distributed locks ,redis A variety of data types are provided to support different business scenarios . besides redis Support transaction persistence ,lua Script ,lru Event cluster
problem 2 Why use redis To do caching
We mainly look at the problem from two aspects: high performance and high concurrency .
High performance : When users access some data in the database at one time, the process will be very slow because it is read from the hard disk , Put the user data in the cache so that you can directly operate the cache the next time you access it , So it's very fast , If the database changes, synchronize the change cache
Small extensions involve cache consistency

High concurrency : The direct operation cache can handle more requests than the user can access the database , So we consider transferring all the data in the database to the cache , In this way, some user requests will go directly to the cache instead of directly accessing the database 
problem 3 Why use redis To do it instead of map/guava
Cache is divided into local cache and distributed cache , With Java For example, use the self-contained map perhaps guava The cache will follow jvm The destruction of , And in the case of multiple instances . Each instance should save a cache of the child , Cache has inconsistencies
Use redis perhaps memcached It's called distributed caching , In the case of multiple instances , Each instance shares a cache data with cache consistency . But to be specific, we need to keep redis,mencached It is more complex in the whole program architecture
problem 4 Redis What is the thread model of ?
redis Internal use of file event handler file event handler, This file event handler is single threaded , therefore redis It's called a single threaded model . It uses IO Multiplexing mechanism listens to multiple channels at the same time socket, according to socket Select the corresponding event handler to handle the event on .
The structure of the file event handler contains 4 Parts of :
- Multiple socket
- IO Multiplexing program
- File event dispatcher
- Event handler ( Connect to answer processor 、 Command request processing . device 、 Command reply processor )
Multiple socket Different operations may occur concurrently , Each operation corresponds to a different file event , however IO Multiplexers listen to multiple socket, Will socket The generated events are queued , The event dispatcher takes an event from the queue one at a time , Give the event to the corresponding event handler for processing .
problem 5 Why? redis Single thread model can also be so efficient ?
Based on memory operation , Using non blocking IO Multiplexing mechanism . Single thread avoids the problem of multi-threaded context switching
problem 6 redis and memcached The difference between
redis Support complex data structures redis comparison memcached Come on , More data structures , Can support richer data operation . Caching can support more complex structures and operations if needed , redis Would be a good choice .
redis The native cluster mode is supported in redis3.x In the version , To support cluster Pattern , and memcached No native cluster mode , Need to rely on the client to write data to the cluster in pieces .
Performance comparison due to redis Single core only , and memcached Can use multi-core , So on average redis When storing small data memcached Higher performance . And in the 100k In the above data ,memcached Better performance redis, although redis Recently, it also optimizes the performance of storing big data , But rather than memcached, It's still a little worse .

problem 7redis What types of data are there ( Common commands can be omitted )
Srting
Common commands set get decr incr mget
String The data structure is simple key-vaule Type of ,value Not only can string It can also be numbers
Hash
Common commands :hget,hset,hgetall
hash It's a String fiedl and value Mapping table ,hash It is especially suitable for storing objects for subsequent operations. I can modify only one value in the object , Used to store user information, etc

List
Common commands :lpush,rpush,lpop,rpop,larange
redisList It's a list redis list There are a lot of common words that can be used to make the attention list and word segmentation list Message list, etc
set
set The functions provided to the outside world and list The type of , The special feature is that the weight can be automatically discharged
Common commands :sadd,spop,smembers,sunion etc.
sintersotore key1 key2 key3
SortedSet
Common commands :zadd,zrange,zrem,zcard etc.
and set comparison sortedSet Added a weight parameter score
Yes, the set can be arranged orderly
Example : The facts in the live broadcast system are arranged in the order of gifts
problem 8 redis Data structure of ?
String: Represents a variable byte data. We can get the string length by initializing the string content string String , You can overwrite the contents of the string , The string can be appended
List:redis Name the data structure list instead of arraylist , Because the list storage structure uses a linked list instead of an array and is a two-line scale , Because it is a linked list, the random location performance is weak , The first insert and delete performance is better ,
hash:hash Equivalent to java Of hashmap , In the implementation structure, she uses two-dimensional array , First bit array , The second linked list ,hash The content of key value The array in the chat table stores the linked list key Look for the element Calculation hashcode And then use hashcode The module is located at the header of the linked list to obtain the corresponding value of the linked list's warp row
set:java Programmers know that hashset The internal implementation uses hashmap It's just all value All point to the same redis The use of hash The structure is the same
socre: yes redis A very special data structure provided , On the one hand, it is equivalent to the data structure map<String,double> You can give each element value Give one
Weight, on the other hand, is similar to treeSet The internal elements are weighted score Sort
problem 9 redis Persistence mechanism ( How to guarantee redis After hanging up, restart the data bank )
AOF :aof Each execution of a persistent session will change redis The database command writes the command to... On disk aof file , Moved the configuration file appendonly.aof To configure the
RDB: Persistence refers to writing the data set snapshot in memory to disk within a specified time interval , The actual operation process is fork A subprocess , Write the dataset to the temporary file first , After writing successfully , Replace the previous file , Compress storage with binary .
problem 10 Redis What are the data elimination strategies ?
stay Redis in , Allows the user to set the maximum memory size to use server.maxmemory, When Redis When the size of the memory data set rises to a certain size , Data elimination strategies will be implemented .
1.volatile-lru: Select the least recently used obsolescence from the data set that has been set to expire
2.volatile-ttr: Select the data that will expire from the data set that has been set to expire
3.volatile-random: Select any data elimination from the data set that has expired
4.allkeys-lru: Select the least recently used data from the data set to eliminate
5.allkeys-random: Select any data from the data set to eliminate
6.noenviction: Ban data obsolescence
redis Data will also be synchronized to aof
problem 11 RDB What are the advantages ?
1). Once adopted , So your whole Redis The database will contain only one file , This is perfect for file backups . such as , You may want to file every hour recently 24 Hours of data , At the same time, it needs to be archived once a day. Recently 30 Days of data . Through such a backup strategy , In the event of a catastrophic system failure , We can recover very easily .
2). For disaster recovery ,RDB It's a very good choice . Because we can easily compress a single file and then transfer it to other storage media .
3). Maximize performance . about Redis In terms of service process , At the beginning of persistence , The only thing it needs to do is fork Out of child process , After that, the subprocess completes these persistent tasks , This can greatly avoid the execution of service process IO Operation .
4). Compared with AOF Mechanism , If the data set is large ,RDB More efficient start-up .
RDB What are the disadvantages ?
1). If you want to ensure high availability of data , That is to avoid data loss as much as possible , that RDB Will not be a good choice . Because once the system goes down before timing persistence , Data not written to disk before will be lost .
2). because RDB It's through fork Sub processes to assist in data persistence , therefore , If the dataset is large , May cause the entire server to stop serving for hundreds of milliseconds , Even 1 Second .
problem 12 RDB What are the advantages ?
1). This mechanism can bring higher data security , Data persistence .Redis Provided in 3 Medium synchronization policy , Synchronization per second 、 Sync and out of sync per change . in fact , Synchronization per second is also done asynchronously , It's also very efficient , What's worse is that once the system goes down , Then the modified data will be lost in one second . And every modification is synchronized , We can think of it as synchronous persistence , That is, every data change will be recorded in the disk immediately . Predictably, , This is the least efficient way . As for asynchrony , Needless to say , I think everyone can understand it correctly .
2). Because this mechanism uses append Pattern , So even if there is downtime in the writing process , It will not destroy the existing content in the log file . However, if we only write half of the data in this operation, the system crashes , Never mind , stay Redis Before next startup , We can go through redis-check-aof Tools to help us solve data consistency problems .
3). If the log is too large ,Redis Can be enabled automatically rewrite Mechanism . namely Redis With append Mode continuously writes the modified data to the old disk file , meanwhile Redis A new file will also be created to record which modification commands were executed during this period . So it's going on rewrite Data security can be better guaranteed when switching .
4). AOF Include a clear format 、 Easy to understand log file is used to record all modification operations . in fact , We can also reconstruct the data through this file .
AOF What are the disadvantages of ?
1). For the same number of data sets ,AOF Files are usually larger than RDB file .RDB Speed ratio when recovering large data sets AOF It's faster to recover .
2). According to different synchronization strategies ,AOF The operation efficiency is often slower than RDB. All in all , The efficiency of synchronization policy per second is relatively high , Efficiency and RDB As efficient as .
Selection criteria , The system is willing to sacrifice some performance , In exchange for higher cache consistency (aof), Still willing to write frequently , Do not enable backup for better performance , To be operated manually save When , Backup again (rdb).rdb This one is even more eventually consistent I'm not sure .
problem 13 What is cache penetration and what is cache avalanche
Cache avalanche : Cache large area invalidation within the same event , So subsequent requests fall on the data , The database is difficult to bear a large number of requests and crashes in a short time
Cache penetration : Simply put, there are a lot of requests key Does not exist in the cache at all , Direct request to the database . Not cached at all
problem 14 Cache avalanche solution ?
In advance : Guarantee redis The high availability of the cluster is discovered and the downtime is supplemented , Choose the right memory retirement strategy
Things : Use local ehcache cache + hystrix Current limiting the drop , avoid mysql Collapse
After the incident : utilize reids The persistence mechanism recovers data as soon as possible
problem 15 The solution to cache penetration ?
- Invalid cache key, Here, the request can be solved key Infrequently , This will result in a large number of key. Obviously, what kind of solution can't solve the problem at all
- The bloon filter : We put the values of all possible requests in the bloom filter, and the user requests them , We will send it to users of xianpa section i Whether the request value exists in the Boolean filter. If it does not exist, the return parameter does not exist. If it exists, the normal execution
problem 16 How to solve redis Concurrent competition key The problem of ?
Multiple system pairs redis The same key Through operation , But the final execution result and execution sequence are different from what we expected , You can use distributed locks
problem 17 How to ensure redis The cache is consistent with the database double write
Update the database first , There is no concurrency problem when deleting the cache again ?
(1) The cache just failed
(2) request A Query the database , Get an old value
(3) request B Writes the new value to the database
(4) request B Delete cache
(5) request A Writes the old value found to the cache
There is a congenital condition for this to happen , It's the steps (3) Write database operations than steps (2) The read database operation takes less time , It's possible to make steps (4) Before the steps (5). But , Think about it , Database reads are much faster than writes ( Why else do read and write separation , The point of doing read/write separation is because the read operation is faster , Less resources ), So step (3) It takes more time than steps (2) shorter , It's very difficult for this to happen .
If it is mysql What we use here is canal frame stay binlog Update the cache when
problem 18 redis How the principle of master-slave replication ?
Redis It is generally used to support high read concurrency , In order to share the reading pressure ,Redis Support master-slave replication . Architecture is master-slave architecture , One master, many followers , The master is responsible for writing , And copy the data to other slave node , The slave node is responsible for reading . All read requests go from the node . In this way, it is easy to realize horizontal expansion , Support read high concurrency .
problem 19 redis Sentinel mode ?
Sentinel( sentry ) yes Redis High availability solutions for : By one or more Sentinel Examples of Sentinel The system can monitor any number of primary servers , And all the slave servers under these master servers , And when the monitored master server goes offline , Automatically upgrade a slave server under the offline master server to a new master server .
边栏推荐
- [Niu Ke's questions -sql big factory interview questions] no1 An audio short video
- Fastadmin list multi image preview (zoom in preview, rotation) one line of code
- Empty, isset and is of PHP_ Null difference
- 快速排序
- [schematic diagram and PCB] design of ultrasonic rangefinder based on single chip microcomputer
- 順序錶的基本操作
- [BP regression prediction] optimize BP regression prediction based on MATLAB GA (including comparison before optimization) [including Matlab source code 1901]
- 顺序表的基本操作
- Search (intensive training)
- 树莓派初步使用
猜你喜欢
随机推荐
順序錶的基本操作
Is it safe to open an account in Guoyuan futures?
EcRT of EtherCAT igh source code_ slave_ config_ Understanding of dc() function.
Redis和MySQL如何保持数据一致性?强一致性,弱一致性,最终一致性
Design and implementation of ks004 based on SSH address book system
低功耗雷达感应模组,智能锁雷达感应方案应用,智能雷达传感器技术
Empty, isset and is of PHP_ Null difference
[Niu Ke's questions -sql big factory interview questions] no1 An audio short video
"Defi moral paradox" behind solend farce
What is the value of the FC2 new domain name? How to resolve to a website?
How to check the domain name for free through IP?
Solend闹剧背后的「DeFi道德悖论」
[day 23] given an array of length N, return the array after deleting the x-th element | array deletion operation
冒泡排序
拓扑排序
Daily question: the difference between ArrayList and LinkedList
Solutions pour l'écran bleu idea
【写文章神器】标记语言Markdown的使用
树的存储结构
Code of ultrasonic rangefinder based on 51 (screenshot version)









