M103: Read Preference Quiz: is `primaryPreferred` likely stale?

I can’t seem to find any similar topic, so open a new question here.
Ch2 Read Preference quiz solution includes primaryPreferred read concern as one of the answers.
I’m not sure if it really results in stale data in return.

  1. if a primary is online, read will come from the primary > fresh data
  2. if a primary is down, there will be no write operation to the cluster then it seems up to the secondary chosen for read.
    2-1. if the secondary data is up to date, the read is fresh
    2-2. if the secondary has yet to replicate data, the read will be stale
    Can it be stale because choosing the secondary is random?
    I’m curious what is the right protocol for routing read to secondaries.
  • random if not specified by nearest (then, it can be stale)
  • or the most up-to-date secondary (then, primaryPreferred will not return stale read)

I’d appreciate clarification.

If we look at the documentation for Read Preference we see the following for primaryPreferred:

In most situations, operations read from the primary member of the set. However, if the primary is unavailable, as is the case during failover situations, operations read from secondary members that satisfy the read preference’s maxStalenessSeconds and tag set lists.

A write could happen that hasn’t propagated to the secondary members and then the primary could go offline before a read request was made. In that case the read would be routed to a secondary member and you would get a stale read in that case. Pretty much anytime there is a chance you could read from a secondary member, you could have a stale value returned.

1 Like

Thanks Doug for clarification.
One more question here.
If there’re several secondaries whose lag is less than maxStalenessSeconds, does the client choose the target read secondary in a random manner or the one with the most up-to-date lag?

I believe, although not sure on this, that it will read from the secondary that is most up to date (lowest lag).

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.