How to get RUs consumed from CosmosDB/MongoDB API

Jan 4, 2018 18:32 · 236 words · 2 minutes read CosmosDB

One of the key factors in using CosmosDB is selecting the number of Request Units per second (RUs) that are needed to keep up with load at various times of the day. In the DocumentDB API, CosmosDB makes this relatively simple since you can check the response header of each request for a particular field. This field tells you exactly how many RUs were used in processing your request, so you can extrapolate the value to get a ballpark of what level of service you need.

Using the MongoDB API, this isn’t so simple. Since MongoDB doesn’t have a similar concept to RUs, the drivers don’t have a field that can be set on a response to say how many RUs were used. This makes it difficult to know how many RUs each query uses, and makes tuning the queries impossible.

It took a while to track down but eventually I found a post buried in Stack Overflow that shows the command getLastRequestStatistics.

Running the command after running any query returns a structure as listed in the answered question:

> db.runCommand({"getLastRequestStatistics": 1})
{
    "_t" : "GetRequestStatisticsResponse", 
    "ok" : 1, 
    "CommandName" : "find", 
    "RequestCharge" : 5.5499999999999998, 
    "RequestDurationInMilliSeconds" : NumberLong(25) 
}

With the number of RUs used in the RequestCharge. The link to the documentation page with this information is here: https://docs.microsoft.com/en-us/azure/cosmos-db/request-units. I’m reposting this mostly to try to give Google another site to index for anybody searching.