Mongo ReplicaSet explain
i'm new in mongo replication.
I've got two server and i set a primary on one server and secondary to the other.
I'm developing a web application using spring and spring-data and i use that xml configuration to access to mongo:
<mongo:mongo id="mongoConnection" replica-set="IP-Primary:27017,IP-Secondary:27017"> <mongo:options connections-per-host="100" threads-allowed-to-block-for-connection-multiplier="50" connect-timeout="10000" max-wait-time="50000" slave-ok="true" auto-connect-retry="true" write-number="1" write-timeout="0" write-fsync="false"/> </mongo:mongo> <beans:bean id="mongoWriteConcern" class="com.mongodb.WriteConcern"> <beans:constructor-arg type="int" name="w" value="2"/></beans:bean> <beans:bean id="mongoCredentials" class="org.springframework.data.authentication.UserCredentials"> <beans:constructor-arg name="username" value="xxx" /> <beans:constructor-arg name="password" value="yyy" /> </beans:bean> <beans:bean id="myTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <beans:constructor-arg ref="mongoConnection" /> <beans:constructor-arg name="databaseName" value="dbName" /> <beans:constructor-arg name="userCredentials" ref="mongoHotelCredentials" /> <beans:property name="writeConcern" ref="mongoWriteConcern"/> </beans:bean>
Can please someone explain me what is the flow on the replica set?
It will write first on primary and automatically the primary write to secondary?
Does the framework will check for success connection to each database and "elect" a primary if the current primary is unavailable ( so elect secondary as primary)? If yes what happen when primary come back again?
Basic flow of replication:
Your application (client) writes to primary of the replica set. Write operations are stored in a log file (oplog), and oplog replicates to secondaries either thru 'chained replication' (default) or replicates from closest member, configurable, read here http://docs.mongodb.org/manual/tutorial/manage-chained-replication/.
"It will write first on primary and automatically the primary write to secondary?"
Writes go to primary, store in oplog, oplog replicates to secondary, then writes apply to secondary. It is automatic, but you have to take network latency into consideration. Latency factors include distance between primary node and secondary node, and also barriers like firewall. There are many things you can toy with MongoDB's replication, e.g. higher writeConcern setting to increase consistency (at cost of throughput), configure delayed secondary to act as backup, election priority for each secondary (useful for multi-datacenter), etc.
"Does the framework will check for success connection to each database and "elect" a primary if the current primary is unavailable ( so elect secondary as primary)? If yes what happen when primary come back again?"
There is 'heartbeat' between members of a replica set. If a member goes down, within 10-30 seconds other members will detect the loss heartbeat and mark that member as unavailable. If the down member is a primary, election process will occur and a new primary gets elected. It is important to keep the 'odd' number of members (3, 5, 7... up to 12 members with 7 voting members) http://docs.mongodb.org/manual/core/replica-set-architecture-four-members/
When that downed primary comes back it will become a secondary, with unprocessed operations (operations not applied to secondaries) stored in 'rollback' folder that have to be manually applied back to the database, or discarded.