当前位置:网站首页>[redis series] redis learning 13. Redis often asks simple interview questions
[redis series] redis learning 13. Redis often asks simple interview questions
2022-04-23 12:02:00 【Little Devil boy Nezha】
Let's see redis Often ask common interview questions
Redis What is a ?
http://www.redis.cn/ redis The Chinese website gives a very clear and clear definition
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-5HZnf7He-1650460558149)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210828092258505.png)]
-
Redis It's open source (BSD The license ) Of , Data structure storage system in memory
-
Redis Can be used as a database 、 Caching and message middleware
-
The supported data structures are 8 Kind of
character string (strings), hash (hashes), list (lists), aggregate (sets), Ordered set (sorted sets), bitmaps, hyperloglogs and geospatial
Use redis What are the benefits ?
- The data accessed is very fast ,redis The data is operated directly in memory
- Rich features : Available for caching , Message queue , wait
- Support 8 Kind of data type
Redis Why so fast ?
First redis It's single threaded , Many people think that a single thread must be slow , Multithreading must be fast , This is a misunderstanding
- redis Using multiple channels I/O Reuse model , Non blocking Of IO
- Using single thread , There will be no consumption of context switching , Don't think about locking and unlocking , There is no performance consumption of competing resources
- The data structure is simple , Above 8 Kind of data type , Master something without too much effort
- operation redis The data of , Basically in the proper operation memory , Must be quick
Redis Can it be persistent ? If you can , What are the ways of persistence ?
It can persist , Yes 2 Ways of planting , RDB and AOF , redis The default is to support RDB Persistence
- RDB Persistence mode
Writes a dataset snapshot in memory to disk within a specified time interval , This is the snapshot snapshot , When restoring a snapshot , Is to read the snapshot file into memory .
redis adopt fork Create a child process to do persistent actions

advantage
- Suitable for large-scale data recovery
- The requirement of data integrity is not high
Inferiority
-
Operations need to be performed at certain intervals , If redis Unexpected downtime , The last written data will be lost
-
fork The subprocess needs to occupy a certain space
-
AOF Persistence mode
AOF yes redis Another way of persistence , Log every write operation , take redis All write operations performed are recorded , Only additional files are allowed , Rewriting of files is not allowed

advantage
- Every operation reids Will be recorded , The integrity of the file is good
- Synchronize once per second , One second of data could be lost
- No synchronization , This is the most efficient
Inferiority
- Relative to data files ,aof The file will be much larger than rdb file , It's faster than rdb The files are slow
- aof Operating efficiency ratio rdb slow , therefore redis The default configuration is rdb Persistence
Redis What are the application scenarios ?
- Simple message queuing
Simple message queue for publishing and subscribing , Use to redis in List data structure , Can pass lpush and rpop Write and read messages , This only applies to simple message queues , If you have a lot of data , Require data consistency , Good performance, etc , Of course, professional message queuing components are preferred
- Used to cache
Access data that you often need to access redis in , And set up redis Maximum memory limit and deletion key The strategy of
- For counter
For example, web page visits , Like and so on ,redis Support self increasing and self decreasing operations , The data is all in memory , Very suitable for counting
- Do distributed locks
redis The order that comes with you SETNX , Naturally, it can be used as a distributed lock , It is used to control the orderly operation of multiple nodes , Of course redis I have provided RedLock For everyone to use
RedLock What is a
Redis The information provided by the official website is based on Redis How to implement distributed locks , It has the following characteristics :
- Fault tolerance
As long as most Redis If the node survives, it can provide services normally
- Safety features
RedLock Mutually exclusive access , There will always be only one redis The client can get the lock , Perform the operation
- Avoid deadlock
Final redis The client may get the lock , There will be no deadlock
- Make a leaderboard
Use zset Make a leaderboard ,top xx And so on
- Do common concern , Be a good friend around you
redis Provide a collection set , Can calculate the common friends , Common interests , And so on
Redis Delete expired key How is it handled ?
Yes 3 Ways of planting :
- Timed expiration
Timed expiration , Every time you set a key When , All for this key Set the corresponding timer , When this key ttl At maturity , Will this key Delete the
Every key When the expiration time is set, we need to get a timer , such It's very exhausting cpu resources , And then affect redis performance
- Expire regularly
Expire regularly , Is to set every other period of time , scanning redis Set the expiration time in key, And delete the expired key.
- Inertia expires
Inertia expires , Whenever you visit this key When , To judge this key Is it invalid , Delete when it fails , If you don't visit , Then this key It's always in memory , It takes up a lot of memory
Redis What is the essence of affairs ?
Is a collection of commands , All commands in a transaction are serialized , In the process of transaction execution , The commands are executed in order , They have
- Disposable
- Sequence
- exclusive
redis There is no concept of isolation level for transactions
redis Transaction , Execute the command like this
Put commands in transactions , Not immediately , But only when the execution order is initiated , adopt exec Trigger

redis It's a single instruction that guarantees atomicity , But transactions don't guarantee atomicity
The process of executing a transaction is like this :
- multi Open transaction
- Join the team with various orders
- exec Perform transactions
The transaction ACID What do you mean by separation ?
- Atomicity Atomicity
Atomicity refers to , A business is an indivisible whole , Or make it together , Or fail together
- Consistency Uniformity
Before and after the execution of the transaction , The integrity of the data must be consistent
- Isolation Isolation,
When multiple transactions are processed at the same time , Mutual interference , The complementary effect , Let's play
- Durability persistence
refer to , Once the transaction is committed , So the data affected is persistent
Redis What is the principle of master-slave replication ?
Slave Start successfully connected to master A... Will be sent later sync command
master After receiving the order , Start the background save process , At the same time, collect all commands received to modify the database set , After the background process is executed ,master The process will transfer the entire data file to slave , And complete a synchronization
Copy in full
slave Service received master After the data transmitted , Save it and load it into memory
Incremental replication
master Pass all the newly collected modified commands to slave , And complete synchronization
Once reconnected master node , A full synchronization is performed
What does sentinel mode do ?
Sentinel mode , Mainly for the realization of redis High availability of clusters
Because using master-slave replication , When redis When the host goes down , There is no way to elect a host , Sentinel mode can

The role of a sentry :
- By sending commands ,Redis The server returns monitoring status information , Including master and slave servers
- If the sentry detects that the primary server is down , Will automatically slave Switch master, Then notify other slave servers by publishing and subscribing , Modify the configuration file , Let him be the host
Subjective offline
If master Server down , Then one of the Sentinels will detect , The system does not execute immediately failover The process of , Just the current sentinel , Judge master Unavailable , This is Subjective offline
Objective offline
The general sentry in the cluster is also a cluster , If deployed 3 A sentinel
When the other two sentinels found master When the server is unavailable , Then there will be a vote between the Sentinels , We will write the specific voting algorithm later , The voting structure was initiated by a sentry , Conduct failover Failover operations , After successful switching , Through the publish and subscribe mode , Let each monitoring sentry switch its monitoring server to this master Server , This is Objective offline
redis Of through , breakdown , Avalanche problem can be seen in the previous article :【Redis series 】redis Learn twelve ,redis Cache penetration , Cache breakdown , Cache avalanche
Reference material :
Welcome to thumb up , Focus on , Collection
friends , Your support and encouragement , I insist on sharing , The drive to improve quality

Okay , That's it this time
Technology is open , Our mindset , It should be more open . Embrace change , Born in the sun , Try to move on .
I am a Little Devil boy Nezha , Welcome to like, pay attention to collection , See you next time ~
版权声明
本文为[Little Devil boy Nezha]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231158518091.html
边栏推荐
猜你喜欢

5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型

激活函数之relu函数

WIN10 启动后花屏

Relu function of activation function

After a circle, I sorted out this set of interview questions..

程序员如何用130行代码敲定核酸统计

Yunna | fixed assets inventory supports multiple inventory methods (asset inventory)

C# F23.StringSimilarity库 字符串重复度、文本相似度、防抄袭
欣旺达宣布电池产品涨价 此前获“蔚小理”投资超10亿

Overall plan management mode in maker Education
随机推荐
Resolution due to AMD not found_ ags_ x64. DLL, unable to continue code execution. Reinstallation of the program may solve this problem, Forza horizon 5
力扣-70.爬楼梯
Blog post navigation (real-time update)
Master slave replication configuration of MySQL
5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型
激活函数之relu函数
docker MySQL主从备份
A detailed explanation of head pose estimation [collection of good articles]
Yunna | fixed assets inventory supports multiple inventory methods (asset inventory)
激活函数之sigmoid函数
第二十六课 类的静态成员函数
第四章 为IM 启用填充对象之启用和禁用表空间的IM列存储(IM 4.5)
亿级流量架构,服务器如何扩容?写得太好了!
NativeForMySQL 连接MySQL8 提示:1251- Client does not support authentication protocol
Here comes the detailed picture and text installation tutorial of H5 game
Share two practical shell scripts
PSCP basic usage
初探 Lambda Powertools TypeScript
In idea Solution to the problem of garbled code in Chinese display of properties file
Yunna | how to manage the company's fixed assets and how to manage fixed assets