Search code examples
mongodbreplicationdatabase-replicationreplicaset

Ensure data is not lost during MongoDB replication


Currently I have a replicaset for the production data. I am adding a new members to the replica set. The state of new members becomes SECONDARY (after STARTUP, STARTUP2 etc).

Does that guarantee that all data in primary member has been replicated to the new members?

Is there any way to make sure that no data is lost after replication?

(Is there anything specified in the official docs of MongoDB - any guarantee for data being not lost or something. I am using MongoDB 3.2)


Solution

    • When the initial sync is completed(clones data from source and the applies oplogs to maintain changes in the data set), you can call rs.printSlaveReplicationInfo() from primary mongodb shell.

    rs.printSlaveReplicationInfo()

    This will return the last oplog entry applied on the secondaries, which are copied from the primaries oplog.rs collection.

    The response is returned as:

    source: m1.example.net:27017
        syncedTo: Thu Apr 10 2014 10:27:47 GMT-0400 (EDT)
        0 secs (0 hrs) behind the primary
    source: m2.example.net:27017
        syncedTo: Thu Apr 10 2014 10:27:47 GMT-0400 (EDT)
        0 secs (0 hrs) behind the primary
    

    Notice that both secondary members are 0 seconds behind the primary which indicates no replication lag.

    That is essentially a difference b/w last operation recorded on primary and the time is was applied on the secondary.

    • As an additional precaution, you can note the db.stats() on the primary right before starting the sync and collecting same stats (db.stats()) from newly synced secondaries.

    Read about initial sync here