tuk
tuk

Reputation: 6842

Write Majority and Secondary Read Preference on Mongo

Let's say I am having one Primary (A) & two secondary (B, C). If I am doing write using write majority. Can some one please explain my below doubts:-

  1. Let's say a write was done using majority and it updated the data in A & B and the write did not yet propagate to C. At this time if a read comes for the same data using secondary or secondary preferred will the query be served from B which is having the latest data or mongo cannot gurantee this and the read may return a stale data from C.
  2. Let's say a write was done using majority again and let's say the write was done on A and then a write is on progress in one of the secondary B. If a read comes at that time will the read be blocked or it will serve a stale data from C?
  3. Let's say I have taken out the secondary C and the same case is in progress as we mentioned in the above case. Will the read from secondary B be blocked till the write is complete on B or the read will not be blocked and a stale data will be served from B?

Environment

Upvotes: 0

Views: 1976

Answers (1)

notionquest
notionquest

Reputation: 39156

Mongodb replication process is async to secondary. If the read concern is set as 'majority', you may read the stale data. Basically, this means you have set the read preference as Eventual consistency.

If the read concern is set as "local", you will get the latest data from primary.

Please note that readConcern level of "majority" can be used in WiredTiger storage engine only. The WiredTiger storage engine is append only storage engine and doesn't use in place updates. There is no locks and offers document level concurrency.

Read concern = "majority"

The query returns the instance’s most recent copy of data confirmed as written to a majority of members in the replica set.

To use a read concern level of "majority", you must use the WiredTiger storage engine and start the mongod instances with the --enableMajorityReadConcern command line option (or the replication.enableMajorityReadConcern setting if using a configuration file).

Question 1: The Mongo does not guarantee that the read will be served from the secondary in which the data is written?

Answer 1: MongoDB doesn't guarantee this. The selection of the secondary depends on the following:-

When you select non-primary read preference, the driver will determine which member to target based on various factors. Refer this link. Read preference mechanics member selection

Question 2: The reading will never be blocked even if a write is on progress on the same data?

Answer 2: Reading will not be blocked. However, you may read some stale data.

Reads may miss matching documents that are updated during the course of the read operation.

Concurrency locking what isolation guarantees does MongoDB provide

Upvotes: 1

Related Questions