Programmatically Update Cosmos DB Throughput

Azure Cosmos DB has an interesting charging model in that you pay for provisioned throughput along with consumed storage by the hour. The provisioned throughput is expressed in Request Units per second (RU/s) which can be used for various operations. The goal is to provision just the right amount of RU/s so you are not overpaying for throughput you are not using. There will be times you need to scale up in order to perform a particular job (like loading a bunch of data) in order to increase the job’s performance and then scale back down when done. The code sample below show you just how to do that.

using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace UpdateCosmosDBThroughput
  class Program
    private static string endpointUri = "https://{tenant}";
    private static string primaryKey = "{PrimaryKey}";
    private static string databaseName = "{DatabaseName}";
    private static string collectionName = "{coloectionName}";
    static async Task Main(string[] args)
      using (DocumentClient documentClient = new DocumentClient(new Uri(endpointUri), primaryKey))
        await ChangeThroughput(documentClient2000);
        // Perform job requiring more throughput
        await ChangeThroughput(documentClient400);
    private static async Task ChangeThroughput(DocumentClient documentClientint throughput)
      // Get a reference to the database
      Database database = await documentClient.ReadDatabaseAsync($"/dbs/{databaseName}");
      // Get a reference to the collection
      DocumentCollection cosmosCollection = documentClient.CreateDocumentCollectionQuery(database.SelfLink)
        .Where(c => c.Id == collectionName)
      //Fetch the offer to be updated
      Offer offer = documentClient.CreateOfferQuery()
                        .Where(r => r.ResourceLink == cosmosCollection.SelfLink)
      // Update the throughput to the specified value
      offer = new OfferV2(offerthroughput);
      // Persist the change by replacing the original offer
      await documentClient.ReplaceOfferAsync(offer);

Note that you are charged for the maximum provisioned throughput for any given hour. So, if you increase provisioned throughput at 9:55 AM from 400 RU/s to 1000 RU/s and then lower provisioned throughput back at 10:05 AM, you will be charged for two hours of 1000 RU/s.