当前位置:网站首页>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 .
边栏推荐
- Shuttle encapsulated AppBar
- Use sqoop to export ads layer data to MySQL
- Embedded Software Engineer career planning
- 刷题---二叉树--2
- 2.7 binary tree, post order traversal - [FBI tree]
- Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
- (C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
- Intel internal instructions - AVX and avx2 learning notes
- async/await 异步函数
- 高性能纠删码编码
猜你喜欢
CDH6之Sqoop添加数据库驱动
Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
Sweetheart leader: Wang Xinling
spfa AcWing 852. spfa判断负环
甜心教主:王心凌
Tas (file d'attente prioritaire)
寻找二叉树中任意两个数的公共祖先
Heap (priority queue)
Sort---
[FFH] little bear driver calling process (take calling LED light driver as an example)
随机推荐
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
甜心教主:王心凌
Introduction to CPU instruction set
The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
Sort---
模块化 CommonJS ES Module
Embedded Software Engineer career planning
bellman-ford AcWing 853. 有边数限制的最短路
Map and set
高性能纠删码编码
Intel internal instructions - AVX and avx2 learning notes
Input box assembly of the shutter package
Post request body content cannot be retrieved repeatedly
Leetcode14 longest public prefix
Redis avalanche, penetration, breakdown
Find the common ancestor of any two numbers in a binary tree
包管理工具
BOM DOM
drools执行String规则或执行某个规则文件