当前位置:网站首页>Redis common interview questions
Redis common interview questions
2022-04-23 18:46:00 【libin】
This article has participated in 「 New people's creation ceremony 」 Activities , Start the road of nuggets creation together .
redis lock
String isLock = jedis.set(lockKey, userId, "NX", "PX", 6 * 1000);
Copy code
jedis Of set Commands can have complex parameters , Through these parameters, the atomic distributed lock command can be realized
jedis.set(lockName, "", "NX", "PX", expireTime);
Copy code, code analysis
redis Of set Commands can carry complex parameters , The first one is locked key, The second is value, The client that can store the lock ID, Check whether the current client has obtained the lock , The value of the third parameter NX/XX, Fourth parameter EX|PX, The fifth is time
NX: If it doesn't exist, set this key XX: If it exists, set this key
EX: The unit is in seconds ,PX: The unit is millisecond
Redis What are the advantages and disadvantages
advantage
- Excellent reading and writing performance , Redis The speed at which you can read is 110000 Time /s, The speed of writing is 81000 Time /s.
- Support data persistence , Support AOF and RDB Two ways to persist .
- Support transactions ,Redis All operations of are atomic , meanwhile Redis It also supports atomic execution after several operations are combined .
- Rich data structure , In addition to supporting string Type of value Support in addition hash、set、zset、list And so on .
- Support master-slave replication , The host will automatically synchronize the data to the slave , Can be read-write separation .
shortcoming
- Database capacity is limited by physical memory , Cannot be used as high-performance read-write of massive data , therefore Redis Suitable scenarios are mainly limited to high-performance operations and operations with small amount of data .
- Redis No automatic fault tolerance and recovery functions , The downtime of the host and slave will lead to the failure of the front-end read and write requests , You need to wait for the machine to restart or manually switch the front end IP To recover .
- Host down , Before the outage, some data could not be synchronized to the slave in time , Switch IP After that, the problem of data inconsistency will be introduced , Reduced system availability .
- Redis Difficult to support online capacity expansion , When the cluster capacity reaches the upper limit, online capacity expansion will become very complex . To avoid this problem , The operation and maintenance personnel must ensure enough space when the system is online , This is a great waste of resources .
Redis Why so soon?
1、 Completely based on memory , Most requests are purely memory operations , Very fast . Data is in memory , Be similar to HashMap,HashMap The advantage is the time complexity of both the lookup and the operation O(1);
2、 The data structure is simple , It's also easy to manipulate data ,Redis The data structure in is specially designed ;
3、 Using single thread , Unnecessary context switches and race conditions are avoided , There is also no switching consumption caused by multiple processes or multiple threads CPU, You don't have to worry about locks , There is no lock release operation , There is no performance penalty due to possible deadlocks ;
4、 Using multiple channels I/O Reuse model , Non blocking IO;
5、 Using the underlying model is different , The underlying implementation and application protocols for communicating with clients are different between them ,Redis Build it yourself VM Mechanism , Because the normal system calls system functions , It's a waste of time moving and requesting ;
data type
Redis There are mainly 5 Type of data , Include String,List,Set,Zset,Hash, Meet most of the use requirements
Redis What is the persistence mechanism of ? Advantages and disadvantages of each ?
Redis There are two persistence mechanisms RDB( Default ) and AOF Mechanism :
RDB: yes Redis DataBase Abbreviated snapshot
RDB yes Redis Default persistence method . Save the memory data to the hard disk in the form of snapshot according to a certain time , The corresponding generated data file is dump.rdb. Through... In the configuration file save Parameter to define the period of the snapshot .
advantage :
1、 Only one file dump.rdb, Easy persistence . 2、 Good disaster tolerance , A file can be saved to a safe disk . 3、 Maximize performance ,fork Sub process to complete the write operation , Let the main process continue processing commands , So it is IO Maximize . Use a single child process for persistence , The main process will not do anything IO operation , To ensure the redis A high performance 4. When the data set is large , Than AOF It's more efficient to start . shortcoming :
1、 Data security is low .RDB It's persistence at intervals , If between persistence redis failure , There will be data loss . So this way is more suitable when the data requirements are not rigorous ) 2、AOF(Append-only file) Persistence mode : Means all command line records to redis Command please The format of the protocol is fully persistent ) Save as aof file .
AOF: Persistence
AOF Persistence ( namely Append Only File Persistence ), Will be Redis Each write command executed is recorded in a separate log file , When restarting Redis It will recover the data from the persistent log again .
When both modes are on at the same time , Data recovery Redis Preference will be given AOF recovery .
advantage :
1、 Data security ,aof Persistence can be configured appendfsync attribute , Yes always, Every time Command operations are recorded as aof Once in the file . 2、 adopt append Mode write file , Even if the server goes down , Can pass redis-check-aof Tools for data consistency . 3、AOF The mechanism rewrite Pattern .AOF The document was not rewrite Before ( When the file is too large, the command Do merge rewrite ), You can delete some of these commands ( For example, misoperation flushall)) shortcoming :
1、AOF File than RDB The file is big , And the recovery speed is slow . 2、 When the data set is large , Than rdb Low starting efficiency . What are the advantages and disadvantages ?
AOF File than RDB High update frequency , priority of use AOF Restore data . AOF Than RDB Safer and bigger RDB Performance ratio AOF good If both have priority loading AOF
Cache avalanche 、 Cache penetration 、 Cache preheating 、 Cache update 、 Cache degradation and other issues
Cache avalanche We can simply understand it as : Due to the original cache failure , Period before new cache ( for example : We set up the cache with the same expiration time , Large cache expiration occurs at the same time ), All requests that should have access to the cache are going to query the database , And for databases CPU And memory , Serious will cause database downtime . Thus a series of chain reactions are formed , Cause the whole system to crash . terms of settlement : Most system designers consider locking ( The most solutions ) Or the way of queue ensures that there will not be a large number of threads reading and writing to the database at one time , So as to avoid a large number of concurrent requests falling on the underlying storage system in case of failure . There is also a simple solution to spread the cache failure time .
Two 、 Cache penetration Cache penetration refers to user query data , There is no , Naturally, there won't be . This leads to user queries when , Could not find... In cache , Every time I have to go to the database and check it again , Then return to empty ( Equivalent to two useless queries ). In this way, the request bypasses the cache and directly queries the database , This is also a frequently raised cache hit rate problem . terms of settlement ; The most common is the use of bloon filter , Hash all possible data to a large enough bitmap in , A certain nonexistent data will be bitmap Intercept , Thus, the query pressure on the underlying storage system is avoided . There's also a simpler and rougher way , If the data returned by a query is empty ( Whether the data doesn't exist , Or a system failure ), We still cache this empty result , But its expiration time will be very short , Up to five minutes . The default values set directly are stored in the cache , In this way, the second access to the buffer has a value , Instead of continuing to access the database , This method is the simplest and rudest . 5TB Our hard disk is full of data , Please write an algorithm to arrange the data . If these data are some 32bit How to deal with the size of data ? If it is 64bit What about ?
The use of space has reached an extreme , That's it Bitmap And the bloon filter (Bloom Filter). Bitmap: A typical example is a hash table The disadvantage is that ,Bitmap Only... Can be recorded for each element 1bit Information , If you want to complete additional functions , I'm afraid we have to sacrifice more space 、 Time has come to complete .
The bloon filter ( recommend ) That is to introduce k(k>1)k(k>1) Independent hash functions , Make sure that in a given space 、 Under the rate of miscalculation , Complete the process of element weight determination . Its advantage is that the space efficiency and query time are far more than the general algorithm , The disadvantage is that it has certain error recognition rate and deletion difficulty . Bloom-Filter The core idea of the algorithm is to use multiple different Hash Function to solve “ Conflict ”. Hash There is a conflict ( Collision ) The problem of , Use the same Hash Two of them URL The value of may be the same . To reduce conflict , We can introduce more Hash, If you go through one of them Hash Value we get that an element is not in the set , Then the element must not be in the collection . Only in all Hash The function tells us that when the element is in the collection , To determine that the element exists in the collection . This is Bloom-Filter The basic idea of . Bloom-Filter It is generally used to determine whether an element exists in a large set of data . Be reminded to add : The difference between cache penetration and cache breakdown Cache breakdown : It means a key Very hot , Constantly carrying big concurrency , Large concurrent centralized access to this point , When this key At the moment of failure , Continuous large concurrency breaks through the cache , Request data directly . Solution ; During a visit to key Before , use SETNX(set if not exists) To set up another short term key To lock in the present key The interview of , Delete the short term after the visit key. increase : Give me a case that our company deals with : Background dual camera token,token Save a copy to redis, Make sure the system is in token When expired, there is only one thread to get token; There are two machines in the online environment , Therefore, distributed lock is used to implement .
版权声明
本文为[libin]所创,转载请带上原文链接,感谢
https:https://yzsam.com/html/oyujzL.html
边栏推荐
- The first leg of the national tour of shengteng AI developer creation and enjoyment day was successfully held in Xi'an
- ESP32 LVGL8. 1 - msgbox message box (msgbox 28)
- ctfshow-web362(SSTI)
- 解决:cnpm : 無法加載文件 ...\cnpm.ps1,因為在此系統上禁止運行脚本
- On iptables
- 使用晨曦记账本,分析某个时间段每个账户收支结余
- Domestic GD chip can filter
- Daily network security certification test questions (April 15, 2022)
- Query the logistics update quantity according to the express order number
- CISSP certified daily knowledge points (April 15, 2022)
猜你喜欢
iptables -L执行缓慢
ctfshow-web361(SSTI)
Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)
使用晨曦记账本,分析某个时间段每个账户收支结余
listener. log
Iptables - L executes slowly
ESP32 LVGL8. 1 - roller rolling (roller 24)
os_ authent_ Prefix
Druid SQL和Security在美团点评的实践
CANopen STM32 transplantation
随机推荐
Coolweather is revised and connected to the wind weather interface to realize the broken line diagram of temperature
After CANopen starts PDO timing transmission, the heartbeat frame time is wrong, PDO is delayed, and CANopen time axis is disordered
The connection of imx6 network port is unstable after power on
Eight bit binary multiplier VHDL
Introduction to quantexa CDI syneo platform
QT error: no matching member function for call to ‘connect‘
Dynamically add and delete layouts
CISSP certified daily knowledge points (April 14, 2022)
Introduction to QT programming
Custom prompt box MessageBox in QT
Function recursion and solving interesting problems
关于unity文件读取的操作(一)
Halo open source project learning (VII): caching mechanism
The corresponding permissions required to automatically open the app in the setting interface through accessibility service
Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système
QT curve / oscilloscope customplot control
Halo 开源项目学习(七):缓存机制
ctfshow-web361(SSTI)
Summary of actual business optimization scheme - main directory - continuous update
Use of content provider