Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP request balancing in LAMP server

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

  1. To get the apache process counts if it is goes beyond 120 then I planned to reject the requests from server.

  2. 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.

like image 828
Sundar Avatar asked Jan 30 '14 07:01

Sundar


People also ask

What does PHP do in LAMP?

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.

Is LAMP stack still used?

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.

Which are the four components of the LAMP framework in PHP?

LAMP has four components: Linux, Apache, MySQL and Perl, PHP and/or Python.

What is LAMP stack and mean stack?

LAMP and MEAN are popular open-source web stacks used for developing high-performance, enterprise-grade web and mobile apps.


2 Answers

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:

  • every time you get a request, you check the queue and make sure you don't have more than 200 active requests;
  • the next step would be to check that the request should run (this implies you can uniquely identify each request i.e by checking some value in GET/POST) - this allows you to make sure that if request #200 was processed, let's say in the last minute, you will ignore it and allow for request #201 to run;
  • if the request checks out, you add it to the queue as active and mark it as completed / delete it from queue once it's done;

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.

like image 147
Relu Motica Avatar answered Sep 28 '22 02:09

Relu Motica


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);
?>
like image 31
KikoV Avatar answered Sep 28 '22 00:09

KikoV