Patch operation taking more RU's than replace. What gives?

Bubba Jones 216 Reputation points
2022-07-11T15:09:20.367+00:00

I have the following document in a cosmos DB (running emulator).

        {  
            "partitionKey": "3be5653d-c500-4a20-9f30-ce9df876c397",  
            "NumberOfRatings": 0,  
            "AverageRating": -1,  
            "Views": 2,  
            "Favorites": 0  
        }  

The above gets updated many times, and according to my reading patching a document should be more optimal than replacing an entire document. I have run some tests with the following code. The first block patches a document the second replaces it. They both increment the Views count in the above document by 1:

    //  Gets a user in the database   
    UserInfo userInfo = GetUser();  

    //  This operation costed 10,79 RU's  
    ItemResponse<UserInfo> response = await container.PatchItemAsync<UserInfo>(  
        id: userInfo.Id.ToString(),  
        partitionKey: new PartitionKey(userInfo.UserId.ToString()),  
        patchOperations: new[] {  
                            PatchOperation.Increment("/Views", 1)  
        }  
    );  
      
    //  This operation costed 10,67 RU's  
    response = await container.ReadItemAsync<UserInfo>(userInfo.Id.ToString(), new PartitionKey(userInfo.UserId.ToString()));  
    UserInfo itemBody = response.Resource;  
    itemBody.Views++;                          
    response = await container.ReplaceItemAsync<UserInfo>(itemBody, itemBody.Id.ToString(), new PartitionKey(itemBody.UserId.ToString()));  

The patching costed 10,79 RU's while the replacing costed 10,67 RU's. I would like to therefore ask the following two questions:

  1. Is this normal?
  2. If its normal, what the purpose of patching if it does not improve RU cost, rather makes it worse?
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,906 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hasan Savran 331 Reputation points MVP
    2022-07-11T15:31:37.373+00:00

    Partial updates main advantage is not saving R/U for transactions.
    They will make update process easier. (You don't need send the whole document to Cosmos DB to update an item)
    Some of the advantages are :
    Also, it reduces network bandwidth since you don't send the whole document anymore.
    lower end-toend latency
    saves CPU cycles on the Azure Cosmos DB SDK client hosts.

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Hasan Savran 331 Reputation points MVP
    2022-07-12T18:22:12.29+00:00

    I am glad this helped @Bubba Jones
    For monitoring, I can reference the following article since it is a free way to do this.
    https://stackify.com/w3wp-high-cpu-usage/

    1 person found this answer 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.