Create new CloudTableClient and CloudTable instances per transaction

After some research, I'm still not sure how best to maintain a “connection” to Azure Table Storage. Should I reuse CloudTableClient or CloudTable through queries?

We use table storage behind a high traffic public API. We demand high availability and performance. All requests are POINT requests (both a section key and a line string are available), and the response payment is small in size (less than 1 kilobyte). Recording performance is not a big issue. Each request in the API can read up to 10 point requests through several sections.

From my reading, I understood the following:

  • CloudTableClient not thread safe and must be created for every transaction. Apparently, this should not interfere with continuous recreation.

  • Thus, a CloudTable instance must be created for each transaction.

Are these assumptions correct?

Thus, I reinitialize CloudTableClient and CloudTable for each request. It seems wasteful.

See implementation:

 public class EntityStorageComponent : IEntityComponent { private CloudStorageAccount storageAccount; public CloudTable Table { get { var tableClient = storageAccount.CreateCloudTableClient(); ServicePoint tableServicePoint = ServicePointManager.FindServicePoint(storageAccount.TableEndpoint); tableServicePoint.UseNagleAlgorithm = false; tableServicePoint.ConnectionLimit = 100; var context = new OperationContext(); context.Retrying += (sender, args) => { Debug.WriteLine("Retry policy activated"); }; // Attempt delays: ~200ms, ~200ms, ~200ms var requestOptions = new TableRequestOptions { RetryPolicy = = new LinearRetry(TimeSpan.FromMilliseconds(200), 3), MaximumExecutionTime = TimeSpan.FromSeconds(60) }; var table = tableClient.GetTableReference("farematrix"); table.CreateIfNotExists(requestOptions, context); return table; } } public EntityStorageComponent(IOptions<ConfigurationOptions> options) { storageAccount = CloudStorageAccount.Parse(options.Value.TableStorageConnectionString); } public SomeEntity Find(Guid partitionKey, Guid rowKey) { var retrieveOperation = TableOperation.Retrieve<SomeEntity>(partitionKey, rowKey); var retrievedResult = Table.Execute(retrieveOperation); return retrievedResult.Result as SomeEntity; } } 
+6
source share
1 answer

Besides the usual overhead of creating an object, I see no problem when creating multiple instances of CloudTableClient and CloudTable . Therefore, if you simply do the following, I do not think that you will succeed in performance:

  var tableClient = storageAccount.CreateCloudTableClient(); var table = tableClient.GetTableReference("farematrix"); 

However, I see a problem with the way you create CloudTable in your code (a member of Table ). Essentially in your code, when you get the Table property from EntityStorageComponent , you are trying to create a table in your storage account.

  var table = tableClient.GetTableReference("farematrix"); table.CreateIfNotExists(requestOptions, context); 

This is a problem because table.CreateIfNotExists(requestOptions, context); will make a network call and significantly slow down your system. You might want to print the code table.CreateIfNotExists(requestOptions, context); and put it in the startup code so that you are always sure (basically) that the table is present.

+7
source

All Articles