title: Azure Cosmos DB Stored Procedure - 413 Request Too Large
body: <p>I've run into an issue regarding a Stored Procedure that I've written to perform bulk updates/deletes as a single transaction.
  The SP is:</p>
  
  <pre><code>function UpdatePages(pageList) 
  {
      // If no pages were given, end right now.
      if (!pageList) throw new Error("Input page list cannot be null");
  
      //getting the collection
      var collection = getContext().getCollection();
      // Check to see that there are pages to update.
      if (pageList.length &gt; 0) 
      {
          // Loop through each page and determine what to do.
          pageList.forEach(function (page) 
          {
              // If there is a self link of a page to delete, perform the deletion.
              if (page.SelfLinkOfPageToDelelte != null) 
              {
                  DeletePage(collection, page.SelfLinkOfPageToDelelte);
              }
              // Otherwise, If this is a special page, perform an update.
              else if (page.ExistingSpecialPage != null) 
              {
                  UpdateExistingPage(collection, page.ExistingSpecialPage);
              }
              // Otherwise, this is just a regular page, perform a update as appropriate
              else 
              {
                  UpdateExistingPage(collection, page.ExistingPage);
              }
          });
  
          // If we've made it this far, we've succeeded.
          getContext().getResponse().setBody("Success");
      }
      // If there aren't any, we shouldn't have called this. Throw an error.
      else 
      {
          throw new Error("No pages found to update/delete.");
      }
  }
  
  // Function for performing an update on an existing Document with the version stored in memory.
  function UpdateExistingPage(collection, page) 
  {
      // Gather information about the DocumentDB Resource
      var docToReplace = page.Doc;
      var requestOptions = { etag: docToReplace._etag };
  
      // Perform a replace (using the ETag for comparison)
      collection.replaceDocument(docToReplace._self, page.Obj, requestOptions, function (err) 
      {
          if (err) 
          {
              var customError = new Error();
              customError.name = err.number;
              customError.message = err.message;
              throw customError;
          }
      });
  }
  
  // Function for performing a page deletion, given the self link.
  function DeletePage(collection, pageSelfLink) 
  {
      collection.deleteDocument(pageSelfLink, {}, function (err) 
      {
          if (err) 
          {
              var customError = new Error();
              customError.name = err.number;
              customError.message = err.message;
              throw customError;
          }
      });
  }
  </code></pre>
  
  <p>The Pages I'm attempting to update contain a list of smaller items; I put an upper limit on what this amount can be so that I never hit the ~2ishMB file size limit, and code (C#) to handle of the splitting/adding of pages in-memory as needed, then call this SP to update Azure Cosmos DB with the changes. The 'Special' Page shown in the logic above is the same as the pages described here, but has a handful of int/boolean/string fields on the page itself.</p>
  
  <p>Of the Test cases I'm using to verify my splitting logic works, there are 2 of note. The first involved creating a page, tries to add 5000 items to it, and calls commit. The second involves loading a special page that already contains 3000 items it it, and calling commit. The act of calling commit will perform the page splitting, and the page size I've set to 1250 for testing. As such, I would expect to see the first case create a total of 4 pages (each containing exactly 1250 items in it), and the second case to update the initial page to have only 1250 pages in it and create two new pages (one with 1250 and the other with 500).</p>
  
  <p>The First Case, passes just fine; Everything works as expected.</p>
  
  <p>The Second Case, however, fails with an error:
  <code>(HttpStatusCode = 413): Message: {"Errors":["Request size is too large"]}</code></p>
  
  <p>This perplexes me a bit, considering in the first case I'm sending a request that is large than in the second case. </p>
  
  <p>My colleagues and I think it might have something to do with the initial state that the document we're updating is in. When my code creates a page, it creates the empty page in Cosmos DB, and returns the newly created document from it. Any changes made to the page are kept in-memory until the commit is called.
  As Such, in the first case we have a blank page that we're replacing, whereas in the second case we are attempting to replace a much larger file.</p>
  
  <p>I couldn't find a lot of documentation on the javascript classes I'm using in the stored procedure (save for whats listed <a href="http://azure.github.io/azure-documentdb-js-server/Collection.html" rel="nofollow noreferrer">here</a>), so I was wondering if anyone had encountered any similar problem, or if anyone had suggestions on how to get around this issue. Any help would be greatly appreciated!</p>