Actual document size limit for Azure Cosmos DB’s API for MongoDB

Ivan Povazan 21 Reputation points
2022-01-12T14:10:13.427+00:00

Description

I was doing some testing and found out that maximum document size limits of CosmosDB seem inconsistent with the documentation:
https://learn.microsoft.com/en-us/azure/cosmos-db/concepts-limits#per-item-limits

Test SQL

  1. Create a Cosmos DB Core (SQL) database
  2. Try to insert an item of 2093334 bytes into "Items" table
    • It succeeds since it is: < 2Mb as stated in the official documentation (link above)
  3. Try to insert a larger item of 2239798 bytes into the same table
    • It fails with error code (413) since the item is > 2Mb

RequestEntityTooLarge error occurred: Microsoft.Azure.Cosmos.CosmosException : Response >status code does not indicate success: RequestEntityTooLarge (413); Substatus: 0; >ActivityId: c1977df8-ec39-40b9-bd69-6e6a40ff6c00; Reason: (Message: {"Errors":["Request >size is too large"]}

Conclusion: Results match the documentation

Test MongoDB

  1. Create a Cosmos DB (MongoDB) database
  2. Try to insert an item of 2093334 bytes into "Items" collection
    • It succeeds since it is < 2Mb
  3. Try to insert an item of 2239798 bytes into the same collection
    • It still succeeds even though it is > 2Mb and the documentation states differently (or I am missing something)
  4. Try to insert an item of 4520606 bytes into the same collection
    • Still succeeds even if it is ~4.31Mb and
  5. Finally, when I try to insert an item of 4526027 bytes into the same collection
    • It fails with error code (413):

ERROR: MongoDB.Driver.MongoWriteException: A write operation resulted in an error. >WriteError: { Category : "Uncategorized", Code : 16, Message : "Error=16, Details='Response >status code does not indicate success: RequestEntityTooLarge (413); Substatus: 0; >ActivityId: 8f20b261-e1c5-4ca9-b4e6-6cbc5352ce7e; Reason: (Message: {"Errors":["Request >size is too large"]}

Conclusion: Results do not match the documentation

NOTE

The number of bytes was calculated in the following way:

var itemStr = JsonConvert.SerializeObject(item);
var bytes = Encoding.UTF8.GetBytes(itemStr);
Console.WriteLine($"total num of bytes: {bytes.Length}");

Regarding the successful write of a large item into MongoDB, I also verified through mongoshell that the stored document is larger than 4Mb:
Object.bsonsize(db.Items.findOne({_id:ObjectId("61dec458316798c759091aef")}))

Question

  1. Is there any other place in the documentation which defines these sizes differently?
  2. Am I not interpreting these results correctly?

Any help is much appreciated, thanks!

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,906 questions
{count} votes

Accepted answer
  1. Mark Brown - MSFT 2,771 Reputation points Microsoft Employee
    2022-01-14T16:59:45.337+00:00

    Cosmos DB's API for MongoDB has a binary storage format that compresses the data. The amount of compression depends on the shape of data within your documents. Documents with deeper hierarchies tend to compress more than those which are flatter. As a result, you are able to store uncompressed data greater than the documented 2MB limit.

    While it is possible to store more than 2MB of uncompressed data with Cosmos DB's API for MongoDB this is not something I would recommend as it is impossible for you to know what amount of compressed data you will have before being inserted.

    I also should point out that in general, you will have better performance in terms of cost and latency with higher numbers of smaller documents than you will with larger sized documents (this applies to native MongoDB as well). So as you model your data for your application, keep that in mind.

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.