Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding WriteConcern in MongoDB C#

I have been reading up on Write Concern in MongoDB. I understand that there are several levels that determine the level of guarantee that a write operation succeeded, with a performance trade-off for the higher you set this level. However, I am working in a C# environment, and I am trying to figure out how to use Write Concern there and what levels work best for certain situations. I already figured out how to collect the result of the check with a WriteConcernResult object, I am mainly interested in the levels themselves.

These are my questions:

How do I set the Write Concern level in C# for specific writes?

This answer suggests using the connection string, but this looks like a global setting, which I don't want, since some of the write operations I will be using are more "important" than others and I don't want to kill the performance. I noticed there's a WriteConcern class but the documentation isn't very detailed about its use (it's under MongoDB.Driver Namespace in the documentation).

In particular, how do I set it to "Journaled" or "Replica Acknowledged", seeing as it's "Acknowledged" by default?

What types of issues could get past the Write Concern check for each level?

For example: system crashes, power failures, network connection issues, etc. I am especially interested in something sneaking by that is not easily detected, as power failures and the like are very noticeable and we could estimate the time interval where operations may have failed and react accordingly.

like image 637
CynicalProgrammer Avatar asked Oct 30 '14 18:10

CynicalProgrammer


People also ask

What is MongoDB Writeconcern?

Write concern describes the level of acknowledgment requested from MongoDB for write operations to a standalone mongod or to replica sets or to sharded clusters. In sharded clusters, mongos instances will pass the write concern on to the shards.

What is read and write concern in MongoDB?

When it comes to writing data, a MongoDB write concern allows you to set the level of acknowledgment for a desired write operation. Likewise, the read concern allows you to control the consistency and isolation properties of the data read from your replica sets.

What is replica set in MongoDB?

A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments. This section introduces replication in MongoDB as well as the components and architecture of replica sets.

What is acknowledged in MongoDB?

If acknowledged is true , it means the MongoDB server has acknowledged the write concern. If false , it means the write concern is not enabled. The MongoDB server will still write data, but there's no guarantee that data will persist on the disk.


2 Answers

For 2.x c# driver, you can use write concern in the following way:

var collection = db.GetCollection<Record>(collectionName)
    .WithWriteConcern(new WriteConcern(
        w: 1,
        wTimeout: default(TimeSpan?),
        fsync: true,
        journal: false));

then any updates to the db using this collection will use the passed write concern.

collection.InsertOne(...);
collection.ReplaceOne(...);
collection.UpdateMany(...);
and so on

There are several predefined write concerns e.g.

for very fast but unreliable updates:

var collection = db.GetCollection<Record>(collectionName)
    .WithWriteConcern(WriteConcern.Unacknowledged);

or for WriteConcern which is similar to the default (w=1)

var collection = db.GetCollection<Record>(collectionName)
    .WithWriteConcern(WriteConcern.W1);

or for acknowledge of majority members of the replica set

var collection = db.GetCollection<Record>(collectionName)
    .WithWriteConcern(WriteConcern.WMajority);

for details and more options please see the documentation here: https://mongodb.github.io/mongo-csharp-driver/2.7/apidocs/html/T_MongoDB_Driver_WriteConcern.htm

like image 128
Aviko Avatar answered Oct 20 '22 00:10

Aviko


The operations in the MongoDB C# driver have overloads that accept a WriteConcern that you can get by using the class constructor or using a predefined static property:

var writeConcern = WriteConcern.W4;
writeConcern.Journal = true;
writeConcern.WTimeout = TimeSpan.FromMilliseconds(100);
new MongoClient().GetServer().GetDatabase("").GetCollection("").Insert(null, null, writeConcern);

This for example requires 3 replicas on top of the primary, hence W4, the Journal flag is turned on and the wtimeout is set to 100 ms.

like image 33
i3arnon Avatar answered Oct 20 '22 00:10

i3arnon