Databases Reference
In-Depth Information
Other replication state stored on the master includes a list of its slaves. (Slaves perform
a handshake using the handshake command when they connect to the master.) This list
is stored in the slaves collection:
> db.slaves.find()
{ "_id" : ObjectId("4c1287178e00e93d1858567c"), "host" : "127.0.0.1",
"ns" : "local.oplog.$main", "syncedTo" : { "t" : 1276282710000, "i" : 1 } }
{ "_id" : ObjectId("4c128730e6e5c3096f40e0de"), "host" : "127.0.0.1",
"ns" : "local.oplog.$main", "syncedTo" : { "t" : 1276282710000, "i" : 1 } }
Slaves also store state in the local database. They store a unique slave identifier in the
me collection, and a list of sources , or nodes, that they are slaving from, in the sources
collection:
> db.sources.find()
{ "_id" : ObjectId("4c1287178e00e93d1858567b"), "host" : "localhost:27017",
"source" : "main", "syncedTo" : { "t" : 1276283096000, "i" : 1 },
"localLogTs" : { "t" : 0, "i" : 0 } }
Both the master and slave keep track of how up-to-date a slave is, using the timestamp
stored in "syncedTo" . Each time the slave queries the oplog for new operations, it uses
"syncedTo" to specify which new operations it needs to apply or to find out if it is out
of sync.
Blocking for Replication
MongoDB's getLastError command allows developers to enforce guarantees about
how up-to-date replication is by using the optional "w" parameter. Here we run a
getLastError that will block until at least N servers have replicated the last write
operation:
> db.runCommand({getLastError: 1, w: N });
If N is not present or is less than two, the command will return immediately. If N is
two, the master won't respond to the command until at least one slave has replicated
the last operation. (The master itself is included toward N .) The master uses the
"syncedTo" information stored in local.slaves to track how up-to-date each slave is.
When specifying "w" , getLastError takes an additional parameter, "wtimeout" , which
is a timeout in milliseconds. This allows the getLastError to time out and return an
error before the last operation has replicated to N servers. (By default the command has
no timeout.)
Blocking for replication will cause write operations to slow down significantly, partic-
ularly for large values of "w" . In practice, setting "w" to two or three for important
operations will yield a good combination of efficiency and safety.
 
Search WWH ::




Custom Search