Search code examples
mongodbmongodb-queryphp-mongodb

Exception "waiting for replication timed out" received for MongoDB Bulk Write operation


We have two servers for MongoDB servers, one is primary, the other is configured as replica.

We use PHP to bulk write a list of updates to MongoDB. From the server logs, we do some a small amount of exceptions of "waiting for replication timed out" in our server logs.

  1. What could be the reason of the exception? The network between two servers are stable.
  2. When we receive the exception "waiting for replication timed out", we can assume the Bulk Write operation has successfully updated the target documents to the primary server?

The bulk write part of the PHP codes:

 try {
            $manager = new MongoDB\Driver\Manager("mongodb://" . DB_HOST);
            $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 10000);
            $result = $manager->executeBulkWrite($DBName . '.' . $collection, $bulk, $writeConcern);
            return $result;
        } catch (Exception $ee) {
            AppendToLogFile(ERROR_Internal_Error, 'Script:' . __FILE__ . ', LINE:' . __LINE__ . ',$ee:' . $ee->getMessage());
            continue;
        }

Thanks!


Solution

  • As per your code, you have mentioned the write concern as Majority and yours is 2 member replica set, so the Majority will be 2. Also, you have mentioned the wtimeout value as 10000ms(10 seconds). So the expectation is all the writes should be replicated from primary to secondary in 10 seconds otherwise, the system will return an error message saying waiting for replication timed out.

    Answering your second question, yes the documents will be updated in the primary successfully, the message is only for the document is not replicated in the secondary in the given wtimeout time in ms.