[英]How to handle concurrent access in mysql?
If a shopping site only has 1 product available and 2 people try to buy it at the same time, who will get the product? 如果购物网站上只有1种产品可用,而2个人尝试同时购买,那么谁会得到该产品? How will the server prioritize the user."curious about flash sale on amazon,flip kart".
服务器将如何确定用户的优先级。“对亚马逊上的快速销售感到好奇”。 which algorithm?
哪种算法?
Followings are what could happen behind the scene and using any modern application framework it is pretty easy to achieve. 以下是幕后可能发生的事情,使用任何现代应用程序框架都非常容易实现。
I'm assuming that your scenario is: 我假设您的情况是:
U1
and U2
U1
和U2
Suppose T1
and T2
are the nanosecond representation of the time when they clicked on the checkout button. 假设
T1
和T2
是单击结帐按钮时的时间的纳秒表示。 The chances that T1
and T2
are equals to each other is very low but there is a possibility. T1
和T2
彼此相等的机会很小,但是有可能。
In your case, the web-server will serve both the requests generated by the users in two different threads, TH1
and TH2
. 在您的情况下,Web服务器将在两个不同的线程
TH1
和TH2
满足用户生成的两个请求。 It is highly unlikely since there are hundreds of users at any given time present in your system but not impossible that the TH1
and TH2
get served by two different core of the CPU, assuming you have more than one core. 这是极不可能的,因为在任何给定时间系统中都会有数百个用户,但假设您有多个内核,则
TH1
和TH2
由CPU的两个不同内核提供服务并非不可能。
Hence both of the TH1
and TH2
will try to get a hold onto your Product. 因此,
TH1
和TH2
都将尝试控制您的产品。
Now you need to have/introduce two attributes (think as MySQL columns) to your PRODUCT
: VERSION
and CHECKED_OUT
. 现在,您需要为
PRODUCT
提供/引入两个属性(如MySQL列): VERSION
和CHECKED_OUT
。
Both the TH1
and TH2
will start their own transaction at the same time, say TR1
and TR2
, assuming you have InnoDB as your database engine. 假设您将InnoDB作为数据库引擎,
TH1
和TH2
都将同时启动自己的事务,例如TR1
和TR2
。
Both of the TR1
and TR2
will: TR1
和TR2
都将:
PRODUCT
from the database table along with the VERSION
and CHECKED_OUT
: {id: 1, version: 0, checked_out: 0, ...}
and transfer it to the server. PRODUCT
以及VERSION
和CHECKED_OUT
: {id: 1, version: 0, checked_out: 0, ...}
并将其传输到服务器。 TR1
and TR2
will increase the VERSION
value which was read earlier and execute an Update statement stating that UPDATE PRODUCT SET CHECKED_OUT = 1, VERSION = 1 WHERE ID = 1 AND VERSION = 0
TR1
和TR2
都将增加先前读取的VERSION
值,并执行一条Update语句,该语句声明UPDATE PRODUCT SET CHECKED_OUT = 1, VERSION = 1 WHERE ID = 1 AND VERSION = 0
UPDATE
and return the number of the modified row in a sequential fashion since the UPDATE
should be executed within a single thread. UPDATE
并按顺序方式返回修改后的行号,因为UPDATE
应该在单个线程内执行。 Note here, this thread is DB's own thread, not the TR1
and TR2
. TR1
和TR2
。 TR1
, ie, TH1
was served before TR2
, ie, TH2
by DB's UPDATE
thread then business logic that is behind TR1
will get that the number of rows updated is equaled to 1, whereas that of TR2
will get 0. TR1
(即TH1
在TR2
之前,即TH2
由DB的UPDATE
线程提供),则TR1
后面的业务逻辑将获得更新的行数等于1,而TR2
的行数将变为0。 U1
get to check out the product whereas U2
will be notified with a nice apologizing message. U1
可以检出产品,而U2
会收到一条很好的道歉消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.