【问题标题】:How to achieve Concurrency in mysql when there are 500+ requests simultaneously to the server当有500+个请求同时发往服务器时如何在mysql中实现并发
【发布时间】:2014-04-10 08:06:15
【问题描述】:

我正在使用 Mysql (InnoDb),在这种情况下,当我们一次检查最多 500 个连接的 curl 请求时(同时)我遇到了问题,但是当我们尝试更多时,我的服务器进入空闲状态,然后我们有重新启动它。

为了更容易理解,我在 MYSQL (InnoDB) 中有一个包含“剩余金额”字段的数据库表。前任。字段包含 $500 的值,$10 将为每位用户扣​​除。

现在,当用户同时(一次)请求时,服务器不知道哪些用户具有更高的优先级,或者当请求被接受但没有更多数量时该做什么。

这里有两个问题:

  1. 收到并发请求时如何管理扣减
  2. 如何设置服务器的最大请求数,以便在更多请求 (500+) 后不会空闲

编辑

如果我想使用 mysql 的 LOCK 机制,那么它是否可以正常工作?

我正在使用 php 脚本与 mysql 通信

【问题讨论】:

  • 嘿@SegarPanchal,你在 cmets 中说你对 apache 的限制是 150,并且你已经尝试过其他答案,但它们没有用和/或完全否认答案,你真的应该多解释一些,否则人们不太可能帮助你:)

标签: php mysql apache innodb


【解决方案1】:

现代服务器环境可以使用多种语言、流程、环境等进行操作,这使得它们易于以各种方式进行调试、监控、更改和组合。

如果仅此请求对您的 MySQL 服务器造成很大压力,我建议使用辅助进程以单线程方式处理它。下面我介绍一个非常简单的 Tcl 辅助服务器和 Php 的胶水代码。可以选择其他语言和通信协议来提高性能,但 HTTP 和 Tcl 超级容易理解,每秒可以轻松处理数百个请求。

即使没有锁定,这也会很好地处理您的处理。 如果问题是 MySQL 服务器无法以传入的速度处理更新请求,那么这是硬件问题,您需要具有多个数据库服务器的完全不同的基础架构。

PHP 代码将处理委托给辅助服务器:

<?php
$result = "FAILURE";
$result = file_get_contents('http://localhost:2222?id=123&amount=10');
echo "result=$result!";
?>

Tcl server 如下,取消注释,把你的实际处理放到程序“Respond”中:

package require mysqltcl
set db [mysqlconnect -host localhost -port 3306 -user root -db test -encoding utf-8 ]
proc sql {sql} {
    global db
    if [catch {::mysql::exec $db $sql} err] {puts $err}
};#sql
proc select {sql} {::mysql::sel $::db $sql -flatlist}

set listenSocket [socket -server Accept 2222]

proc Accept {sock addr port} {
   puts "Accepted $sock from $addr port $port"
   fconfigure $sock -buffering line
   fileevent $sock readable "Respond $sock"
}

proc Respond {sock} {
   gets $sock request
   puts request=$request!
   set result "ERROR IN THE INVOKING PHP PROGRAM"
   if [regexp {id=(\d+)&amount=(\d+)} $request x id amount] {
    puts "request for amount=$amount id=$id"
    #set balance [select "SELECT balance FROM `accounts` WHERE somefield=$id"]
    #sql "INSERT Table2(name,field1,field2) VALUES('xxx',$balance,$amount)"
    #sql "UPDATE `accounts` SET amount=amount-$amount WHERE id=$id LIMIT 1"
    #set result [select "SELECT * FROM `whatever` WHERE condition"]
    set result "{\"balance\": \"$balance\",\"value\":\"$result\"}";#PHP will get this string, choose convenient format
   }
   puts $sock "HTTP/1.1 200 OK\n\n$result"
   close $sock
   puts "closed $sock"
}

vwait forever

所有的“puts”都是调试用的,可以删除。正如我所说,可以极大地优化通信(针对 Unix 管道、消息队列、共享内存),可以选择 C ​​而不是 Tcl 等。我只是选择了对我来说简单的。

“?id=123&amount=10”的请求当然是完全任意的,你可以设计不同的协议。

【讨论】:

  • 谢谢,太好了!!!这是一个非常深刻的困惑,尚未完全解决,但我同意你在某种深层方式(架构师级别)上的回答,再次感谢:),美好的一天
【解决方案2】:
  1. 使用数据库会话。这样您就可以从 DB 中获取行,在 PHP(或任何其他语言)中进行计算并将新值返回给 DB,而不必担心其他用户同时更改值。

  2. max_connections 在文件my.cnf(mysql 配置文件)中。您可以使用此工具https://tools.percona.com/wizard 来帮助您配置数据库。在 Percona 网站上,您还可以找到很多关于数据库配置和优化的好文章。

【讨论】:

  • 我已经尝试了 max_connections 到 500,但是 apache 的最大限制是 150,所以它不会工作,你使用数据库会话的第一个答案很好,我会试试这个,谢谢跨度>
【解决方案3】:

Michael Blood 的回答不起作用,因为 SQL 语句的序列不是“原子的”。

如果你只换一张桌子,那就简单多了。而且它比必须执行多个查询更安全:

UPDATE foo SET amt = amt - 15 WHERE amt > 15 AND id = 1001;
if rows_affected > 0, then success

如果您需要多个语句(例如从一个帐户中取钱,然后添加到另一个帐户),则必须在它们周围添加 BEGIN...COMMIT。

BEGIN;
SELECT amt FROM foo WHERE id = 1001 FOR UPDATE;  -- Note the lock on that row
if $amt >= 15 then
    UPDATE foo SET amt = amt - 15 WHERE id = 1001;
    UPDATE foo SET amt = amt + 15 WHERE id = 2222;
end
COMMIT;

连接完成后应断开连接。假设更新只需要几毫秒(实际上),您每秒可以轻松处理至少 100 个事务。

如果您需要超过 100/秒,请查看 innodb_flush_log_at_trx_commit。

【讨论】:

    【解决方案4】:

    借助 MySQL 的 LOCK'ing 机制,您可以在并发控制意义上使用一个缓冲表(临时表)。因此,当一个请求基于优先级发送到服务器时,您可以将请求设置为队列中的请求,或者在某种意义上限制表。这意味着在请求成功完成之前不会执行任何新条目。

    【讨论】:

      【解决方案5】:

      我的理解是您正在尝试采取以下步骤

       a. Retrieve the balance from the database
       b. Verify that the balance is sufficient
       c. If the balance is sufficient, update the database with the new balance (less $10)
       d. Return true
      

      当您有两个几乎同时的结果时会出现问题,并且

       a. Request #1 retrieves the balance of $15 from the database
       a. Request #2 retrieves the balance of $15 from the database
       b. Request #1 verifies that the balance is sufficient  - more than $10
       b. Request #2 verifies that the balance is sufficient  - more than $10
       c. Request #1 updates the database with a new balance of $5 ($15 - $10)
       c. Request #2 updates the database with a new balance of $5 ($15 - $10)
       d. Request #1 Returns true
       d. Request #2 Returns true
      

      更“线程安全”或“事务安全”的方法可能是更新数据库,然后查看余额。 这种方法使您不必担心从数据库中检索值和更新数据库之间的时间

       a. Update the balance in the database subtracting $10
       b. Retrieve the remaining balance and if it is greater than $0 return true
       c. If the balance is less than $0 put the money back into the database and return false
      

      在用户 account.id 为 1001 的一些发明表上的一些非常基本的伪代码:

      update account set balance = balance - 10 where id = 1001
      newbalance = select balance from account where id = 1001
      if  newbalance > 0  
          return true
      else
          update account set balance = balance + 10 where id = 1001
          return false
      

      【讨论】:

      • 不,当我同时收到请求时,此进程会挂起我的服务器
      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 2016-08-09
      • 2012-11-11
      • 2018-01-24
      • 2011-04-18
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      相关资源
      最近更新 更多