Ayush Nawani
Ayush Nawani

Reputation: 664

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

Upvotes: 0

Views: 398

Answers (2)

Ali Tou
Ali Tou

Reputation: 2205

Let's suppose you always use primary read preference and you have 2 members that are electable for being primary.

Since all of your reads are taking place in primary instance, its cache is heavily populated and since your other electable member doesn't receive any reads, its cache can be considered to be empty.

Using mirrored reads, the primary will send a portion (in your question 100%) of read requests to that secondary as well, to make her familiar with the pattern of read queries and populate its cache.

Suddenly a disaster occurs and current primary goes down. Now your new primary has a pre-warmed cache that can respond to queries as fast as the previous primary, without shocking the system to populate its cache.

Regarding the impact of sampling rate, MongoDB folks in their blog post introducing this feature stated that increasing the sampling rate would increase load on the Replica Set. My understanding is that you may already have queries with read preference other than primary that makes your secondary instance already busy. In this case, these mirrored reads can impact on the performance of your secondary instance. Hence, you may not want to perform all primary reads again on these secondaries (The repetition of terms secondary and primary is mind blowing!).

The story with secondaryPreferred reads is different and you're querying secondaries for data, unless there is no secondary.

Upvotes: 1

D. SM
D. SM

Reputation: 14520

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).

Upvotes: 1

Related Questions