Sunday, 24 June 2012

Replication in MongoDB: Replica Sets

Replication:
Replica Sets:

Overview :

      • Consists of a set of nodes where one of the node is primary and rest are secondary nodes and all of these consists of copy of each other.
      • By default, read/writes are directed to primary. Setting slaveOkay bit for secondary, redirects the read operations to secondary thereby balancing the load.
      • The write operations are done restricted on only one machine(primary) and propagated to secondaries, for the simple reason, to maintain consistency.
      • Follows cluster-wide commit protocol : After a client writes to the database, only after a majority of secondaries have replicated the updated data onto them, a commit is issued to the client.


Replication Process :

      • A client writes to the primary. The operations are written to oplog.
        • Oplog is a fixed size collection(capped collection) that saves the operations and a timestamp for that operation.
        • The old operations are replaced by the new ones after the old ones age out. (FIFO)
      • Each secondary copies the operation to their own oplog and applies the operations to their data.
      • Why do we require Oplog?
        • If a secondary falls behind for a short period of time, it will make a best effort to “catch-up”.
        • As oplog contains a sequential set of operations applied on primary, it is easy to catch-up with the primary because the secondary is aware of the operations that took place in primary and applies it accordingly.
        • If a secondary fall behind for a long period of time such that old operations are overwritten, then the secondary cant restore its consistency, as some of the operations goes missing. In this case, the secondary is said to have stale data and it stops replicating.