Search code examples
mongodbmongodb-querymongodb-replica-set

MongoDB 4.4.1 mirroredRead vs secondaryPreferred readPerence


In MongoDB 4.4.1 there is mirroredRead configuration which allows primary to forward read/update requests to secondary replicaset.

How it is different from secondaryPreferred readPerence when its sampling rate is set to 1.0? What is the use-case of mirroredRead?

reference - https://docs.mongodb.com/manual/replication/#mirrored-reads-supported-operations


Solution

  • What is the use-case of mirroredRead?

    This is described in the documentation you linked:

    MongoDB provides mirrored reads to pre-warm the cache of electable secondary members

    If you are not familiar with cache warming, there are many resources describing it, e.g. https://www.section.io/blog/what-is-cache-warming/.

    A secondary read:

    • Is sent to the secondary, thus reducing the load on the primary
    • Can return stale data

    A mirrored read:

    • Is sent to the primary
    • Always returns most recent data

    mirroredRead configuration which allows primary to forward read/update requests to secondary replicaset.

    This is incorrect:

    • A mirrored read is not applicable to updates.
    • The read is not "forwarded". The primary responds to the read using its local data. Additionally, the primary sends a read request to one or more secondaries, but does not receive a result of this read at all (and does not "forward" the secondary read result back to the application).