question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RU Consumption is Unaffected when using Gateway Cache and Consistency Level Session

See original GitHub issue

Describe the bug Using Microsoft.Azure.Cosmos version 3.23.0-preview, when the CosmosClient is generated if it uses CosmosClientOptions.ConsistencyLevel = ConsistencyLevel.Session duplicate queries will not reduce the RU cost whatsoever, however it will reduce the RU cost to 0 if using ConsistencyLevel.Eventual.

To Reproduce Create a CosmosClient pointing to a CosmosDB resource with gateway caching enabled with the following options:

new CosmosClient({yourUri},
    {yourAuthKey},
    new CosmosClientOptions
    {
	    ConnectionMode = ConnectionMode.Gateway,
	    ConsistencyLevel = ConsistencyLevel.Session
    });

Then write a query against any database and container. Run the query more than once and check the FeedResponse.RequestCharge every time. Every time the query is executed RUs are charged against the account. Now create a ComosClient with the following options:

new CosmosClient({yourUri},
    {yourAuthKey},
    new CosmosClientOptions
    {
	    ConnectionMode = ConnectionMode.Gateway,
	    ConsistencyLevel = ConsistencyLevel.Eventual
    });

Repeat the steps above and you’ll see that after the first request the RU charge drops down to 0 for subsequent requests.

Expected behavior According to this documentation: https://docs.microsoft.com/en-us/azure/cosmos-db/how-to-configure-integrated-cache#adjust-request-consistency

The integrated cache should be utilized if the ConsistencyLevel is Eventual or Session

Actual behavior The integrated Cache appears to be being bypassed when the ConsistencyLevel is Session

Environment summary SDK Version: 3.23.0-preview OS Version Windows

Additional context Additionally, you may want to consider not exposing CosmosClient.ClientOptions.ConnectionMode and CosmosClient.ClientOptions.ConsistencyLevel because if those variables are not set in the constructor for CosmosClient they seem to be ignored and that can be quite confusing.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
robsierracommented, Nov 24, 2021

I will email you our CosmosDB name but I’m including the sample code here for reference in the event others experience this issue.

async Task Main()
{
	var client = new CosmosClient("{connectionUrl}",
		"{authKey}",
		new CosmosClientOptions
		{
			ConnectionMode = ConnectionMode.Gateway,
			ConsistencyLevel = ConsistencyLevel.Session
		});


	Database database = client.GetDatabase("{dbName}");
	Container container = database.GetContainer("{containerName}");

	double totalRU = 0;
	var cmd = "select * from T where T.Timestamp > '2021-11-10'";
	var query = new QueryDefinition(cmd);
	using (FeedIterator<dynamic> feedIterator = container.GetItemQueryIterator<dynamic>(query,
	requestOptions: new QueryRequestOptions
	{
		// Only available using preview sdk
		DedicatedGatewayRequestOptions = new DedicatedGatewayRequestOptions
		{
			MaxIntegratedCacheStaleness = TimeSpan.FromMinutes(30),
			
		}
	}))
	{
		while (feedIterator.HasMoreResults)
		{
			FeedResponse<dynamic> response = await feedIterator.ReadNextAsync();
			totalRU += response.RequestCharge;
		}
	}
	totalRU.Dump();
}

This is run in Linqpad, if you run the above code twice you will be charged RU’s both times, but if you set the consistencyLevel to eventual and run the code twice you will only be charged for the first request.

1reaction
robsierracommented, Nov 19, 2021

@ealsur I see, that does make sense and thank you for the explanation. If that is the case I would appreciate any help directing me to whom I should make a bug report.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Consistency level choices - Azure Cosmos DB
Azure Cosmos DB has five consistency levels to help balance eventual consistency, availability, and latency trade-offs.
Read more >
Request Units in Azure Cosmos DB
Data consistency: The strong and bounded staleness consistency levels consume approximately two times more RUs while performing read operations ...
Read more >
azure-docs/articles/cosmos-db/integrated-cache.md at main
Session consistency is the most widely used consistency level for both single region and globally distributed Azure Cosmos DB accounts. With ...
Read more >
How to configure the Azure Cosmos DB integrated cache
If you set MaxIntegratedCacheStaleness to 0, your read request will never use the integrated cache, regardless of the consistency level.
Read more >
Consistency Levels in Azure Cosmos DB Explained
This level of consistency honors the client session. It ensures a strong consistency for an application session with the same session token.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found