Using Retryable Writes to Improve Reliability of Azure Cosmos DB for MongoDB
    • Dark
      Light
    • PDF

    Using Retryable Writes to Improve Reliability of Azure Cosmos DB for MongoDB

    • Dark
      Light
    • PDF

    Article Summary

    #ServerlessTips - Azure Cosmos DB
    Author: Rahul Rai Microsoft MVP

    Azure Cosmos DB is a fully managed database service that supports multiple APIs, including MongoDB. It offers global distribution, elastic scalability, consistent low latency, and high availability. You can learn more about Azure Cosmos DB for MongoDB by visiting the Microsoft documentation.

    Retryable writes is a feature that allows MongoDB drivers to retry certain write operations in case of failure automatically. This feature can improve the reliability and performance of your applications that use Azure Cosmos DB for MongoDB.

    The retryable writes feature can be enabled via the Azure CLI (instructions) or through the Azure Portal by selecting the "Features" option, clicking on the "Retryable writes" option, and then clicking on the "Enable" button. Below is a screenshot illustrating how to enable the feature in the Azure Portal:

    download 2

    Fig1.Enabling retryable writes feature

    To use this feature with your applications, you must use a MongoDB driver that supports retryable writes and enable it in your connection string. For example, you can add "retryWrites=true" to your connection string as follows in your .NET applications as follows:

    using MongoDB.Driver; 
    var connectionString = "mongodb://username:password@host:port/?retryWrites=true"; 
    var client = new MongoClient(connectionString); 
    

    This code uses the MongoDB C# driver to create a client with retryable writes enabled. You can copy the connection string by clicking on the "Connection strings" option in the "Settings" section, as shown in the screenshot below:

    download 4

    Fig2.Copy connection string to the database

    With retryable writes enabled, your application can handle transient errors such as network failures or primary replica elections without implementing custom logic or error handling. This can simplify your code and reduce the risk of data loss or inconsistency.

    Retryable writes are supported for insertOne, insertMany, updateOne, updateMany, replaceOne, deleteOne, deleteMany, and bulkWrite operations. However, they are not supported for transactions or operations that affect multiple documents with different _id values.

    Below is a code snippet for a complete sample application that uses the MongoDB C# driver to create a client that connects to CosmosDB for MongoDB using the MongoDB C# driver. First, the program creates a database and collection if they exist. It also creates a C# record for products that match the schema of the documents. It then inserts a sample product document into the collection, queries it by name, and finally prints the result to the console.

    using MongoDB.Driver; 
    using System; 
    
    // Replace with your connection string from Azure portal 
    var connectionString = "mongodb://username:password@host:port/?retryWrites=true"; 
    var client = new MongoClient(connectionString); 
    
    // Replace with your database name 
    var databaseName = "testdb"; 
    var database = client.GetDatabase(databaseName); 
    
    // Replace with your collection name 
    var collectionName = "testcol"; 
    var collection = database.GetCollection<Product>(collectionName); 
    
    // Create a sample product document 
    var product = new Product { Id = Guid.NewGuid(), Name = "iPhone 14", Price = 999 }; 
    
    // Insert the product into the collection 
    collection.InsertOne(product); 
    
    // Query the product by name 
    var filter = Builders<Product>.Filter.Eq("name", "iPhone 14"); 
    var result = collection.Find(filter).FirstOrDefault(); 
    
    // Print the result to console 
    Console.WriteLine($"Found product: {result.Id}, {result.Name}, {result.Price}"); 
    
    // Define a C# record for products 
    public record Product(Guid Id, string Name, decimal Price); 
    

    MicrosoftTeams-image 48


    Was this article helpful?