ren
ren

Reputation: 3993

windows azure table storage, how do I enable keep alive

Currently I put to azure table storage like this:

public static void AzurePut(string Key, byte[] Value)
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

    var keyhash = MyTable.CalculateMD5Hash(Key);
    var tc = MyTable.GetBinFromHash(keyhash, AzureTables.TableCount);
    CloudTable table = tableClient.GetTableReference("table" + tc);

    var entity = new AzureEntity();
    entity.Key = keyhash;
    entity.SetValue(Value);

    TableOperation insertOperation = TableOperation.InsertOrReplace(entity);
    table.Execute(insertOperation);
}

I do a lot of puts and they are slow. When I open fidller they become like 40 times faster. After checking why it turns out that fiddler reuses connections with connection: keep-alive headers. Is there any way to do this using table storage api?

Upvotes: 2

Views: 1485

Answers (1)

b2zw2a
b2zw2a

Reputation: 2693

Short: Add this to the start-up code of your application:

        ServicePointManager.DefaultConnectionLimit = 100; // Default 2
        ServicePointManager.UseNagleAlgorithm = false; // Default true

Explanation

You don't have to add any Keep-Alive headers, they are already there. Have a look at HttpWebRequestFactory (line 86):

#if WINDOWS_DESKTOP && !WINDOWS_PHONE
            request.KeepAlive = true;

            // Disable the Expect 100-Continue
            request.ServicePoint.Expect100Continue = false;
#endif

            return request;
        }

On top of that HttpWebRequest uses HTTP 1.1 by default which makes connection persistent by default

You can use TcpView to see that connection is being reused.

Fiddler is so fast mostly because it's extremely clever about reusing connections, batching and buffering requests, especially when your application is making a lot of parallel requests.

By default ServicePointManager.DefaultConnectionLimit is 2 which means that you can have only 2 pending requests at the same time. Imagine you have 8 threads trying to make a request, 2 of them can be active at time, the rest is waiting. Raising the limit greatly improves concurrent requests.

Another issue is that ServicePointManager.UseNagleAlgorithm is enabled by default. Since most of Azure Table requests are relatively small(HTTP message size < 1460 bytes), they are unnecessary buffered. See much longer explanation of this at Microsoft Azure Storage Team Blog (Nagle’s Algorithm is Not Friendly towards Small Requests)

Upvotes: 3

Related Questions