Search code examples
azure-cosmosdbazure-cosmosdb-sqlapi

Server-side cancellation in Cosmos DB?


Many methods in the Cosmos DB .NET client take a CancellationToken parameter to allow cancellation of a request (e.g. FeedIterator.ReadAsync()). From the source code it is apparent that the token is checked at various points in the code, but it is also eventually sent along to the HTTP endpoint of the Cosmos DB.

If the cancellation token is cancelled during the phase when the client is waiting for data from the HTTP endpoint (e.g. in GateStoreClient.InvokeClientAsync(), the HTTP request is cancelled. Does this also cause the Cosmos DB to prematurely stop the computation and consume fewer RUs (e.g. when it is stopped after a few milliseconds even though it would otherwise run for a few minutes)?


Solution

  • Short answer: No.

    CancellationTokens are a .NET specific construct, they cannot be sent to the service.

    Having said that, the service quotas mention that the processing time for a request is up to 5 seconds.

    In .NET, CancellationToken is use along with RequestTimeout: https://learn.microsoft.com/azure/cosmos-db/nosql/troubleshoot-dotnet-sdk-request-timeout?tabs=cpu-new#customize-the-timeout-on-the-azure-cosmos-db-net-sdk

    CancellationToken is not checked while the request is on the wire (that's what RequestTimeout is for) but in-between retries (that's why the docs say that the CancellationToken time should always be larger than RequestTimeout).

    So even if you set the CancellationToken to 1 second, the network request is not stopped at 1 second, it will run either until it receives a response or the RequestTimeout is elapsed.

    EDIT: CancellationTokens are also checked in-between pages of a Query operation. In a nutshell, checked in-between network requests (either because they are retries or next pages).