当前位置:网站首页>Middleware_ Redis_ 06_ Redis transactions
Middleware_ Redis_ 06_ Redis transactions
2022-06-11 01:19:00 【Study in a small corner】
List of articles
Redis Business
1、 Business profile
problem :Redis During the execution of instructions , Multiple consecutive instructions are disturbed , interrupt , Jump the queue , How to solve ?
redis Concept of transactions :redis A transaction is a queue of command execution , Wrap a series of predefined commands into ( all in one ) A line .
When executed , One time sequential execution , There will be no interruption in the middle , That is, in a queue , Disposable 、 Sequence 、 Execute a series of commands exclusively .
2、 Transaction basic operations
2.1、 The basic operation of a transaction
# Open transaction
multi
# effect
Set the start position of the transaction , After this instruction is executed , All subsequent instructions are added to the transaction
# Perform transactions
exec
# effect
Set the end position of the transaction , Execute transactions at the same time .
# Be careful :
1. multi and exec Pairs appear , Use in pairs
2. The command to add a transaction is stored in the task queue first , Not immediately , Only execute exec It's the order that begins
problem : A problem was found during the definition of the transaction , What do I do ?
Solution :
# Cancel the business
discard
# effect
Terminate the definition of the current transaction , It happened in multi after ,exec Before
2.2、 The workflow of a transaction

2.3、 Transaction considerations
problem 1: In the process of defining transactions , What if the command format is wrong ?
Grammar mistakes
It means that the command writing format is incorrect
Processing results
There is a syntax error in the command contained in the defined transaction , All commands in the overall transaction will not be executed . Including commands with correct syntax
problem 2: In the process of defining transactions , What if there is an error in command execution ?
Running error
Indicates that the command format is correct , But it can't be executed correctly . for example , Yes list Conduct incr operation
Processing results
Commands that run correctly execute , Running the wrong command will not be executed
Be careful : The data corresponding to the executed command will not be rolled back automatically , You need to implement rollback by yourself .
2.4、 Manual transaction rollback
- During recording operation , The status of the affected data
- Single data :string
- More data :hash、list、set、zset
- Set command , Restore all modified items
- Single data : direct set( Pay attention to the surrounding properties , For example, prescription )
- More data : Modify the corresponding value or overall clone assignment
3、 lock
3.1、 Transaction execution based on specific conditions —— lock
Business scenario :
- Tmall double 11 During the hot sale , Replenish the sold out goods ,4 Every salesman has the right to replenish . Replenishment may be a series of operations , Multiple continuous operations are involved , How to ensure that the operation will not be repeated ?
Business analysis :
- Multiple clients may modify the same set of data at the same time , And once the data is modified , Will not apply to continue operation
- Lock the data to be operated before operation , As soon as it changes , Terminate the current operation
Solution :
# Yes key Add monitoring lock , In execution exec If key change , Terminate transaction execution
watch key1 [key2] ……
# Cancel for all key Surveillance
unwatch
Tips:Redis It is applied to batch task execution based on state control
3.2、 Transaction execution based on specific conditions —— Distributed lock
Business scenario :
- Tmall double 11 During the hot sale , Replenish the sold out goods , And the replenishment is completed . Customers are enthusiastic about buying ,3 Buy all goods in seconds . The inventory has been completely emptied in this replenishment , How to prevent the last item from being purchased by more than one person at the same time ?【 Oversold problem 】
Business analysis :
- Use watch Monitor one key Is there any change that can't solve the problem , The specific data to be monitored here
- although redis It's single threaded , But when multiple clients operate on the same data at the same time , How to avoid not being modified at the same time ?
Solution :
# Use setnx Set a public lock
setnx lock-key value
utilize setnx The return value characteristic of the command , If there is a value, the setting fails , If there is no value, the setting is successful
- For the return setting successful , Have control , Carry out specific business operations in the next step
- For return settings that failed , No control , Line up or wait
After the operation, pass del Operate the release lock
Be careful : The above solution is a design concept , Rely on norms to ensure , Risky
Tips:Redis It is applied to scene control based on distributed lock correspondence
3.2、 Transaction execution based on specific conditions —— Distributed lock improvement
Business scenario :
- Mechanisms that rely on distributed locks , When a user operates, the corresponding client goes down , And the lock has been obtained at this time . How to solve ?
Business analysis :
- Since the lock operation is controlled by the user, lock and unlock , There must be a risk of not unlocking after locking
- The unlocking operation that needs to be performed cannot rely solely on user control , At the system level, the corresponding minimum treatment scheme shall be given
Solution :
# Use expire For lock key Add time limit , Don't release , Abandon lock
expire lock-key second
pexpire lock-key milliseconds
Because operations are usually microseconds or milliseconds , Therefore, the locking time should not be set too large . The specific time needs to be confirmed after business test .
- for example : The longest execution time for holding a lock 127ms, The shortest execution time 7ms.
- Testing the maximum execution time of a million times corresponds to the maximum time consumption of a command , Testing millions of network delays takes an average of
- Lock time setting is recommended : Most time consuming x 120% + Average network latency x 110%
- If the business takes the most time << Average network latency , Usually it is 2 An order of magnitude , It takes a long time to take one of them
边栏推荐
- 北京通州区高新技术企业培育支持标准,补贴10万
- zabbix离线安装
- ion_ dma_ buf_ begin_ cpu_ access
- Basic introduction of graph and depth first traversal and breadth first traversal
- Current limiting and download interface request number control
- 简述自定义注解
- Animation
- Support standard for cultivation of high-tech enterprises in Changping District, Beijing, with a subsidy of 100000 yuan
- Introduction to the subsidy fund for leading technological innovation of Beijing enterprises, with a subsidy of 5million yuan
- 配置化自定义实现1.实现接口,2.自定义配置3.默认配置
猜你喜欢
![[persistent problems of NVIDIA driver] - - /dev/sdax:clean, xxx/xxx files, xxx/xxx blocks - the most complete solution](/img/0e/8c79f7c77f61dfa9a155ab33b77081.png)
[persistent problems of NVIDIA driver] - - /dev/sdax:clean, xxx/xxx files, xxx/xxx blocks - the most complete solution

ZABBIX offline installation
WSL 自动更新 ip hosts 文件

Centos7 actual deployment mysql8 (binary mode)

pytorch分类问题总结
![[paper reading] fixmatch: simplifying semi supervised learning with consistency and confidence](/img/86/72726f933deef6944b62149759b7d5.png)
[paper reading] fixmatch: simplifying semi supervised learning with consistency and confidence

CentOS actual deployment redis
![[introduction to ROS] - 03 single chip microcomputer, PC host and ROS communication mechanism](/img/ad/d798284ceb370f7c68cbab755a11de.png)
[introduction to ROS] - 03 single chip microcomputer, PC host and ROS communication mechanism

IRS应用发布之十六:H5 应用设计指南

项目_基于网络爬虫的疫情数据可视化分析
随机推荐
Current limiting and download interface request number control
About the log traffic monitoring and early warning small project | standardized return of interaction with the database in flask
Why can't Google search page infinite?
Bubble sort and quick sort
Blend for visual studio overview
用data和proc怎么写出这个,不用sql
Beijing Dongcheng District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
Josephus problem_ Unidirectional circular linked list_ code implementation
cosine 相似度计算总结
Shenzhen Nanshan District specialized special new enterprise application process, with a subsidy of RMB 100000-500000
部分 力扣 LeetCode 中的SQL刷题整理
配置化自定义实现1.实现接口,2.自定义配置3.默认配置
2022年高考加油 那年我的高考如此兴奋
明朝的那些皇帝
CSRF攻击
云呐|庆远固定资产管理及条码盘点系统
OTA升级
Hash table (hash table \u hashtable)_ Array + linked list_ Code for employee management
Introduction to prefix, infix and suffix expressions (code implementation of inverse Polish calculator)
Solution to prompt "network initialization failed operation failed" in PD virtual machine installation system