2
votes

I am inserting/upserting a document with the following structure into DocumentDB:

enter image description here

The request charge for upserting a document of this format is 10.67 RUs when using the default index. This seems higher than I would expect so I am looking to optimize.

After reading the performance tips in this article:

https://azure.microsoft.com/en-us/blog/performance-tips-for-azure-documentdb-part-2/

I changed the Collection Indexing Policy's Indexing Mode to Lazy. I expected the request charge to be significantly less, but it only dropped to 9.9 RUs.

I then modified the Indexing Policy again to add a bunch of properties to the Excluded Paths. Here is the custom Indexing Policy:

enter image description here

Adding the Excluded Paths had no effect on the RUs for an upsert - it remained at 9.9.

Am I doing something wrong? And is it possible to make upserts for this document structure consume fewer RUs?

Edit:

Here is a helper class I use for setting up and caching the connection to DocumentDB:

public class Documents
{
    public static Documents Instance = new Documents();

    public IReliableReadWriteDocumentClient Client { get; private set; }

    private Documents()
    {
        var endpointUrl = ConfigurationManager.AppSettings["Microsoft.Azure.DocumentDb.EndpointUrl"];
        var authKey = ConfigurationManager.AppSettings["Microsoft.Azure.DocumentDb.AuthorizationKey"];
        var min = TimeSpan.FromMilliseconds(1000);
        var max = TimeSpan.FromMilliseconds(5000);
        var delta = TimeSpan.FromMilliseconds(1000);
        var connectionPolicy = new ConnectionPolicy()
        {
            ConnectionMode = ConnectionMode.Direct,
            ConnectionProtocol = Protocol.Tcp
        };
        var client = new DocumentClient(new Uri(endpointUrl), authKey, connectionPolicy).AsReliable(new ExponentialBackoff(3, min, max, delta));
        Task result = client.OpenAsync();
        result.Wait();
        Client = client;
    }
}

And here is the code for the Put handler in my Web API class:

public class InstallationController : ApiController
{
    private IReliableReadWriteDocumentClient docdb;
    private string docdbUri;

    public InstallationController()
    {
        docdb = Documents.Instance.Client;
        var databaseId = ConfigurationManager.AppSettings["Microsoft.Azure.DocumentDb.DatabaseId"];
        var collectionName = ConfigurationManager.AppSettings["Microsoft.Azure.DocumentDb.CollectionName"];
        docdbUri = "dbs/" + databaseId + "/colls/" + collectionName;
    }

    // PUT api/installation/<installationId>
    // This creates or updates an installation
    public async Task<IHttpActionResult> Put(string id, DeviceInstallation deviceUpdate)
    {
        string message;
        var telemetryClient = new TelemetryClient();

        if (id != deviceUpdate.Id)
        {
            return BadRequest();
        }

        // Code to check for existing record with same APNS Token omitted for clarity

        var upsertResponse = await docdb.UpsertDocumentAsync(docdbUri, deviceUpdate);
        requestCharge = upsertResponse.RequestCharge;
        message = string.Format("Request charge for installation upsert: {0}", requestCharge);
        telemetryClient.TrackTrace(message);

        // Code to save installation to notification hub omitted for clarity

        return Ok();
    }
}
1
Could you add your c# code please as it is hard to know given your current post's content. In general, I found that caching collection selfLink's gave me a significant performance boost. - user152949
@IngeHenriksen There's no need for selflinks anymore (or related queries ot the database to fetch such id's such as collection selflink), with id-based routing (which is just a string). And this has nothing to do with a query's RU cost. - David Makogon
@IngeHenriksen - I added the code you requested. - James Richards

1 Answers

1
votes

Replacing a document under 1kB consumes ~10 RUs without any indexing due to the cost of IO and replication. Indexing properties is lightweight in DocumentDB (only a fraction of an RU per property/term), so moving from Consistent to Lazy will not make a big difference in this case. It will make a difference if you had a document with thousands of properties in it.

Hope this helps.