First of all i am not discarding anything of @Asya Kamsky
. But i would like to write down something about replica Set : Mater vs Slave.
As per MongoDB documented source from the MongoDB the Definite Guide
Master-slave replication is the most general replication mode supported by MongoDB
.This mode is very flexible and can be used for backup
, failover
, read scaling
. The basic setup is to start a master node
and one or more slave nodes
, each of which knows the address of the master
. To start the master
, run mongod --master
. To start a slave
, run mongod --slave --source master_address
, where master_address
is the address of the master node
that was just started.
Suppose that First, create a directory for the master
to store data in and choose a port
(10000
):
$ mkdir -p ~/dbs/master
$ ./mongod --dbpath ~/dbs/master --port 10000 --master
Now set up the slave
, choosing a different data directory and port. For a slave, you also need to tell it who its master is with the --source
option:
$ mkdir -p ~/dbs/slave
$ ./mongod --dbpath ~/dbs/slave --port 10001 --slave --source localhost:10000
All slaves must be replicated from a master node
. There is currently no mechanism for replicating from a slave (daisy chaining), because slaves do not keep their own oplog
.
There is no explicit limit on the number of slaves
in a cluster
, but having a thousand slaves querying a single master will likely overwhelm the master node.
Note : If you slave
off of two different masters with the same collections, MongoDB
will attempt to merge them, but correctly doing
so is not guaranteed. If you are using a single slave with multiple
different masters, it is best to make sure the masters use different
namespaces
.
A replica set
is basically a master-slave
cluster with automatic failover
. The biggest difference between a master-slave cluster and a replica set is that a replica set does not have a single master: one is elected by the cluster and may change to another node if the current master goes down. However, they look very similar: a replica set always has a single master node
(called a primary) and one or more slaves (called secondaries
).
Note: When the primary server goes down, the secondary server will become master
.
The record of operations kept by the master is called the oplog, short for operation log. The oplog
is stored in a special database called local, in the oplog.$main
collection. Each document in the oplog
represents a single operation performed on the master server.
In Master-Slave in replica set in Failover and Primary Election scenario depends upon the condition and configuration between Master & Slave like that
- When the
primary
server goes down
, the secondary
server will become
master
.
- If the
primary
goes down, the highest-priority
servers will compare
how up-to-date
they are.
- If the
Primary Server
goes down among the Secondaries the highest-priority most-up-to-date
server
will become the new primary
.
Using slaves
to scale reads
in MongoDB
is that replication is asynchronous
. This means that when data is inserted or updated on the master
, the data on the slave
will be out-of-date
momentarily. This is important to consider if you are serving some requests using queries to slaves
.
Note: you should be sure never to write
to any database on the slave
that is being replicated
from the master
. The slave
will not
revert any such writes
in order to properly mirror
the master
. The
slave
should also not have any of the databases that are being
replicated when it first starts up. If it does, those databases will
not ever be fully synced
but will just update with new operations
.