Configure Replica Set Tag Sets

A replica set member or members can be configured withtags:

  1. { "<tag1>": "<string1>", "<tag2>": "<string2>",... }

For read operations, you can specify a tag set in the readpreferences to help direct readoperations to members that have specific tag(s).

For write operations, you can use the tags to create a customwrite concern.

Use Tag Sets in Read Preference

If a replica set member or members are associated withtags, you can specify a tag set in the readpreference to target those members. A tagset is an array of documents, where each document contains the tag andvalue pair(s). The specifications are tried in order until a match isfound. Once found, that specification is used to find all eligiblematching members.

Note

You cannot specify a tag set when specifying read preference modeprimary.

For example, a replica set has the following replica setconfiguration (some of the fieldshave been omitted for brevity):

  1. {
  2. "_id" : "rs0",
  3. "version" : 1,
  4. "protocolVersion" : NumberLong(1),
  5. "writeConcernMajorityJournalDefault" : true,
  6. "members" : [
  7. { "_id" : 0, "host" : "mongodb0.example.net:27017", ..., "tags": { }, ... },
  8. { "_id" : 1, "host" : "mongodb1.example.net:27017", ..., "tags": { }, ... },
  9. { "_id" : 2, "host" : "mongodb2.example.net:27017", ..., "tags": { }, ... }
  10. ],
  11. "settings" : {
  12. ...
  13. }
  14. }
  • Add tags to the members.

Connect a mongo shell to the replica set and users.reconfig() to add tags to the members:

  1. conf = rs.conf();
  2. conf.members[0].tags = { "dc": "east", "usage": "production" };
  3. conf.members[1].tags = { "dc": "east", "usage": "reporting" };
  4. conf.members[2].tags = { "dc": "west", "usage": "production" };
  5. rs.reconfig(conf);
  • Verify the replica set configuration.

Run rs.conf() to verify the replica set configuration(some of the fields have been omitted for brevity). Thers.conf() returns a document similar to the following:

  1. {
  2. "_id" : "rs0",
  3. "version" : 2,
  4. "protocolVersion" : NumberLong(1),
  5. "writeConcernMajorityJournalDefault" : true,
  6. "members" : [
  7. {
  8. "_id" : 0,
  9. "host" : "mongodb0.example.net:27017",
  10. ...
  11. "tags" : {
  12. "dc": "east",
  13. "usage": "production"
  14. },
  15. ...
  16. },
  17. {
  18. "_id" : 1,
  19. "host" : "mongodb1.example.net:27017",
  20. ...
  21. "tags" : {
  22. "dc": "east",
  23. "usage": "reporting"
  24. },
  25. ...
  26. },
  27. {
  28. "_id" : 2,
  29. "host" : "mongodb2.example.net:27017",
  30. ...
  31. "tags" : {
  32. "dc": "west",
  33. "usage": "production"
  34. },
  35. ...
  36. }
  37. ],
  38. "settings" : {
  39. ...
  40. }
  41. }
  • Specify tag sets in the read preference.

To direct read operations to the secondaries tagged with a particulartag(s), in the mongo shell connected to the replica set,you can use the readPref() method to specify theread preference mode and the tagset. For example,

  • To direct read operations to the secondary tagged with both"dc": "east" and "usage": "production", include thefollowing tag set:
  1. db.collection.find({}).readPref( "secondary", [ { "dc": "east", "usage": "production" } ] )
  • To direct a read operation to the secondaries tagged with "dc":"east", and if not found, to secondaries tagged with"usage": "production", include the following tag set:
  1. db.collection.find({}).readPref( "secondary", [ { "dc": "east"}, { "usage": "production" } ] )

See also

Mongo.setReadPref()

Custom Multi-Datacenter Write Concerns

If a replica set member or members are associated withtags, you can configure the replica set’ssettings.getLastErrorModes setting to create a custom writeconcern.

Given a five member replica set with members in two data centers:

  • a facility VA tagged dc_va
  • a facility CA tagged dc_ca
  1. {
  2. "_id" : "rs0",
  3. "version" : 1,
  4. "protocolVersion" : NumberLong(1),
  5. "writeConcernMajorityJournalDefault" : true,
  6. "members" : [
  7. { "_id" : 0, "host" : "mongodb0.example.net:27017", ..., "tags": { }, ... },
  8. { "_id" : 1, "host" : "mongodb1.example.net:27017", ..., "tags": { }, ... },
  9. { "_id" : 2, "host" : "mongodb2.example.net:27017", ..., "tags": { }, ... }
  10. { "_id" : 3, "host" : "mongodb3.example.net:27017", ..., "tags": { }, ... }
  11. { "_id" : 4, "host" : "mongodb4.example.net:27017", ..., "tags": { }, ... }
  12. ],
  13. "settings" : {
  14. ...
  15. }
  16. }
  • Add tags to the replica set members.

Connect a mongo shell to the replica set and users.reconfig() to add tags to the members:

  1. conf = rs.conf();
  2. conf.members[0].tags = { "dc_va": "rack1"};
  3. conf.members[1].tags = { "dc_va": "rack2"};
  4. conf.members[2].tags = { "dc_ca": "rack1"};
  5. conf.members[3].tags = { "dc_ca": "rack2"};
  6. conf.members[4].tags = { "dc_va": "rack1"};
  7. rs.reconfig(conf);
  • Create a custom write concern.

In the replica set configuration, define a custom write concern inthe settings.getLastErrorModes setting. For example, thefollowing defines the custom write concern MultipleDC thatrequires the write to propagate to two members with differentdc_va tag values and one member with any dc_ca tag value.

  1. conf = rs.conf();
  2. conf.settings = { getLastErrorModes: { MultipleDC : { "dc_va": 2, "dc_ca": 1 } } };
  3. rs.reconfig(conf);

Note

The MultipleDC write concern is not satisfied if the writepropagates to two members with the same "dc_va" tag. Forexample, if the write has only propagated to members[0] andmembers[4], "dc_va": 2 is not satisfied since they havethe same tag value "rack1".

  • Use the custom write concern.

To use the custom write concern, pass in the write concern name tothe w Option in the write concern:

  1. db.collection.insert( { id: "xyz", status: "A" }, { writeConcern: { w: "MultipleDC" } } )