Cannot use non-local read concern until replica set is finished initializing

When you receive the error message “cannot use non-local read concern until replica set is finished initializing” in MongoDB, it usually means that you are trying to use a non-local read concern on a replica set that hasn’t finished initializing yet.

Read concern in MongoDB allows you to control the visibility and consistency of data during read operations. There are three levels of read concern in MongoDB: “local”, “majority”, and “linearizable”. The read concern level determines how up-to-date and consistent the data is when reading from replica sets.

The error message indicates that you are trying to use a read concern level other than “local” on a replica set that is still in the process of initializing. During initialization, replica set members sync data from a primary member and elect a primary if necessary. Until this initialization process is complete, replica set members might not have consistent data, and using a non-local read concern can lead to inconsistencies and incorrect results.

To resolve this issue, you should either wait until the replica set finishes initializing or use the “local” read concern level for your read operations. Once the replica set is fully initialized, you can safely use other read concern levels.

Example

Let’s assume you have a replica set with three members: primary, secondary1, and secondary2. The replica set is currently initializing, and you try to perform a read operation with the “majority” read concern:

    db.collection.find().readConcern("majority")
  

This will result in the error message “cannot use non-local read concern until replica set is finished initializing”.

To fix this, you can either wait until the replica set finishes initializing or change the read concern to “local” temporarily:

    db.collection.find().readConcern("local")
  

Once the replica set initialization is complete, you can switch back to using the appropriate read concern level, such as “majority”.

Read more interesting post

Leave a comment