Introduction to MongoDb with .NET part 43: the read and write preference in the .NET driver

Introduction

In the previous post we discussed the ideas of read preference and read concern in MongoDb. Both play a role in multi-server scenarios where we can give hints to MongoDb where we want to read the data from: the primary server, one of the secondaries, the one with the shortest response time etc.

In this post we’ll see how to set the read and write concern options in the MongoDb .NET driver.

Read preference and read concern in the driver

There are multiple levels where we can set both the read and write concern. We can set it at the client level as follows:

MongoClientSettings settings = new MongoClientSettings();
settings.ReadConcern = new ReadConcern(ReadConcernLevel.Majority);
Tag tag = new Tag("tagName", "tagValue");
TagSet tagSet = new TagSet(new List<Tag>() { tag });
settings.ReadPreference = new ReadPreference(ReadPreferenceMode.Secondary, new List<TagSet>() { tagSet});
WriteConcern writeConcernWithNumberOfServers = new WriteConcern(2, TimeSpan.FromSeconds(60), false, false);
WriteConcern writeConcerntWithMajority = new WriteConcern("majority", TimeSpan.FromSeconds(60), false, false);
settings.WriteConcern = writeConcerntWithMajority;
MongoClient mongoClient = new MongoClient(settings);

The above example shows a number of things:

  • The MongoClientSettings object is the entry point to set various settings for the read concern, read preference and write concern options
  • Both ReadConcernLevel and ReadPreferenceMode are enumerations which have values that we saw in the previous post, e.g. Majority for the read concern and Secondary for the read preference mode
  • We also mentioned that we can specify a tag set for the machines we want to read from. The Tag and TagSet objects help to set those options in code
  • WriteConcern can have either and integer or a string as the first parameter. You’ll recall from the post on the write concern what an integer and the mode “majority” mean. Next there is a timeout for the write operation. The first boolean value stands for the “fsync” parameter which we haven’t discussed before and which can safely be put to false. The last parameter, which is also a boolean type is the journal option, i.e. whether we want to wait for the write operations to be saved in the journal

Setting these options on the mongo client level helps you create various mongo clients that can all have differing read and write concerns.

The next level of customisation is at the database level using the MongoDatabaseSettings object:

ModelContext _modelContext = new ModelContext();
MongoDatabaseSettings dbSettings = new MongoDatabaseSettings();
//structure identical to MongoClientSettings
.
.
_modelContext.Database = _modelContext.Client.GetDatabase(configurationRepository.GetConfigurationValue("DemoDatabaseName", "model"), dbSettings);

MongoDatabaseSettings has the same type of properties as MongoClientSettings for the read and write concern options so I didn’t provide any new code example here.

We can also provide our settings on the collection level using the MongoCollectionSettings object:

MongoCollectionSettings collectionSettings = new MongoCollectionSettings();
//structure identical to MongoClientSettings
.
.
IMongoCollection<RestaurantDb> Database.GetCollection<RestaurantDb>(ConfigurationRepository.GetConfigurationValue("RestaurantsCollectionName", "restaurants"), collectionSettings);

Again, the read and write preference options can be set in the same way as in the case of MongoClientSettings. With MongoCollectionSettings we can have different read and write preference settings for each collection in the database.

Finally we can specify our read and write preferences directly in the collection string. This post explains exactly how to to it but here is an example for the read preference:

mongodb://example1.com,example2.com,example3.com/?readPreference=secondary

…and here’s another example for the write concern:

mongodb://example1.com,example2.com,example3.com/?w=2&wtimeoutMS=2000

This was the last post in the series on MongoDb with .NET. The next post will summarise what we’ve learnt.

You can view all posts related to data storage on this blog here.

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a comment

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.