mongodbdistributed-systemreplicasetaws-documentdbmongodb-replica-set

How to read from specific instance of a documentdb cluster


I am having a replica lag issue with documentDB. Where I am trying to write some data from a collection and read the same at the same time. But because I am using a distributed system, I am not able to read the already written data from the replica sets.

Here's the cluster design.

enter image description here.

So, is it possible to read from the primary instance in nodejs or is it possible to read from a specific instance?


Solution

  • How big is the replication lag? It might be worth investigating the cause for the lag, maybe bigger instances are needed or queries have to be optimized. If your application can't tolerate eventual consistency or read after write consistency is required, then use readPreference: primaryPreferred to instruct the driver to read from the Primary instance when available. However, in this case, the replicas will not be used to scale horizontally the read traffic.

    Amazon DocumentDB has other endpoints too:

    The best practice is to connect as replica set, using the readPreference parameter to adjust the preference. Instance endpoints can be useful when, for example, there's a need for large analytics queries and a bigger instance is deployed, temporarily, to run them.