当前位置:网站首页>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 .
边栏推荐
- Fastdateformat why thread safe
- CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
- Redis avalanche, penetration, breakdown
- Input box assembly of the shutter package
- Adding database driver to sqoop of cdh6
- ThreadLocal的简单理解
- What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
- spfa AcWing 851. spfa求最短路
- Anti shake throttle
- When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
猜你喜欢
![[ybtoj advanced training guidance] cross the river [BFS]](/img/4e/83f14452ea6410768cdd01e725af2e.jpg)
[ybtoj advanced training guidance] cross the river [BFS]

Multiply LCA (nearest common ancestor)

堆(优先级队列)

Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?

Anti shake throttle

Distributed machine learning framework and high-dimensional real-time recommendation system

Deep copy event bus

Dijkstra AcWing 850. Dijkstra求最短路 II

SparkContext: Error initializing SparkContext解决方法

ThreadLocal的简单理解
随机推荐
Sort---
深拷貝 事件總線
"As a junior college student, I found out how difficult it is to counter attack after graduation."
Anti shake throttle
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
防抖 节流
Leetcode14 longest public prefix
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
LeetCode—剑指 Offer 59 - I、59 - II
LeetCode—<动态规划专项>剑指 Offer 19、49、60
drools执行String规则或执行某个规则文件
MySQL and PostgreSQL methods to grab slow SQL
Intel internal instructions - AVX and avx2 learning notes
Jenkins user rights management
计算二叉树的最大路径和
高性能纠删码编码
Dijkstra AcWing 850. Dijkstra求最短路 II
Input a three digit number and output its single digit, ten digit and hundred digit.
Go学习笔记—基于Go的进程间通信
WSL 2 will not be installed yet? It's enough to read this article