The php driver of Apache plus mongodb is unstable under high concurrency.


My environment: Apache/2.2.21 (Win32) PHP/5.4.3
Test code:

$db=new Mongo("mongodb://localhost:27017");
$user = $c->findOne(array('userName' => 'admin','passWord'=>'admin'));
echo var_dump($user);
echo var_dump($db);

Use apache concurrent testing tool ab.exe to test. Test command is as follows

D:\Apache2.2\bin>ab.exe -n 100

Test results:

Concurrency Level:      1
Time taken for tests:   0.266 seconds
Complete requests:      100
Failed requests:        58
   (Connect: 0, Receive: 0, Length: 58, Exceptions: 0)
Write errors:           0
Total transferred:      888192 bytes
HTML transferred:       870668 bytes
Requests per second:    376.47 [#/sec] (mean)
Time per request:       2.656 [ms] (mean)
Time per request:       2.656 [ms] (mean, across all concurrent requests)
Transfer rate:          3265.41 [Kbytes/sec] received

Php error message:

[13-Jun-2012 07:40:26 UTC] PHP Notice:  Unknown: localhost:27017: pool done (0x3877a10) in Unknown on line 0

First of all, it is suggested that you set the pool size of php mongo before you initializeMongoBefore the object


This can improve the connection efficiency of mongo, especially in the case of large concurrency. In addition, your ab command was used incorrectly.

  1. You used it-n 100, this number is too small, usually to set more than thousands of times
  2. You didn’t use it-cThe parameter sets the concurrency number, causing ab to press concurrency according to the limit of the total number, which is not scientific.

You should use

ab.exe -n 1000 -c 100

This completes 1,000 requests at 100 concurrent times. You can adjust these two parameters according to your own situation.

Last but not least, sometimes errors are reported, which is not unstable. Every server has its limits. For example, you are using windows xp to test server performance. Its hardware and software optimization (such as half-open connections) will greatly affect the final performance.

If you want to test the server performance scientifically, it is recommended to use a special server and select a standard optimized linux operating system.