How can I limit the number of requests to a php page, for example, to 3 in 1 sec, and how can this be better implemented?

Ps The page itself contains dynamic JSON data.

  • one
    the simplest is by IP ... a bit more complicated: a bunch of IP + browser type or even IP + browser type + browser version .... hardly anyone can change it more than 3 times per second - Alexey Shimansky
  • JSON data is individual for the user? Users authenticated? - Sergiks
  • Yes, and to access them, the user is logged in - Red Woolf
  • @RedWoolf if for access - the user is authorized, then in general what is the question? check that it is not a guest, and set the circulation interval for the user + limit circulation, which are reset to each specified interval (in this case, the second). - Alexey Shimanskyj

2 answers 2

And you do not restrict access, just cache the result by storing it in RAM, for example, in memcached with a retention period of 3 seconds.

 <?php $m = new Memcached(); $m->addServer('localhost', 11211); $json = 'error'; if (!($json = $m->get('json'))) { if ($m->getResultCode() == Memcached::RES_NOTFOUND) { // Долго и трудоемко вычисляем JSON $json = '{...}'; // Устанавливаем значение на 3 секунды $m->add('json', $json, 3); } } echo $json; 

As soon as memcached destroys the json key, the script will not detect it, will again calculate the dynamic request, put it in memcached and give it to the client. In between these events, json will be retrieved from memcached very quickly. Then you can give an arbitrary number of requests, without subjecting the storage to the responsibility for generating JSON. Moreover, with increasing load, you can increase the key storage time, and when it decreases, on the contrary, reduce it.

  • but will there be a restriction in general on access to the page? and if necessary for each user separately? ......... and not for 3 seconds but for 3 requests per second at the vehicle is written ;-) - Alexey Shimanskyj
  • @ Alexey Shimansky This is one of the options, perhaps JSON does not change at all for hours (there is little information in the question). If JSON changes extremely quickly, you can, as you suggest, store client information with a predetermined lifespan (as is done in the example), for example, its IP address in the same memcached (although probably better in redis) and check its presence. There is such an IP address - return return, no - give JSON. However, compared to common cache, this system is more fragile and expensive. - cheops
  • Well, in general, the idea is not bad, on the site I use memcache so you can use it - Red Woolf

It is better to implement this not in PHP, but by means of a web server.

For example for nginx there is a limit_req module intended just for your task - limiting the number of requests per unit of time. In the examples there is a config for limiting by one IP. But you can set a "key" and several variables - for example. IP and browser version:

 limit_req_zone $binary_remote_addr$http_user_agent zone=one:10m rate=3r/s; 
  • I use nginx but I would like to do it in PHP - Red Woolf
  • In any case, the decision will be, in the abstract, as follows: the statistics of previous requests are recorded in some shared memory area, read, and the decision is made to answer or not. The request goes to nginx, then it is sent to php-fpm. The question is: why drive a heavy php if you can solve the problem already at the nginx stage. - Sergiks 9:10 pm