Configure Replica Set Tag Sets
A replica set member or members can be configured withtags
:
- { "<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):
- {
- "_id" : "rs0",
- "version" : 1,
- "protocolVersion" : NumberLong(1),
- "writeConcernMajorityJournalDefault" : true,
- "members" : [
- { "_id" : 0, "host" : "mongodb0.example.net:27017", ..., "tags": { }, ... },
- { "_id" : 1, "host" : "mongodb1.example.net:27017", ..., "tags": { }, ... },
- { "_id" : 2, "host" : "mongodb2.example.net:27017", ..., "tags": { }, ... }
- ],
- "settings" : {
- ...
- }
- }
- Add tags to the members.
Connect a mongo
shell to the replica set and users.reconfig()
to add tags to the members:
- conf = rs.conf();
- conf.members[0].tags = { "dc": "east", "usage": "production" };
- conf.members[1].tags = { "dc": "east", "usage": "reporting" };
- conf.members[2].tags = { "dc": "west", "usage": "production" };
- 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:
- {
- "_id" : "rs0",
- "version" : 2,
- "protocolVersion" : NumberLong(1),
- "writeConcernMajorityJournalDefault" : true,
- "members" : [
- {
- "_id" : 0,
- "host" : "mongodb0.example.net:27017",
- ...
- "tags" : {
- "dc": "east",
- "usage": "production"
- },
- ...
- },
- {
- "_id" : 1,
- "host" : "mongodb1.example.net:27017",
- ...
- "tags" : {
- "dc": "east",
- "usage": "reporting"
- },
- ...
- },
- {
- "_id" : 2,
- "host" : "mongodb2.example.net:27017",
- ...
- "tags" : {
- "dc": "west",
- "usage": "production"
- },
- ...
- }
- ],
- "settings" : {
- ...
- }
- }
- 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:
- 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:
- db.collection.find({}).readPref( "secondary", [ { "dc": "east"}, { "usage": "production" } ] )
See also
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
taggeddc_va
- a facility
CA
taggeddc_ca
- {
- "_id" : "rs0",
- "version" : 1,
- "protocolVersion" : NumberLong(1),
- "writeConcernMajorityJournalDefault" : true,
- "members" : [
- { "_id" : 0, "host" : "mongodb0.example.net:27017", ..., "tags": { }, ... },
- { "_id" : 1, "host" : "mongodb1.example.net:27017", ..., "tags": { }, ... },
- { "_id" : 2, "host" : "mongodb2.example.net:27017", ..., "tags": { }, ... }
- { "_id" : 3, "host" : "mongodb3.example.net:27017", ..., "tags": { }, ... }
- { "_id" : 4, "host" : "mongodb4.example.net:27017", ..., "tags": { }, ... }
- ],
- "settings" : {
- ...
- }
- }
- Add tags to the replica set members.
Connect a mongo
shell to the replica set and users.reconfig()
to add tags to the members:
- conf = rs.conf();
- conf.members[0].tags = { "dc_va": "rack1"};
- conf.members[1].tags = { "dc_va": "rack2"};
- conf.members[2].tags = { "dc_ca": "rack1"};
- conf.members[3].tags = { "dc_ca": "rack2"};
- conf.members[4].tags = { "dc_va": "rack1"};
- 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.
- conf = rs.conf();
- conf.settings = { getLastErrorModes: { MultipleDC : { "dc_va": 2, "dc_ca": 1 } } };
- 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:
- db.collection.insert( { id: "xyz", status: "A" }, { writeConcern: { w: "MultipleDC" } } )