In the previous article, we saw the five different types of consistency levels ranging from Strong to Eventual. In Cosmos DB, to set the consistency level, we start by setting the default one from all the available consistency levels for every account entirely. This default consistency level will be applied to all the operations inside the accounts under the databases. This will be set till the time we change it and even if we do, we can go back and activate it any time.
To set the consistency level, we go to Explorer -> Settings -> Default Consistency.

As you can see it is set to Default Consistency by default. You can set any of these consistency levels from here.
Suppose, you choose Bounded staleness, you will see the options to set the acceptable level of freshness, which means by setting the maximum lag of operations i.e. the no. of operations by which you are lagging behind and the maximum lag in terms of days, hours, minutes, and seconds.
It looks like this:

We can set these control values and in the above picture, we are setting the no. of operations by which are allowed to lag behind as 100 and time to be 5 seconds and not more than that. We definitely do not want stale results more than these set control properties.
Another thing that we can do is override the default consistency level for each particular query but by only weakening the consistency level. This means that if your default consistency is session, then you can set the consistency level per query to only consistent prefix or eventual. Not the higher ones.
Similarly, if the set consistency for the entire account is Strong, you can set the consistency per query to any of the consistency levels because all other consistency levels are weaker than this one.
This option is better to use when we set it to Strong for no dirty reads with higher latency and then we can set it to lower consistency levels for particular queries for which we want the data as quickly as possible and it is totally fine if it is stale data too.
Let us see how do we do this.
Step 1. Use the .NET sdk, go to the DocumentClient class
Step 2. In that class, choose the overloaded constructor that accepts a consistency level enum.
It should look like:
new DocumentClient(new Uri(endpoint), masterKey, connectionPolicy, ConsistencyLevel._______________)
This blank can have any of the five available consistency levels:
– BoundedStaleness
– ConsistentPrefix
– Eventual
– Session
– Strong