当前位置:网站首页>Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
2022-07-02 12:37:00 【Squat in the corner and count the ants】
One 、Redis The process and principle of transaction implementation
First step : Observation data
Before the client modifies the data , First use watch Command to observe the data to be modified , This step is equivalent to recording the version number of the data
The second step : Open transaction
Use multi Order to start a transaction .
After opening the transaction , All commands will not be sent immediately to Redis, Instead, cache it locally on the client , It's not really going to be implemented , Until the execution of exec When you order a transaction to be committed , The client will send these commands to Redis To carry out .
The third step :Redis Carry out orders
Client side usage exec Commit transaction ,Redis Received client exec After the command , The version number of the data will be compared first , If the version number is the same , Will execute all commands submitted by the client .
If the current version number and are watch The version number is inconsistent , Then all commands in the transaction submitted by the client will not be executed , It is equivalent to the transaction execution failure .
Two 、 Actual operation
Summarize the process : Use watch Monitoring data , Then start the transaction , Do business processing , Submission of orders .

notes : Under what circumstances , There will be inconsistent version numbers ?
For example, client A Open transaction , But it hasn't been implemented yet exec Submit , At this time, the client A Yes Redis The commands of are cached on the client A On , It has not been submitted in batch to Redis Carry out orders , At this time, the client B Write the data , Then the version number of this data will change , At this time , client A perform exec command , The version number will be inconsistent , Causes the client to A The problem that all commands under this transaction fail to execute .
3、 ... and 、PHP Pseudo code , Realize the prevention of commodity oversold

Four 、 explain
Redis adopt watch And multi Realize the problem of preventing oversold , Its essence is divided into two parts :
The first part is watch, The second part is from multi To exec All the orders of .
When the client watch command ,Redis Start monitoring data , Get the current version number of the data
When the client exec command ,Redis First, check whether the current version number of the data is consistent with the version number at the beginning of monitoring , If it is consistent, we will start from multi To exec All commands of are executed , otherwise multi To exec All commands of are not executed .
边栏推荐
- Day12 control flow if switch while do While guessing numbers game
- CDH6之Sqoop添加数据库驱动
- CDA data analysis -- common knowledge points induction of Excel data processing
- 包管理工具
- The second composition template of postgraduate entrance examination English / chart composition, English chart composition is enough
- (C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
- Record the range of data that MySQL update will lock
- ThreadLocal的简单理解
- Leetcode - Sword finger offer 59 - I, 59 - II
- Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
猜你喜欢

线性DP AcWing 902. 最短编辑距离

Tas (file d'attente prioritaire)
![[ybtoj advanced training guidance] judgment overflow [error]](/img/be/bbe357ac2f2a8839afc5af47db88d0.jpg)
[ybtoj advanced training guidance] judgment overflow [error]

Rust search server, rust quick service finding tutorial

CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节

Use sqoop to export ads layer data to MySQL

Openssh remote enumeration username vulnerability (cve-2018-15473)

浏览器node事件循环

VLAN experiment

Docker-compose配置Mysql,Redis,MongoDB
随机推荐
Simple use of drools decision table
VLAN experiment
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
High performance erasure code coding
Sub thread get request
Heap (priority queue)
Input a three digit number and output its single digit, ten digit and hundred digit.
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
堆(优先级队列)
Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
Go learning notes - go based interprocess communication
线性DP AcWing 895. 最长上升子序列
bellman-ford AcWing 853. 有边数限制的最短路
Multiply LCA (nearest common ancestor)
(C language) octal conversion decimal
H5 to app
arcgis js 4.x 地图中加入图片
计数类DP AcWing 900. 整数划分
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
[I'm a mound pytorch tutorial] learning notes