Verifying Propagation of Writes with getLastError

Please read the getLastError Command page first.

A client can await a write operation's replication to N servers (v1.6+). Use the getLastError Command with the parameter w:

// examples:

db.runCommand( { getlasterror : 1 , w : 2 } )

db.runCommand( { getlasterror : 1 , w : "majority" } )

db.runCommand( { getlasterror : 1 , w : "majority", j:true } )

db.runCommand( { getlasterror : 1 , w : "majority", wtimeout : 5000 } )

If w is not set, or equals one, the command may return almost immediately, implying the data is on one server (itself). If w is 2, then the data is on the current server and 1 other server (a secondary).

v2.0+ supports a "majority" value for w which indicates "await the data reaching a majority of members". This can be quite useful as a "cluster wide commit" of the write has occurred once the write has reached a majority of the (non-arbiter) members of a set.

The optional wtimeout parameter allows one to time out after a certain number of milliseconds (perhaps then returning an error to a user).

> db.runCommand({getlasterror : 1, w : 40, wtimeout : 3000})
{
        "err" : null,
        "n" : 0,
        "wtimeout" : true,
        "waited" : 3006,
        "errmsg" : "timed out waiting for slaves",
        "ok" : 0
}

Note: the current implementation returns when the data has been delivered to w servers.  Future versions will provide more options for delivery vs. say, physical fsync at the server.

See also replica set configuration for information on how to change the getlasterror default parameters.

See Also


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.

blog comments powered by Disqus