There are 2000 requests are received at a time in AWS 1.7GB server. The application is trying to handle this but ended with memory exhausted errors. I optimized the PHP script and MySQL database upto whatever I knows and gathered.
So here what I decide is
I would like to process 200 requests in the server and reject 1800 requests for first time. The next time the next 200 request will be processed and 1600 is rejected. In this way I can process all the requests.
Question:1 How to achieve this?
I planned to achieve this like below
To get the apache process counts if it is goes beyond 120 then I planned to reject the requests from server.
To monitor the server RAM free memory based on that I planned to reject the requests.
Suggestions Required: Which is the best option to implement?
If any other suggestions also welcome.
Question:2 How to get the apache process counts using PHP?
Question:3 How to get the free RAM memory size using PHP?
Note: Rejecting the requests are not a problem I can get back again. If I reject the requests from server then there is no problem and the server is normal. Once I processed this 2000 requests then there is no problem after I always get less load.
PHP (Hypertext Preprocessor) is a programming language that combines all the elements of the LAMP stack and allows websites and web applications to run efficiently. When a visitor opens the webpage, the server processes the PHP commands and sends the results to the visitor's browser.
The LAMP stack (Linux, Apache, MySQL, PHP) has been entrenched as the website, software, and IT standard for years, but more recently, the cloud and other new architecture models have been eroding its dominance.
LAMP has four components: Linux, Apache, MySQL and Perl, PHP and/or Python.
LAMP and MEAN are popular open-source web stacks used for developing high-performance, enterprise-grade web and mobile apps.
First of all, I advise against using system calls, especially when you have that many requests. Running external processes can cause big performance problems and since in your case the no. of processes / memory usage changes rapidly (you were saying 2000 requests at a time), you cannot use a cronjob to cache those values (even if you run a cron every second, you can't be sure the values are 100% real). You can get the memory usage for your script, approximate the no. of processes you can handle at a time and that should do it.
Now, as far as i understand, you want to process requests in a certain order: process requests 1-200, then 201-400, and so on? If that is the case, you would need to keep track of the requests that were already processed.
A simple way to achieve this would be to keep a request queue in a database - if you can use memcached or something similar, even better:
However, if request order doesn't matter to you, instead of a request queue you could just keep a request count, and make sure you never go above a certain limit.
I've made a prototype of a PHP process limiter using APC.
<?php
$processes = apc_fetch('processes');
if(!$processes) { // Initial Status
$processes=1;
}
if ($processes > 3) {
echo "Reject: ". $processes;
// Return HTTP/403 ...
exit -1;
}
$processes ++;
apc_store('processes', $processes);
// Long memory hunger code
sleep(10);
// .... your code .....//
// Implement global MUTEX??
$processes = apc_fetch('processes');
echo "Pending process: ". ($processes -1);
$processes --;
apc_store('processes', $processes);
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With