当前位置:网站首页>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 .
边栏推荐
- [deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
- Binder通信过程及ServiceManager创建过程
- 前缀、中缀、后缀表达式「建议收藏」
- Reflection and imagination on the notation like tool
- Flutter draws animation effects of wave movement, curves and line graphs
- Flutter InkWell & Ink组件
- 53. 最大子数组和:给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
- jenkins安装
- 个人组件 - 消息提示
- go map
猜你喜欢
"Baidu Cup" CTF competition in September, web:sql
Operational research 68 | the latest impact factors in 2022 were officially released. Changes in journals in the field of rapid care
jasypt配置文件加密|快速入门|实战
[daily question] 1200 Minimum absolute difference
Laravel framework operation error: no application encryption key has been specified
What is a network port
"Baidu Cup" CTF competition in September, web:upload
不知道这4种缓存模式,敢说懂缓存吗?
真正的缓存之王,Google Guava 只是弟弟
Record in-depth learning - some bug handling
随机推荐
今年上半年,通信行业发生了哪些事?
爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
Apicloud studio3 API management and debugging tutorial
Android本地Sqlite数据库的备份和还原
53. Maximum subarray sum: give you an integer array num, please find a continuous subarray with the maximum sum (the subarray contains at least one element) and return its maximum sum.
Idea remote debugging agent
Lb10s-asemi rectifier bridge lb10s
【Hot100】34. Find the first and last positions of elements in a sorted array
[public class preview]: basis and practice of video quality evaluation
go 字符串操作
49. Grouping of alphabetic ectopic words: give you a string array, please combine the alphabetic ectopic words together. You can return a list of results in any order. An alphabetic ectopic word is a
“百度杯”CTF比赛 九月场,Web:SQL
jasypt配置文件加密|快速入门|实战
49. 字母异位词分组:给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。 字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。
Summit review | baowanda - an integrated data security protection system driven by compliance and security
Jasypt configuration file encryption | quick start | actual combat
restTemplate详解
Reflection and imagination on the notation like tool
asp. Net read TXT file
Don't know these four caching modes, dare you say you understand caching?