title: Mongodb Is there a way to guarantee no-stale data read from Secondaries
body: <p>Problem statment:</p>
  
  <ul>
  <li>I m trying to distribute the load to secondaries.</li>
  <li>Read's are dominant.</li>
  </ul>
  
  <p>Read data from secondaries, that are not stale.</p>
  
  <p>Thing's i tried.
  -  read preference  as nearest
  -   write concern - majority(to ensure majority of the servers are in sync)</p>
  
  <pre><code>@Override
      @ConditionalOnProperty(value = "mars.mongodb.readWrite.concern.majority", havingValue = "true")
      @Bean
      public MongoTemplate mongoTemplate() throws Exception {
          MongoTemplate template = new MongoTemplate(this.mongoDbFactory(), this.mappingMongoConverter());
          template.setReadPreference(ReadPreference.nearest());
          template.setWriteConcern(WriteConcern.MAJORITY);
          return template;
      }
  </code></pre>
  
  <p>Some read's:</p>
  
  <p><a href="https://docs.mongodb.com/manual/core/causal-consistency-read-write-concerns/" rel="nofollow noreferrer">https://docs.mongodb.com/manual/core/causal-consistency-read-write-concerns/</a> </p>
  
  <p><a href="https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#sessions" rel="nofollow noreferrer">https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#sessions</a></p>