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 Unicode C++ Downloads
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkRestW.h>
#include <CkAuthAzureStorageW.h>
#include <CkSocketW.h>
#include <CkHttpW.h>
#include <CkPrngW.h>
#include <CkDateTimeW.h>
void ChilkatSample(void)
{
bool success = false;
// StringBuilder compares text by value in every generated language.
CkStringBuilderW textCompare;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
CkJsonObjectW config;
config.put_Strict(true);
success = config.LoadFile(L"qa_data/azure/storage.json");
if (success != true) {
wprintf(L"Could not load private Azure configuration.\n");
return;
}
textCompare.Clear();
textCompare.SetString(config.stringOf(L"account"));
bool sameText1 = textCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"account") != 1) || (sameText1 == true)) {
wprintf(L"Supply the storage account name.\n");
return;
}
// access_key is the base64 account key, not a connection string or SAS.
textCompare.Clear();
textCompare.SetString(config.stringOf(L"access_key"));
bool sameText2 = textCompare.ContentsEqual(L"",true);
if ((config.JsonTypeOf(L"access_key") != 1) || (sameText2 == true)) {
wprintf(L"Supply the base64 account access key.\n");
return;
}
CkStringBuilderW accountText;
success = accountText.Append(config.stringOf(L"account"));
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
if ((accountText.get_Length() < 3) || (accountText.get_Length() > 24)) {
wprintf(L"Storage account names need 3-24 lowercase letters or digits.\n");
return;
}
CkStringBuilderW allowedAccountChars;
success = allowedAccountChars.Append(L"abcdefghijklmnopqrstuvwxyz0123456789");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
int charIndex = 0;
while ((charIndex < accountText.get_Length())) {
if (allowedAccountChars.Contains(accountText.getRange(charIndex,1,false),true) != true) {
wprintf(L"Storage account names need lowercase letters or digits.\n");
return;
}
charIndex = charIndex + 1;
}
CkStringBuilderW endpoint;
success = endpoint.Append(config.stringOf(L"account"));
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = endpoint.Append(L".table.core.windows.net");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
// Table names contain only letters and digits and are case-insensitive.
// Entity PartitionKey and RowKey values are case-sensitive.
const wchar_t *resourcePath = L"/ExampleCustomers()";
CkRestW rest;
rest.put_ConnectTimeoutMs(20000);
rest.put_IdleTimeoutMs(60000);
CkAuthAzureStorageW azAuth;
azAuth.put_Account(config.stringOf(L"account"));
azAuth.put_AccessKey(config.stringOf(L"access_key"));
azAuth.put_Service(L"Table");
azAuth.put_Scheme(L"SharedKey");
azAuth.put_XMsVersion(L"2026-04-06");
success = rest.SetAuthAzureStorage(azAuth);
if (success != true) {
wprintf(L"Could not configure Shared Key signing.\n");
return;
}
CkSocketW connection;
connection.put_RequireSslCertVerify(true);
connection.put_MaxReadIdleMs(60000);
connection.put_MaxSendIdleMs(60000);
success = connection.Connect(endpoint.getAsString(),443,true,20000);
if (success != true) {
wprintf(L"TLS connection failed; inspect diagnostics privately.\n");
return;
}
// Do not replay a write automatically after losing its response.
success = rest.UseConnection(connection,false);
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
const wchar_t *responseText = L"";
CkJsonObjectW result;
result.put_Strict(true);
success = rest.AddHeader(L"DataServiceVersion",L"3.0;NetFx");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddHeader(L"MaxDataServiceVersion",L"3.0;NetFx");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddHeader(L"Content-Type",L"application/json");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = rest.AddHeader(L"Accept",L"application/json;odata=minimalmetadata");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
const wchar_t *partitionKey = L"customers-west";
const wchar_t *rowKey = L"customer-001";
CkStringBuilderW encodedPk;
success = encodedPk.Append(partitionKey);
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
int escapedencodedPk = encodedPk.Replace(L"'",L"''");
success = encodedPk.Encode(L"url_rfc3986",L"utf-8");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
CkStringBuilderW encodedRk;
success = encodedRk.Append(rowKey);
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
int escapedencodedRk = encodedRk.Replace(L"'",L"''");
success = encodedRk.Encode(L"url_rfc3986",L"utf-8");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
// Escape apostrophes for OData first, then percent-encode each key once.
CkStringBuilderW entityPath;
success = entityPath.Append(L"/ExampleCustomers(PartitionKey='");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = entityPath.Append(encodedPk.getAsString());
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = entityPath.Append(L"',RowKey='");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = entityPath.Append(encodedRk.getAsString());
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = entityPath.Append(L"')");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
resourcePath = entityPath.getAsString();
int attempt = 0;
int baseDelayMs = 1000;
bool finished = false;
CkHttpW timer;
CkPrngW jitter;
CkStringBuilderW retryText;
CkStringBuilderW digits;
success = digits.Append(L"0123456789");
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
CkDateTimeW retryDate;
CkDateTimeW currentTime;
while ((finished != true)) {
attempt = attempt + 1;
responseText = rest.fullRequestNoBody(L"GET",resourcePath);
if (rest.get_LastMethodSuccess() != true) {
wprintf(L"Transport failure; this example does not retry incomplete reads.\n");
return;
}
int httpStatus = rest.get_ResponseStatusCode();
if (httpStatus == 200) {
finished = true;
}
else {
if ((httpStatus != 408) && (httpStatus != 429) && (httpStatus != 500) && (httpStatus != 502) && (httpStatus != 503) && (httpStatus != 504)) {
wprintf(L"Non-retryable HTTP status; inspect request ID privately.\n");
return;
}
if (attempt >= 5) {
wprintf(L"Retry limit reached.\n");
return;
}
int delayMs = baseDelayMs + jitter.RandomInt(0,250);
if (jitter.get_LastMethodSuccess() != true) {
wprintf(L"Could not generate retry jitter.\n");
return;
}
success = retryText.SetString(rest.responseHdrByName(L"Retry-After"));
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
success = retryText.Trim();
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
if (retryText.get_Length() > 0) {
bool numericDelay = true;
int digitIndex = 0;
while ((digitIndex < retryText.get_Length())) {
if (digits.Contains(retryText.getRange(digitIndex,1,false),true) != true) {
numericDelay = false;
}
digitIndex = digitIndex + 1;
}
int requestedSeconds = 0;
if (numericDelay == true) {
if (retryText.get_Length() > 2) {
wprintf(L"Retry-After exceeds this example wait budget.\n");
return;
}
requestedSeconds = retryText.get_IntValue();
}
else {
success = retryDate.SetFromRfc822(retryText.getAsString());
if (success != true) {
wprintf(L"Invalid Retry-After date.\n");
return;
}
success = currentTime.SetFromCurrentSystemTime();
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
requestedSeconds = retryDate.DiffSeconds(currentTime) + 1;
}
if (requestedSeconds > 30) {
wprintf(L"Server requested a longer wait; stop instead of retrying too soon.\n");
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(delayMs);
baseDelayMs = baseDelayMs * 2;
rest.Disconnect(10);
success = connection.Connect(endpoint.getAsString(),443,true,20000);
if (success != true) {
wprintf(L"TLS reconnection failed.\n");
return;
}
success = rest.UseConnection(connection,false);
if (success != true) {
wprintf(L"Could not prepare the request.\n");
return;
}
}
}
success = result.Load(responseText);
if (success != true) {
wprintf(L"Expected well-formed JSON.\n");
return;
}
textCompare.Clear();
textCompare.SetString(result.stringOf(L"PartitionKey"));
bool sameText3 = textCompare.ContentsEqual(L"",true);
if ((result.JsonTypeOf(L"PartitionKey") != 1) || (sameText3 == true)) {
wprintf(L"Missing string PartitionKey.\n");
return;
}
textCompare.Clear();
textCompare.SetString(result.stringOf(L"RowKey"));
bool sameText4 = textCompare.ContentsEqual(L"",true);
if ((result.JsonTypeOf(L"RowKey") != 1) || (sameText4 == true)) {
wprintf(L"Missing string RowKey.\n");
return;
}
textCompare.Clear();
textCompare.SetString(result.stringOf(L"PartitionKey"));
bool sameText5 = textCompare.ContentsEqual(partitionKey,true);
textCompare.Clear();
textCompare.SetString(result.stringOf(L"RowKey"));
bool sameText6 = textCompare.ContentsEqual(rowKey,true);
if ((sameText5 != true) || (sameText6 != true)) {
wprintf(L"Returned entity keys do not match.\n");
return;
}
wprintf(L"Entity read completed with bounded retries.\n");
}