![]()
Azure CosmosDB is a globally distributed, multi-model database service that provides high availability and scalability. However, applications interacting with CosmosDB may encounter failures due to various reasons. Understanding these failures and their resolutions is crucial for maintaining application stability and performance. In this article, we will explore different types of CosmosDB failures, their root causes, and how to resolve them effectively.
Identifying Failures in Azure CosmosDB
You can monitor failures in Azure CosmosDB using the Azure portal by navigating to:
Azure Portal -> Respective CosmosDB -> Monitoring -> Insights -> Requests
![Azure Portal -> Respective CosmosDB -> Monitoring -> Insights -> Requests]()
From here, you can click on the Metrics icon to get a detailed breakdown of failure types, their frequency, and affected resources.
![Metrics]()
MongoDB provides different kinds of error codes.
11000
- MongoDB Duplicate Key Error: This occurs when you try to insert a document with a unique key that already exists in the database.
- This is commonly due to a conflict with a unique index, especially on the _id field.
1️⃣ Duplicate _id Field
- Issue: The _id field in MongoDB (and Cosmos DB with the MongoDB API) must be unique. If you try to insert a document with an existing _id, you’ll get error 11000.
- Fix: Ensure that each document has a unique _id before inserting.
✅ Solution (Generate a Unique _id):
db.collection.insertOne({
"_id": ObjectId(), // Ensure a unique ID
"name": "New User",
"email": "[email protected]"
})
If you're using C# with the MongoDB driver:
var document = new BsonDocument { { "_id", ObjectId.GenerateNewId() }, { "name", "John Doe" } };
2️⃣ Unique Index Violation
- Issue: You may have a unique index on another field (e.g., email, username).
- Fix: Check existing indexes:
db.collection.getIndexes()
If email is unique and you're trying to insert an existing email, it will fail.
✅ Solution (Update Instead of Insert)
db.collection.updateOne(
{ "email": "[email protected]" },
{ $set: { "name": "Updated User" } },
{ upsert: true }
)
This updates the document if it exists. Otherwise, it inserts a new one.
3️⃣ Bulk Insert with Duplicates
- Issue: If you’re inserting multiple documents in a batch and one has a duplicate _id, the entire operation can fail.
- Fix: Use ordered:false in bulk operations to allow partial success.
db.collection.insertMany([
{ "_id": ObjectId(), "name": "User1" },
{ "_id": ObjectId(), "name": "User2" },
{ "_id": "existing_id", "name": "Duplicate" } // This will fail
], { ordered: false })
🛠 How to Debug Further?
Check the Exact Field Causing the Conflict:
db.collection.find({ "_id": "your_value" })
If a document exists, you need to update it instead of inserting a duplicate.
Check Unique Indexes:
db.collection.getIndexes()
Fix it by Removing the Unique Index (If Not Needed):
db.collection.dropIndex("email_1") // Removes unique constraint
16500
In Azure Cosmos DB (MongoDB API), error code 16500 typically indicates an issue related to sharding or partitioning. This error occurs when you try to perform an operation that requires targeting a single shard, but the operation is attempted across multiple shards.
🔍 Common Causes and Fixes for Error 16500
1️⃣ Querying Without a Shard Key (For Sharded Collections)
- Issue: If your Cosmos DB collection is sharded (partitioned), you must include the partition key (shard key) in your query.
- Fix: Always filter queries using the shard key.
✅ Example (Correct Query with Partition Key):
db.collection.find({ "deviceId": "12345" }) // Assuming "deviceId" is the shard key
❌ Incorrect Query (Will Cause Error 16500)
db.collection.find({ "status": "active" }) // Missing the shard key
2️⃣ Update or Delete Without a Shard Key
- Issue: Cosmos DB requires updates and deletes to be targeted to a specific shard.
- Fix: Ensure that the partition key is included in the filter.
✅ Solution (Include Shard Key in Update)
db.collection.updateOne(
{ "deviceId": "12345", "status": "active" },
{ $set: { "status": "inactive" } }
)
❌ Incorrect (Missing Shard Key)
db.collection.updateOne(
{ "status": "active" }, // Missing shard key
{ $set: { "status": "inactive" } }
)
3️⃣ Aggregation Queries Without $match on Shard Key
- Issue: Some aggregation operations (e.g., $group, $sort) can only run within a single partition.
- Fix: Use $match with the shard key before applying other aggregation stages.
✅ Solution (Use $match First)
db.collection.aggregate([
{ $match: { "deviceId": "12345" } }, // Ensure query targets one shard
{ $group: { _id: "$status", count: { $sum: 1 } } }
])
❌ Incorrect (No $match on Shard Key)
db.collection.aggregate([ { $group: { _id: "$status", count: { $sum: 1 } } } ])
4️⃣ Upsert (updateOne with upsert: true) Without a Shard Key
- Issue: If you use upsert (insert if not exists) in a sharded collection, you must include the partition key.
- Fix: Add the shard key in the filter.
✅ Solution (Correct Upsert)
db.collection.updateOne( { "deviceId": "12345", "userIdb.collection.updateOne(
{ "deviceId": "12345", "userId": "abc" }, // deviceId is the partition key
{ $set: { "status": "active" } },
{ upsert: true }
)
🛠 How to Debug Further?
Check Your Collection's Shard Key
db.getCollectionInfos({ name: "your_collection_name" })
Look for "shardKey" in the response.
Check Your Query for a Missing Shard Key
db.collection.find({}).explain("executionStats")
If it shows "scatter-gather", it means the query is hitting multiple shards.
Check Indexes (If Your Query Uses the Correct Key)
db.collection.getIndexes()