1

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!

leon
  • 10,085
  • 19
  • 60
  • 77

1 Answers1

5

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.

Mani
  • 1,471
  • 1
  • 13
  • 19
  • Thanks for your reply. The wtimeout I set is 10000. It should be 10 seconds right? – leon Mar 20 '19 at 10:21
  • I have updated my answer. Also as per our question, all your bulk writes are updates, Are you updating the same documents multiple times? – Mani Mar 20 '19 at 16:27
  • If it has timeout with the secondary on bulkwrite, will the data sync from the primary to secondary later to keep data consistency? – leon Mar 21 '19 at 02:03
  • `wtimeout` causes write operations to return with an error after the specified limit, even if the required write concern `will eventually succeed`. When these write operations return, MongoDB does not undo successful data modifications performed before the write concern exceeded the `wtimeout` time limit. – Mani Mar 21 '19 at 03:27