当前位置:网站首页>Redis6 transaction and locking mechanism
Redis6 transaction and locking mechanism
2022-07-05 13:40:00 【Do you know what a code monster is?】
Catalog
Redis Three characteristics of transaction
Business
Redis A transaction is a separate isolation operation : All commands in the transaction are serialized 、 To execute in order . Transaction is in the process of execution , Will not be interrupted by command requests from other clients .
Redis The main function of a transaction is to concatenate multiple commands to prevent other commands from queuing .
Basic operation
multi Command to form a team , The next input instructions will enter the command queue in turn , But will not execute , Until input exec after ,Redis The previous commands in the command queue will be executed successively .
As shown below :
If you want to give up forming a team , Just input discard that will do .
Error mechanism
An error occurred while forming a team , All tasks will be canceled during execution .
As shown below :
An error occurred while executing , Only commands that report errors will not be executed , Other commands will be executed , No rollback .
As shown below :
Pessimistic locking
Every time Lock the data every time you get it , In this way, when others take the data, it will block until it is unlocked . There are many lock mechanisms used in traditional relational databases , For example, line locks. , Table lock, etc. , Read the lock , Write locks, etc. , It's all locked before the operation .
Optimism lock
It will not be locked every time you take data , But when updating, we will judge whether others have updated this data during this period , You can use mechanisms such as version numbers to control . Optimistic lock is suitable for multi read applications , This can improve throughput .Redis It's using this check-and-set The mechanism implements the .
monitor key
In execution multi Before , Execute first watch key1 [key2], You can watch one ( Or more ) key , If you're in business This before execution ( Or these ) key Altered by other orders , Then the business will be interrupted .
watch <key>
Cancel surveillance
Cancel watch Command to all key Surveillance .
If in execution watch After the command ,exec Order or discard If the order is executed first , Then there's no need to execute unwatch 了 .
unwatch
Example :
We are connecting number one and number two at the same time k1 To monitor , And all open transactions . In connection 1, add 100 The operation of , You can find , Connection 1 was successfully executed , Return results :200, Then add... In the second connection 200 The operation of , The result is nil, The reason is because k1 The data of has been modified , Cause the transaction to be interrupted .
Redis Three characteristics of transaction
Separate isolation operation : All commands in the transaction are serialized 、 To execute in order . Transaction is in the process of execution , Will not be interrupted by command requests from other clients .
There is no concept of isolation level : Commands in the queue will not be executed until they are submitted , Because no instruction is actually executed before the transaction is committed .
There is no guarantee of atomicity : If a command fails in a transaction , Subsequent orders will still be executed , No rollback .
Second kill case
Oversold problem
When shopping websites conduct commodity spike activities , In the case of concurrency , Locks need to be set to avoid oversold . for instance , A commodity has 1000 Pieces of , Yes 10000 A person is doing a second kill ,10000 Requests without lock , There will be oversold problems , The inventory of final goods is negative .
Solution
Use optimistic locks to limit , When the inventory of goods changes , Transaction execution failed .
Connection timeout problem
Many users send seckill requests at the same time , There will be a request timeout problem , You need to set the timeout .
Solution
Use connection pool to solve .
Inventory remaining problems
The optimistic lock is used to eliminate the oversold problem , But when a user succeeds in seckill , Someone who kills at the same time as this user , And the people who have succeeded in the second kill in the program due to the change of inventory information , Transactions do not execute . Although this part of people occupy the quota of second kill , But in fact, I didn't buy any goods , Inventory of goods has not decreased , But it took up the number of seconds .
Solution
Use LUA Script , Will be complex or multi-step redis The operation is written as a script , Submit to redis perform , Reduce repeated connections redis The number of times , Lifting performance . also LUA Scripts have a certain atomicity , Will not be interrupted by other orders . Use LUA Scripts solve problems , It's actually redis Take advantage of its single threaded feature , The problem of multi task concurrency is solved by using task queue .
边栏推荐
- mysql获得时间
- Summit review | baowanda - an integrated data security protection system driven by compliance and security
- C# 对象存储
- 网络安全-HSRP协议
- 记录一下在深度学习-一些bug处理
- 【Hot100】34. Find the first and last positions of elements in a sorted array
- JPA规范总结和整理
- "Baidu Cup" CTF competition in September, web:upload
- When using Tencent cloud for the first time, you can only use webshell connection instead of SSH connection.
- Write API documents first or code first?
猜你喜欢
MySQL - database query - sort query, paging query
Operational research 68 | the latest impact factors in 2022 were officially released. Changes in journals in the field of rapid care
Win10 - lightweight gadget
FPGA 学习笔记:Vivado 2019.1 添加 IP MicroBlaze
FPGA learning notes: vivado 2019.1 add IP MicroBlaze
Summit review | baowanda - an integrated data security protection system driven by compliance and security
精彩速递|腾讯云数据库6月刊
Shandong University Summer Training - 20220620
不知道这4种缓存模式,敢说懂缓存吗?
Laravel框架运行报错:No application encryption key has been specified
随机推荐
asp.net 读取txt文件
How to choose note taking software? Comparison and evaluation of notion, flowus and WOLAI
Binder communication process and servicemanager creation process
redis6事务和锁机制
TortoiseSVN使用情形、安装与使用
使用Dom4j解析XML
leetcode 10. Regular Expression Matching 正则表达式匹配 (困难)
What about data leakage? " Watson k'7 moves to eliminate security threats
Summit review | baowanda - an integrated data security protection system driven by compliance and security
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
华为推送服务内容,阅读笔记
这18个网站能让你的页面背景炫酷起来
时钟周期
Clock cycle
不知道这4种缓存模式,敢说懂缓存吗?
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
APICloud Studio3 WiFi真机同步和WiFi真机预览使用说明
精彩速递|腾讯云数据库6月刊
Could not set property ‘id‘ of ‘class XX‘ with value ‘XX‘ argument type mismatch 解决办法
Laravel框架运行报错:No application encryption key has been specified