satish kumar V
satish kumar V

Reputation: 1755

DocumentDb insert performance

I am using DocumentDb to store my data and this is the sample code I am using to insert a record in documentdb.

I am calling method like this

var result = ProcessRequestAsync(() => Client.CreateDocumentAsync("collection link", data)).Result;

and the method logic is like this

 public async static Task<ResourceResponse<T>> ProcessRequestAsync<T>(Func<Task<ResourceResponse<T>>> request)
            where T : Resource, new()
        {
            var delay = TimeSpan.Zero;
            var minDelayTime = new TimeSpan(0, 0, 1);
            for (; ; )
            {
                try
                {
                    await Task.Delay(delay);
                    return await request();
                }
                catch (DocumentClientException documentClientException)
                {
                    var statusCode = (int)documentClientException.StatusCode;
                    if (statusCode == 429 || statusCode == 503)
                    {
                        delay = TimeSpan.Compare(documentClientException.RetryAfter, minDelayTime) >= 0 ? documentClientException.RetryAfter : minDelayTime;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }

It is taking 2 seconds to insert a record into documentDb.

However I repeat the insertion process in a loop, the first record is taking 2 seconds to insert and remaining are taking 400ms around.

Anything I need to add to improve the speed of insertion?

Thanks in advance.

Upvotes: 1

Views: 1155

Answers (1)

Aravind Krishna R.
Aravind Krishna R.

Reputation: 8003

Have you followed the performance tips listed here: http://azure.microsoft.com/blog/2015/01/20/performance-tips-for-azure-documentdb-part-1-2/ and http://azure.microsoft.com/blog/2015/01/27/performance-tips-for-azure-documentdb-part-2/? You should see a performance of < 10 ms on writes with DocumentDB aside from network latency of your connection.

If you could post a complete sample, we can help further. Like Ryan mentioned, the longer time for the first call could be for the initialization of the client. The blog above also explains how to avoid that.

Upvotes: 2

Related Questions