I have a very simple query that inserts every api call to messageRequest table. it can go maximum of 1000 requests or more per second.
for example, http://myapplication.com/sendMessage.php?phone=123&gsm=mobitel&message=helloworld
assume, anyone with our app in mobile will call this URL. and when i make like 1000 calls via
ab -n 1000 -c 10 "http://myapplication.com/sendMessage.php?phone=123&gsm=mobitel&message=helloworld"
The query is insert into messageRequest values(null, phone, gsm, message);
it works fine, but the issue is while this many concurrent calls are there, when i try to use my activity log page in this URL http://myapplication.com/apiLogs.php to see how many SMS api calls are received in last 30 minutes.
It just queries SELECT count(*) from messageRequest where created_at > '2016-10-01 12:01:00'
it takes like 100 seconds to return result. and when i stopped making concurrent calls via ab -n command its working fine. so i assumed when 1000 concurrent calls happens and a record is inserted for every call, MYSQL table messageRequest is getting locked.
to avoid this problem,
i changed this to multiple sessions, for example to sendMessage.php i made it run with user1, and for the apiLogs.php i made it with user2. so 2 diff sessions can access the same table to read and write simultaneously but that didnt help too.
sendMessage.php
define('DB_HOST', 'localhost');
define('DB_USER', 'user1');
define('DB_PASS', '');
apiLogs.php
define('DB_HOST', 'localhost');
define('DB_USER', 'user2');
define('DB_PASS', '');
i am confused how to make it work without any waiting time while so many concurrent calls?
Update
sendMessage.php code
define('DB_HOST', 'localhost');
define('DB_USER', 'user1');
define('DB_PASS', 'user1');
mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db('smsApp');
mysql_query(
"insert into messageRequest values (
NULL,
'{$_REQUEST['phone']}',
'{$_REQUEST['gsm']}',
'{$_REQUEST['message']}',
'{$_REQUEST['text']}',
now()
);
"
);
And i am making concurrent request with this command
ab -n 1000 -c 10 "http://myapplication.com/sendMessage.php?phone=123&gsm=mobitel&message=helloworld&text=123"
And while is running, i am trying to view this page apiLogs.php (source code is below)
define('DB_HOST', 'localhost');
define('DB_USER', 'user2');
define('DB_PASS', 'user2');
mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db('smsApp');
$30m_ago = new DateTime("30 minutes ago");
$s = $30m_ago->format("Y-m-d H:i:s");
$result = mysql_query("SELECT count(*) from messageRequest where created_at > '$s'");
$response['last_30_min_sms_count'] = current(mysql_fetch_row($result));
echo json_encode($response)."\n";
messageRequesttable.