0

I have replication set up (localhost:27017(primary),localhost:27018(secondary),localhost:27019(arb))

I have set the readPrefrence as secondary in connection lever like below mongoInstance.setReadPreference(ReadPreference.secondary());

When i save some data through this connection will request rout to primary?

how do i confirm this?

I have seen DBCollection coll = getCollection();

         coll.getStats();
enter code here

In this "serverUsed":"localhost:27018" has shown.

rajesh
  • 35
  • 6
  • easiest way to confirm this is make sure there is no primary in your replica set and then try to successfully perform a write. watch it fail. – Asya Kamsky Dec 03 '13 at 03:39

2 Answers2

1

No. As the term says, readPreference only sets the preferred server to read from in the cluster. You can only write to primary servers in a replica set.

Munim
  • 6,310
  • 2
  • 35
  • 44
  • this will be done automationcally right? means write operation will go to primary right? – rajesh Dec 02 '13 at 12:44
  • Always write operations go to primary. – Maximiliano Rios Dec 02 '13 at 12:47
  • when i see the DBCollection coll = getCollection(); coll.getStats() I was seeing the output like below { "serverUsed" : "localhost/127.0.0.1:27017" , "ns" : "test_db.Customer" , "count" : 4 , "size" : 320 , "avgObjSize" : 80.0 , "storageSize" : 4864 , "numExtents" : 1 , "nindexes" : 1 , "lastExtentSize" : 4864 , "paddingFactor" : 1.0 , "flags" : 1 , "totalIndexSize" : 8192 , "indexSizes" : { "_id_" : 8192} , "ok" : 1.0} what does mean serverUsed ? – rajesh Dec 02 '13 at 12:52
  • @rajesh not just automatic. It is impossible to write on the primary. it is not a configuration which can be set or changed. I am not sure about what serverused means because I never used the java mongo api, but it seems like that is the server from which the stats were fetched. – Munim Dec 02 '13 at 13:09
  • I think getStats is mapped to collection stats function in the shell. In this case I think you're obtaining stats from the primary server. Due to stats is harmless and doesn't update data I think it's pretty normal to get it from the master. – Maximiliano Rios Dec 02 '13 at 13:14
0

It doesn't read from a primary, read preference secondary means exactly this, it doesn't disturb primary ever.

Maximiliano Rios
  • 2,196
  • 2
  • 20
  • 34
  • Anyway, keep in mind you can overwrite this behavior always using a read preference in a find operation. Might be you wanted to use secondaryPreferred which is slightly different to secondary. Secondary is always mandatory to the secondary, secondaryPreferred reads from secondary and in case there's not a member available reads from primary. – Maximiliano Rios Dec 02 '13 at 12:46
  • Any way write operations would go to primary only right even if we set the readpreference as secondary? – rajesh Dec 02 '13 at 12:46
  • Write operations go always to primary, you cannot send them to a secondary. The rest of the members of a replica set only receive reads (in case they're enabled of course) and they contain data (Remember there're different replica set members) – Maximiliano Rios Dec 02 '13 at 12:48
  • when i see the DBCollection coll = getCollection(); coll.getStats() I was seeing the output like below { "serverUsed" : "localhost/127.0.0.1:27017" , "ns" : "test_db.Customer" , "count" : 4 , "size" : 320 , "avgObjSize" : 80.0 , "storageSize" : 4864 , "numExtents" : 1 , "nindexes" : 1 , "lastExtentSize" : 4864 , "paddingFactor" : 1.0 , "flags" : 1 , "totalIndexSize" : 8192 , "indexSizes" : { "_id_" : 8192} , "ok" : 1.0} what does mean serverUsed ? – rajesh Dec 02 '13 at 12:54