skip to Main Content

This YouTube video @27:20 talks about populating the cache with routing info to avoid latency during a cold start.
You can either try to get a document you know doesn’t exist, or you can use CosmosClient.CreateAndInitializeAsync().

I already have this code set up:

private async Task<Container> CreateContainerAsync(string endpoint, string authKey)
{
    var cosmosClientBuilder = new CosmosClientBuilder(
            accountEndpoint: endpoint,
            authKeyOrResourceToken: authKey)
        .WithConnectionModeDirect(portReuseMode: PortReuseMode.PrivatePortPool, idleTcpConnectionTimeout: TimeSpan.FromHours(1))
        .WithApplicationName(UserAgentSuffix)
        .WithConsistencyLevel(ConsistencyLevel.Session)
        .WithApplicationRegion(Regions.AustraliaEast)
        .WithRequestTimeout(TimeSpan.FromSeconds(DatabaseRequestTimeoutInSeconds))
        .WithThrottlingRetryOptions(TimeSpan.FromSeconds(DatabaseMaxRetryWaitTimeInSeconds), DatabaseMaxRetryAttemptsOnThrottledRequests);

    var client = cosmosClientBuilder.Build();
    
    var databaseResponse = await CreateDatabaseIfNotExistsAsync(client).ConfigureAwait(false);
    
    var containerResponse = await CreateContainerIfNotExistsAsync(databaseResponse.Database).ConfigureAwait(false);

    return containerResponse;
}

Is there any way to incorporate CosmosClient.CreateAndInitializeAsync() with it to populate the cache?

If not, is it ok to do this to populate the cache?

public class CosmosClientWrapper
{   
    public CosmosClientWrapper(IKeyVaultFacade keyVaultFacade)
    {           
        var container = CreateContainerAsync(endpoint, authenticationKey).GetAwaiter().GetResult();

        // Get a document that doesn't exist to populate the routing info:
        container.ReadItemAsync<object>(Guid.NewGuid().ToString(), PartitionKey.None).GetAwaiter().GetResult();     
    }
}

2

Answers


  1. Please see CosmosClientBuilder.BuildAndInitializeAsync which creates the cosmos client and initialize the provided containers. I believe this is what you are looking for.

    Login or Signup to reply.
  2. The point of CreateAndInitialize or BuildAndInitialize is to pre-establish the connections required to perform Data Plane operations to the desired containers (reference https://learn.microsoft.com/azure/cosmos-db/nosql/sdk-connection-modes#routing).

    If the containers do not exist, then it makes no sense to use CreateAndInitialize or BuildAndInitialize because there are no connections that can be pre-established/warmed up, because there are no target backend endpoints to connect to. That is why the container/database information is required, because the only benefit is warming up the connections to the backend machines that support that/those container/s.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search