问题
We are using Mongo DB for our Java Java EE based application. We are using one primary and two secondary as part of cluster of Mongo DB The Mongo DB is continuously read / Updated / Write as part of our architecture.
Due to this we are getting Dirty Reads ( zero values ) When we do a fetch operation from Mongo DB.
TO solve this, I am trying to configure it in such a way that the read operation always fetches from primary itself and writes / updates can go to primary / secondary.
Please let me know if this is possible (always read from primary and use any thing primary/secondary for write/update)
And will it have any negative impact with such a design?
回答1:
Due to mongoDB uses staged writing in inserts, you can have lag in write operations even in a single node. http://docs.mongodb.org/manual/reference/glossary/#term-journal
Use write concern to avoid problems (with dirty data): http://api.mongodb.org/java/2.6/com/mongodb/WriteConcern.html
You can read more about write operations: http://docs.mongodb.org/manual/core/write-operations/
Which states that using writes without error check is not recommended for production.
回答2:
To exclude slave reads you have to set a corresponding ReadPreference. See documentation on Mongo connection class.
Something like this:
mongo.setReadPreference(ReadPreference.primary());
The documentation says, however, that by default all reads should already go to primary:
By default, all read and write operations will be made on the primary, but it's possible to read from secondaries by changing the read preference:
Are you sure you didn't turn slave reads on in the app?
来源:https://stackoverflow.com/questions/14575948/how-to-perform-read-operations-from-primary-only