Search code examples
c#azure-cosmosdbazure-automation

Azure CosmosDB AutoScaling Using Code/Automation


Is it possible to autoscale the CosmosDB throughput using either c# or Azure Automation? Please provide a sample example.


Solution

  • Storage is automatically scaled. Here is the code to change the throughput automatically

    //Fetch the resource to be updated
    Offer offer = client.CreateOfferQuery()
                      .Where(r => r.ResourceLink == collection.SelfLink)    
                      .AsEnumerable()
                      .SingleOrDefault();
    
    // Set the throughput to 2500 request units per second
    offer = new OfferV2(offer, 2500);
    
    //Now persist these changes to the database by replacing the original resource
    await client.ReplaceOfferAsync(offer);
    

    See more here: https://learn.microsoft.com/en-us/azure/cosmos-db/set-throughput

    HTH