Slow when reading from MongoDB using PHP mongodb driver with replica set

General Tech Bugs & Fixes 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

So when I connect to a MongoDB with multiple hosts, there is a delay in every one minute when executing a command. The delay time is exactly 10 seconds. I know this sounds so weird.

First I thought that the problem is related to MongoDB server configuration. But it only happens when I use PHP mongodb driver because it works fine when I tested with Java mongodb driver. - MongoDB Server: 3.6 - PHP mongodb driver: 1.5.3 - Java mongodb driver: 3.10.1

I also tested older version of PHP mongodb driver with the same problem.

PHP

// start tracking reading time
$start = microtime(true);

$m = new \MongoDB\Client('mongodb://rs1.host.com,rs2.host.com');
$db = $m->selectDatabase('test');
// slow when querying
$db->selectCollection('user')->findOne();

// end tracking reading time
$end = microtime(true);
$duration = $end - $start;

Full source code: https://drive.google.com/open?id=1r-EKiCntZbUq59sFzaBOiCrfy0tzjadt

Java

Date date1 = new Date();
long start = date1.getTime();

MongoClient mongoClient = MongoClients.create("mongodb://rs1.host.com,rs2.host.com");
MongoDatabase db = mongoClient.getDatabase("ple");

db.getCollection("user").find().first();

Date date2 = new Date();
long end = date2.getTime();

System.out.println((end - start) + "ms");

Full source code: https://drive.google.com/open?id=1TYTFrRxUiQUZdIUJdetllHpnOYHQiepd

I repeated the script above every 2 seconds, so here is result I tested:

PHP

0.013s
0.0056s
0.0135s
0.0052s
10.0271s (54sec from last delay)
0.0057s
0.0059s
0.0122s
0.0108s
0.0106s
0.0133s
0.014s
0.0095s
0.0076s
0.0145s
0.0102s
0.0148s
0.0058s
0.0132s
0.0125s
0.0134s
0.0056s
0.0055s
0.0135s
0.0124s
0.0116s
0.013s
0.013s
0.0139s
0.0135s
0.0132s
10.0234s (52sec from last delay)
0.0125s
0.0128s
0.0168s
0.0121s
0.0131s
0.0134s
0.0141s
0.0128s
0.0128s
0.0123s
0.0131s
0.0156s
0.006s
0.0053s
0.0058s
0.0136s
0.0131s
0.0139s
0.0131s
0.0063s
0.0125s
0.0127s
0.013s
0.0061s
0.0058s
10.015s (52sec from last delay)
0.0071s
0.0144s

Java

16ms
17ms
17ms
16ms
19ms
17ms
16ms
17ms
16ms
17ms
17ms
16ms
17ms
16ms
16ms
16ms

                                                
                                                
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.