Custom write concerns

We can also identify our replica set members with different tags (that is, reporting, East Coast Servers, and HQ servers) and specify a custom write concern per operation, as follows:

  1. Use the usual procedure for connecting to the primary via the mongo shell, as follows:
> conf = rs.conf()
> conf.members[0].tags = { "location": "UK", "use": "production", "location_uk":"true" }
> conf.members[1].tags = { "location": "UK", "use": "reporting", "location_uk":"true" }
> conf.members[2].tags = { "location": "Ireland", "use": "production" }
  1. We can now set a custom write concern, as follows:
> conf.settings = { getLastErrorModes: { UKWrites : { "location_uk": 2} } }
  1. After applying this, we use the reconfig command:
> rs.reconfig(conf)
  1. We can now start by setting writeConcern in our writes, as follows:
> db.mongo_books.insert({<our insert object>}, { writeConcern: { w: "UKWrites" } })

This means that our write will only be acknowledged if the UKWrites write concern is satisfied, which, in turn, will be satisfied by at least two servers, with the tag location_uk verifying it. Since we only have two servers located in the UK, we can make sure that with this custom write concern, we have written our data to all of our UK-based servers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset