title: Mongo Driver for Azure Cosmos - Reads and Rate Limiting
body: <p>Having an issue with the following configuration,</p>
  <p>Driver version : 3.12.1, mongodb-driver for Java</p>
  <p>Server Version: 3.2 of Mongo API for Azure Cosmos DB (Ancient, I know)</p>
  <p>We run some fairly high read/write loads and may hit rate limiting from the Cosmos API for Mongo. In this case, I <strong>expect</strong> an exception to occur.  We're doing pretty vanilla queries, code snippet looks similar to</p>
  <pre><code>public DatabaseQueryResult find(String collectionName, Map&lt;String, Object&gt; queryData) {
  
      Document toFind = new Document(queryData);
      MongoCollection&lt;Document&gt; collection = this.mongoDatabase.getCollection(collectionName);
  
      FindIterable&lt;Document&gt; findResults = collection.find(toFind);
  
      if (findResults != null) {
          Document dataFound = findResults.first();
          return new DatabaseQueryResult(dataFound.toJson(this.settings))     
      }
  
      // other stuff...
  }
  </code></pre>
  <p>When rate limited by Azure, you'll receive a response like so</p>
  <pre><code>{
     &quot;$err&quot;:&quot;Message: {\&quot;Errors\&quot;:[\&quot;Request rate is large. More Request Units may be needed, so no changes were made. Please retry this request later. Learn more: http://aka.ms/cosmosdb-error-429\&quot;]}\r\n s&quot;,
     &quot;code&quot;:16500,
     &quot;_t&quot;:&quot;OKMongoResponse&quot;,
     &quot;errmsg&quot;:&quot;Message: {\&quot;Errors\&quot;:[\&quot;Request rate is large. More Request Units may be needed, so no changes were made. Please retry this request later. Learn more: http://aka.ms/cosmosdb-error-429\&quot;]}\r\n&quot;,
     &quot;ok&quot;:0
  }
  </code></pre>
  <p>I expect an exception to be thrown here - but that doesn't seem to be the case with the later driver. What's happening is,</p>
  <ul>
  <li>collection.find is returning a FindIterable with the JSON error result as above as the first document</li>
  <li>We're eventually returning a DatabaseQueryResult with JSON error as the query payload</li>
  </ul>
  <p>I don't want this to happen - I'd much prefer the mongo driver to throw a <strong>MongoCommandException/MongoQueryException</strong> if a query operation returns an OKMongoResponse where &quot;ok&quot; 0. This seems fine on writes,
  which will use a CommandProtocol object and the response is validated as I'd expect - it's just reads that seems to have changed.</p>
  <p>Comparing the 2 driver versions, this seems to be a change in read behaviour - perhaps due to retryable reads that were introduced in version 3.11? Response validation now seems to be around <a href="https://github.com/mongodb/mongo-java-driver/blob/b1e2eadf27593a4e90d1f3d697ae4f5ea87b251e/driver-core/src/main/com/mongodb/internal/connection/QueryProtocol.java#L518%0A" rel="nofollow noreferrer">this section.</a></p>
  <p><strong>Q:</strong> Is there a way to configure my Mongo client so that the driver will validate server responses on read operations and throw an exception if it receives a OKMongoResponse, and ok == 0?</p>
  <p>I can of course validate the results myself, but I'd prefer not to and let the driver do this if possible</p>