Azure Table: Retry a Read with Bounded Backoff
See more Azure Table Service Examples
Retries only this GET for selected transient HTTP statuses, at most five attempts. Uses exponential backoff plus jitter and honors numeric or HTTP-date Retry-After without shortening a requested wait. Stops when the server asks for more than 30 seconds, on invalid Retry-After, authentication failures or transport errors. Each connection/read also has a timeout. This is not a generic write-retry policy; uncertain mutation outcomes require reconciliation.
Targets Azure Storage Table service in the public cloud, not Azure Cosmos DB for Table. Uses Chilkat 11.6.0 or later and an unlocked library. Direct data requests pin x-ms-version 2026-04-06 and OData 3.0. Shared Key examples read private qa_data/azure/storage.json with account and access_key string fields. Authentication examples explain their additional fields. Create private local directories before running; keep credentials, tokens, SAS, entity values, checkpoints and diagnostics outside public content and source control.
HTTPS certificates are verified. Transport completion and HTTP status are checked separately. Writes are not automatically retried after an uncertain outcome. Entity keys, ETags and continuation tokens remain strings. Examples operate on the existing ExampleCustomers table unless noted; review the targets before adapting them. Microsoft Entra ID with suitable Table data roles is preferred where available.
Chilkat Objective-C Downloads
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoRest.h>
#import <CkoAuthAzureStorage.h>
#import <CkoSocket.h>
#import <CkoHttp.h>
#import <CkoPrng.h>
#import <CkoDateTime.h>
BOOL success = NO;
// StringBuilder compares text by value in every generated language.
CkoStringBuilder *textCompare = [[CkoStringBuilder alloc] init];
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = NO;
CkoJsonObject *config = [[CkoJsonObject alloc] init];
config.Strict = YES;
success = [config LoadFile: @"qa_data/azure/storage.json"];
if (success != YES) {
NSLog(@"%@",@"Could not load private Azure configuration.");
return;
}
[textCompare Clear];
[textCompare SetString: [config StringOf: @"account"]];
BOOL sameText1 = [textCompare ContentsEqual: @"" caseSensitive: YES];
if (([[config JsonTypeOf: @"account"] intValue] != 1) || (sameText1 == YES)) {
NSLog(@"%@",@"Supply the storage account name.");
return;
}
// access_key is the base64 account key, not a connection string or SAS.
[textCompare Clear];
[textCompare SetString: [config StringOf: @"access_key"]];
BOOL sameText2 = [textCompare ContentsEqual: @"" caseSensitive: YES];
if (([[config JsonTypeOf: @"access_key"] intValue] != 1) || (sameText2 == YES)) {
NSLog(@"%@",@"Supply the base64 account access key.");
return;
}
CkoStringBuilder *accountText = [[CkoStringBuilder alloc] init];
success = [accountText Append: [config StringOf: @"account"]];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
if (([accountText.Length intValue] < 3) || ([accountText.Length intValue] > 24)) {
NSLog(@"%@",@"Storage account names need 3-24 lowercase letters or digits.");
return;
}
CkoStringBuilder *allowedAccountChars = [[CkoStringBuilder alloc] init];
success = [allowedAccountChars Append: @"abcdefghijklmnopqrstuvwxyz0123456789"];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
int charIndex = 0;
while ((charIndex < [accountText.Length intValue])) {
if ([allowedAccountChars Contains: [accountText GetRange: [NSNumber numberWithInt: charIndex] numChars: [NSNumber numberWithInt: 1] removeFlag: NO] caseSensitive: YES] != YES) {
NSLog(@"%@",@"Storage account names need lowercase letters or digits.");
return;
}
charIndex = charIndex + 1;
}
CkoStringBuilder *endpoint = [[CkoStringBuilder alloc] init];
success = [endpoint Append: [config StringOf: @"account"]];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
success = [endpoint Append: @".table.core.windows.net"];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
// Table names contain only letters and digits and are case-insensitive.
// Entity PartitionKey and RowKey values are case-sensitive.
NSString *resourcePath = @"/ExampleCustomers()";
CkoRest *rest = [[CkoRest alloc] init];
rest.ConnectTimeoutMs = [NSNumber numberWithInt:20000];
rest.IdleTimeoutMs = [NSNumber numberWithInt:60000];
CkoAuthAzureStorage *azAuth = [[CkoAuthAzureStorage alloc] init];
azAuth.Account = [config StringOf: @"account"];
azAuth.AccessKey = [config StringOf: @"access_key"];
azAuth.Service = @"Table";
azAuth.Scheme = @"SharedKey";
azAuth.XMsVersion = @"2026-04-06";
success = [rest SetAuthAzureStorage: azAuth];
if (success != YES) {
NSLog(@"%@",@"Could not configure Shared Key signing.");
return;
}
CkoSocket *connection = [[CkoSocket alloc] init];
connection.RequireSslCertVerify = YES;
connection.MaxReadIdleMs = [NSNumber numberWithInt:60000];
connection.MaxSendIdleMs = [NSNumber numberWithInt:60000];
success = [connection Connect: [endpoint GetAsString] port: [NSNumber numberWithInt: 443] ssl: YES maxWaitMs: [NSNumber numberWithInt: 20000]];
if (success != YES) {
NSLog(@"%@",@"TLS connection failed; inspect diagnostics privately.");
return;
}
// Do not replay a write automatically after losing its response.
success = [rest UseConnection: connection autoReconnect: NO];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
NSString *responseText = @"";
CkoJsonObject *result = [[CkoJsonObject alloc] init];
result.Strict = YES;
success = [rest AddHeader: @"DataServiceVersion" value: @"3.0;NetFx"];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
success = [rest AddHeader: @"MaxDataServiceVersion" value: @"3.0;NetFx"];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
success = [rest AddHeader: @"Content-Type" value: @"application/json"];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
success = [rest AddHeader: @"Accept" value: @"application/json;odata=minimalmetadata"];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
NSString *partitionKey = @"customers-west";
NSString *rowKey = @"customer-001";
CkoStringBuilder *encodedPk = [[CkoStringBuilder alloc] init];
success = [encodedPk Append: partitionKey];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
int escapedencodedPk = [[encodedPk Replace: @"'" replacement: @"''"] intValue];
success = [encodedPk Encode: @"url_rfc3986" charset: @"utf-8"];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
CkoStringBuilder *encodedRk = [[CkoStringBuilder alloc] init];
success = [encodedRk Append: rowKey];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
int escapedencodedRk = [[encodedRk Replace: @"'" replacement: @"''"] intValue];
success = [encodedRk Encode: @"url_rfc3986" charset: @"utf-8"];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
// Escape apostrophes for OData first, then percent-encode each key once.
CkoStringBuilder *entityPath = [[CkoStringBuilder alloc] init];
success = [entityPath Append: @"/ExampleCustomers(PartitionKey='"];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
success = [entityPath Append: [encodedPk GetAsString]];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
success = [entityPath Append: @"',RowKey='"];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
success = [entityPath Append: [encodedRk GetAsString]];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
success = [entityPath Append: @"')"];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
resourcePath = [entityPath GetAsString];
int attempt = 0;
int baseDelayMs = 1000;
BOOL finished = NO;
CkoHttp *timer = [[CkoHttp alloc] init];
CkoPrng *jitter = [[CkoPrng alloc] init];
CkoStringBuilder *retryText = [[CkoStringBuilder alloc] init];
CkoStringBuilder *digits = [[CkoStringBuilder alloc] init];
success = [digits Append: @"0123456789"];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
CkoDateTime *retryDate = [[CkoDateTime alloc] init];
CkoDateTime *currentTime = [[CkoDateTime alloc] init];
while ((finished != YES)) {
attempt = attempt + 1;
responseText = [rest FullRequestNoBody: @"GET" uriPath: resourcePath];
if (rest.LastMethodSuccess != YES) {
NSLog(@"%@",@"Transport failure; this example does not retry incomplete reads.");
return;
}
int httpStatus = [rest.ResponseStatusCode intValue];
if (httpStatus == 200) {
finished = YES;
}
else {
if ((httpStatus != 408) && (httpStatus != 429) && (httpStatus != 500) && (httpStatus != 502) && (httpStatus != 503) && (httpStatus != 504)) {
NSLog(@"%@",@"Non-retryable HTTP status; inspect request ID privately.");
return;
}
if (attempt >= 5) {
NSLog(@"%@",@"Retry limit reached.");
return;
}
int delayMs = baseDelayMs + [[jitter RandomInt: [NSNumber numberWithInt: 0] high: [NSNumber numberWithInt: 250]] intValue];
if (jitter.LastMethodSuccess != YES) {
NSLog(@"%@",@"Could not generate retry jitter.");
return;
}
success = [retryText SetString: [rest ResponseHdrByName: @"Retry-After"]];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
success = [retryText Trim];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
if ([retryText.Length intValue] > 0) {
BOOL numericDelay = YES;
int digitIndex = 0;
while ((digitIndex < [retryText.Length intValue])) {
if ([digits Contains: [retryText GetRange: [NSNumber numberWithInt: digitIndex] numChars: [NSNumber numberWithInt: 1] removeFlag: NO] caseSensitive: YES] != YES) {
numericDelay = NO;
}
digitIndex = digitIndex + 1;
}
int requestedSeconds = 0;
if (numericDelay == YES) {
if ([retryText.Length intValue] > 2) {
NSLog(@"%@",@"Retry-After exceeds this example wait budget.");
return;
}
requestedSeconds = [retryText.IntValue intValue];
}
else {
success = [retryDate SetFromRfc822: [retryText GetAsString]];
if (success != YES) {
NSLog(@"%@",@"Invalid Retry-After date.");
return;
}
success = [currentTime SetFromCurrentSystemTime];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
requestedSeconds = [[retryDate DiffSeconds: currentTime] intValue] + 1;
}
if (requestedSeconds > 30) {
NSLog(@"%@",@"Server requested a longer wait; stop instead of retrying too soon.");
return;
}
if (requestedSeconds < 0) {
requestedSeconds = 0;
}
if (requestedSeconds * 1000 > delayMs) {
delayMs = requestedSeconds * 1000;
}
}
// Retry only this GET; never copy this loop blindly to mutation requests.
[timer SleepMs: [NSNumber numberWithInt: delayMs]];
baseDelayMs = baseDelayMs * 2;
[rest Disconnect: [NSNumber numberWithInt: 10]];
success = [connection Connect: [endpoint GetAsString] port: [NSNumber numberWithInt: 443] ssl: YES maxWaitMs: [NSNumber numberWithInt: 20000]];
if (success != YES) {
NSLog(@"%@",@"TLS reconnection failed.");
return;
}
success = [rest UseConnection: connection autoReconnect: NO];
if (success != YES) {
NSLog(@"%@",@"Could not prepare the request.");
return;
}
}
}
success = [result Load: responseText];
if (success != YES) {
NSLog(@"%@",@"Expected well-formed JSON.");
return;
}
[textCompare Clear];
[textCompare SetString: [result StringOf: @"PartitionKey"]];
BOOL sameText3 = [textCompare ContentsEqual: @"" caseSensitive: YES];
if (([[result JsonTypeOf: @"PartitionKey"] intValue] != 1) || (sameText3 == YES)) {
NSLog(@"%@",@"Missing string PartitionKey.");
return;
}
[textCompare Clear];
[textCompare SetString: [result StringOf: @"RowKey"]];
BOOL sameText4 = [textCompare ContentsEqual: @"" caseSensitive: YES];
if (([[result JsonTypeOf: @"RowKey"] intValue] != 1) || (sameText4 == YES)) {
NSLog(@"%@",@"Missing string RowKey.");
return;
}
[textCompare Clear];
[textCompare SetString: [result StringOf: @"PartitionKey"]];
BOOL sameText5 = [textCompare ContentsEqual: partitionKey caseSensitive: YES];
[textCompare Clear];
[textCompare SetString: [result StringOf: @"RowKey"]];
BOOL sameText6 = [textCompare ContentsEqual: rowKey caseSensitive: YES];
if ((sameText5 != YES) || (sameText6 != YES)) {
NSLog(@"%@",@"Returned entity keys do not match.");
return;
}
NSLog(@"%@",@"Entity read completed with bounded retries.");