简体   繁体   English

如何在mysql中处理并发访问?

[英]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: 我假设您的情况是:

  • Two users logged into your system say they are U1 and U2 有两个用户登录到您的系统,说他们分别是U1U2
  • Select a product 选择一个产品
  • There is only one product is available 只提供一种产品
  • They both clicked Add to Cart/Buy Now/Checkout 他们都单击了添加到购物车/立即购买/结帐
  • One user will be served and another user will be notified that the product is not available anymore 将为一个用户提供服务,并且将通知另一用户该产品不再可用

Suppose T1 and T2 are the nanosecond representation of the time when they clicked on the checkout button. 假设T1T2是单击结帐按钮时的时间的纳秒表示。 The chances that T1 and T2 are equals to each other is very low but there is a possibility. T1T2彼此相等的机会很小,但是有可能。

In your case, the web-server will serve both the requests generated by the users in two different threads, TH1 and TH2 . 在您的情况下,Web服务器将在两个不同的线程TH1TH2满足用户生成的两个请求。 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. 这是极不可能的,因为在任何给定时间系统中都会有数百个用户,但假设您有多个内核,则TH1TH2由CPU的两个不同内核提供服务并非不可能。

Hence both of the TH1 and TH2 will try to get a hold onto your Product. 因此, TH1TH2都将尝试控制您的产品。

Now you need to have/introduce two attributes (think as MySQL columns) to your PRODUCT : VERSION and CHECKED_OUT . 现在,您需要为PRODUCT提供/引入两个属性(如MySQL列): VERSIONCHECKED_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作为数据库引擎, TH1TH2都将同时启动自己的事务,例如TR1TR2

Both of the TR1 and TR2 will: TR1TR2都将:

  • Read your 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以及VERSIONCHECKED_OUT{id: 1, version: 0, checked_out: 0, ...}并将其传输到服务器。
  • In the server, both of the 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 在服务器中, TR1TR2都将增加先前读取的VERSION值,并执行一条Update语句,该语句声明UPDATE PRODUCT SET CHECKED_OUT = 1, VERSION = 1 WHERE ID = 1 AND VERSION = 0
  • DB will lock the row, execute the UPDATE and return the number of the modified row in a sequential fashion since the UPDATE should be executed within a single thread. DB将锁定行,执行UPDATE并按顺序方式返回修改后的行号,因为UPDATE应该在单个线程内执行。 Note here, this thread is DB's own thread, not the TR1 and TR2 . 请注意,此线程是DB自己的线程,而不是TR1TR2
  • Here if I assume 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 (即TH1TR2之前,即TH2由DB的UPDATE线程提供),则TR1后面的业务逻辑将获得更新的行数等于1,而TR2的行数将变为0。
  • Which in turn imply that 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM