Cosmosdb MongoDb API - 413 Request too large on documents under 2mb (bson)

Jonathan Grant 0 Reputation points
2025-03-26T09:06:49.68+00:00

I have a cosmosdb mongodb instance, in which I've had large documents stored in it for some time. I've been able to update them fine - until the beginning of this week (24/03/2025).

This is the error:


Mongo Server error (MongoWriteException): Write operation error on server test-db-uksouth.mongo.cosmos.azure.com:10255. Write error: WriteError{code=16, message='Error=16, Details='Response status code does not indicate success: RequestEntityTooLarge (413); Substatus: 0; ActivityId: b926cdda-ca85-4a5a-9880-cc79cfa957cb; Reason: (Message: {"Errors":["Request size is too large"]}
ActivityId: b926cdda-ca85-4a5a-9880-cc79cfa957cb, Request URI: /apps/c425cd55-b446-43d7-ae68-8fd6be6f658b/services/bb4327ba-6de5-4e62-971c-f0c6f97644f4/partitions/b0ec57aa-cebe-4683-9833-a75f8052ce1c/replicas/133873962017831188p/, RequestStats: Microsoft.Azure.Cosmos.Tracing.TraceData.ClientSideRequestStatisticsTraceDatum, SDK: Windows/10.0.20348 cosmos-netstandard-sdk/3.18.0);', details={}}.

Now Microsoft states the limit for documents at 2mb. Our document DOES exceed this in it's json UTF-8 length (at almost 4mb), however - we were able to update the document just fine until 24/03 (and we'd made an assumption that because it was working, it must be based on the bson size, which our problem document is under - at 1.5mb).

So it looks like the server is rejecting it based on the request size being too big (413). I've now found this limit (as of 24/03) to be at roughly 3mb for a json UTF-8 length. It appears like Microsoft has likely just made the api server more strict on the length of the content it allows through.

Now I understand, to have a document of the size above is not optimal - but moreover, the point here is how this setting (that triggers the 413 error) appears to have been introduced silently and without warning. This should be returned back to what it was, and introduced in a transparent manner with time for applications to be updated.

Has anyone else been experiencing this at all recently?

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

1 answer

Sort by: Most helpful
  1. Vijayalaxmi Kattimani 3,250 Reputation points Microsoft External Staff Moderator
    2025-03-26T10:35:03.6133333+00:00

    Hi Jonathan Grant,

    Greetings!

    I understand your concern regarding the recent inability to update large documents in your Azure Cosmos DB for MongoDB instance, especially given that these operations were successful prior to March 24, 2025.

    The error you are encountering, RequestEntityTooLarge (413), indicates that the server is rejecting your document because its size exceeds the allowed limit. As you mentioned, Cosmos DB has a limit of 2 MB for documents. However, the effective limit for requests can be lower due to overhead, especially when considering the JSON representation of the document. The BSON size may be smaller, but the JSON representation can be larger due to encoding.

    Here are a few steps you might consider to address this problem:

    • Activate the EnableMongo16MBDocumentSupport capability for your Azure Cosmos DB account. This can be done via the Azure Portal under the Features tab or programmatically by adding the capability to your account settings. Remember, this change will only affect new collections created after enabling the feature. To know more about this, please refer to these documents https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/how-to-configure-capabilities https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/feature-support-50
    • Consider optimizing your document structure to reduce its size. Break down your large document into smaller, more manageable pieces, removing unnecessary fields or data. This can help you stay within the size limits and avoid the 413 error.
    • If possible, consider breaking your updates into smaller batches. Instead of sending a large document in one request, you could update smaller portions of the document in multiple requests.
    • Compress the data before storing it in CosmosDB. This can reduce the size of your documents and help you stay within the limits.

    I hope this information helps. Please do let us know if you have any further queries.

    0 comments No comments

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.