Application developers can override the defaultretryandbackoffpolicies.
The default policies are to continue retrying for up to 15 minutes, and to use truncated (at 5 minutes) exponential backoff, doubling the maximum backoff period between retries.
namespace spanner = ::google::cloud::spanner;
using ::google::cloud::StatusOr;
[](std::string const& project_id, std::string const& instance_id,
std::string const& database_id) {
// Use a truncated exponential backoff with jitter to wait between
// retries:
// https://en.wikipedia.org/wiki/Exponential_backoff
// https://cloud.google.com/storage/docs/exponential-backoff
auto client = spanner::Client(spanner::MakeConnection(
spanner::Database(project_id, instance_id, database_id),
google::cloud::Options{}
.set<spanner::SpannerRetryPolicyOption>(
std::make_shared<spanner::LimitedTimeRetryPolicy>(
/*maximum_duration=*/std::chrono::seconds(60)))
.set<spanner::SpannerBackoffPolicyOption>(
std::make_shared<spanner::ExponentialBackoffPolicy>(
/*initial_delay=*/std::chrono::milliseconds(500),
/*maximum_delay=*/std::chrono::seconds(64),
/*scaling=*/1.5))));
std::int64_t rows_inserted;
auto commit_result = client.Commit(
[&client, &rows_inserted](
spanner::Transaction txn) -> StatusOr<spanner::Mutations> {
auto insert = client.ExecuteDml(
std::move(txn),
spanner::SqlStatement(
"INSERT INTO Singers (SingerId, FirstName, LastName)"
" VALUES (20, 'George', 'Washington')"));
if (!insert) return std::move(insert).status();
rows_inserted = insert->RowsModified();
return spanner::Mutations{};
});
if (!commit_result) throw std::move(commit_result).status();
std::cout << "Rows inserted: " << rows_inserted;
}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThis page provides access to various versions of the Spanner retry example documentation, ranging from version 2.11.0 up to the latest release candidate, 2.37.0-rc.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can customize default retry and backoff policies, which are initially set to retry for up to 15 minutes using a truncated exponential backoff.\u003c/p\u003e\n"],["\u003cp\u003eThe example demonstrates how to set a custom retry policy with a time limit of 60 seconds and an exponential backoff policy with specific initial and maximum delays, and a scaling factor.\u003c/p\u003e\n"],["\u003cp\u003eThe documentation contains code examples and the option to configure the exponential backoff policy and alternative retry policies, such as \u003ccode\u003eLimitedTimeRetryPolicy\u003c/code\u003e and \u003ccode\u003eLimitedErrorCountRetryPolicy\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["Version 2.11.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/spanner/latest/spanner-retry-example)\n- [2.41.0](/cpp/docs/reference/spanner/2.41.0/spanner-retry-example)\n- [2.40.0](/cpp/docs/reference/spanner/2.40.0/spanner-retry-example)\n- [2.39.0](/cpp/docs/reference/spanner/2.39.0/spanner-retry-example)\n- [2.38.0](/cpp/docs/reference/spanner/2.38.0/spanner-retry-example)\n- [2.37.0](/cpp/docs/reference/spanner/2.37.0/spanner-retry-example)\n- [2.36.0](/cpp/docs/reference/spanner/2.36.0/spanner-retry-example)\n- [2.35.0](/cpp/docs/reference/spanner/2.35.0/spanner-retry-example)\n- [2.34.0](/cpp/docs/reference/spanner/2.34.0/spanner-retry-example)\n- [2.33.0](/cpp/docs/reference/spanner/2.33.0/spanner-retry-example)\n- [2.32.0](/cpp/docs/reference/spanner/2.32.0/spanner-retry-example)\n- [2.31.0](/cpp/docs/reference/spanner/2.31.0/spanner-retry-example)\n- [2.30.0](/cpp/docs/reference/spanner/2.30.0/spanner-retry-example)\n- [2.29.0](/cpp/docs/reference/spanner/2.29.0/spanner-retry-example)\n- [2.28.0](/cpp/docs/reference/spanner/2.28.0/spanner-retry-example)\n- [2.27.0](/cpp/docs/reference/spanner/2.27.0/spanner-retry-example)\n- [2.26.0](/cpp/docs/reference/spanner/2.26.0/spanner-retry-example)\n- [2.25.1](/cpp/docs/reference/spanner/2.25.1/spanner-retry-example)\n- [2.24.0](/cpp/docs/reference/spanner/2.24.0/spanner-retry-example)\n- [2.23.0](/cpp/docs/reference/spanner/2.23.0/spanner-retry-example)\n- [2.22.1](/cpp/docs/reference/spanner/2.22.1/spanner-retry-example)\n- [2.21.0](/cpp/docs/reference/spanner/2.21.0/spanner-retry-example)\n- [2.20.0](/cpp/docs/reference/spanner/2.20.0/spanner-retry-example)\n- [2.19.0](/cpp/docs/reference/spanner/2.19.0/spanner-retry-example)\n- [2.18.0](/cpp/docs/reference/spanner/2.18.0/spanner-retry-example)\n- [2.17.0](/cpp/docs/reference/spanner/2.17.0/spanner-retry-example)\n- [2.16.0](/cpp/docs/reference/spanner/2.16.0/spanner-retry-example)\n- [2.15.1](/cpp/docs/reference/spanner/2.15.1/spanner-retry-example)\n- [2.14.0](/cpp/docs/reference/spanner/2.14.0/spanner-retry-example)\n- [2.13.0](/cpp/docs/reference/spanner/2.13.0/spanner-retry-example)\n- [2.12.0](/cpp/docs/reference/spanner/2.12.0/spanner-retry-example)\n- [2.11.0](/cpp/docs/reference/spanner/2.11.0/spanner-retry-example) \n\nRetry, Backoff, and Idempotency Policies.\n=========================================\n\nApplication developers can override the default [retry](/cpp/docs/reference/spanner/2.11.0/namespacegoogle_1_1cloud_1_1spanner) and [backoff](/cpp/docs/reference/spanner/2.11.0/namespacegoogle_1_1cloud_1_1spanner) policies.\n\nThe default policies are to continue retrying for up to 15 minutes, and to use truncated (at 5 minutes) exponential backoff, doubling the maximum backoff period between retries. \n\n namespace spanner = ::google::cloud::spanner;\n using ::google::cloud::StatusOr;\n [](std::string const& project_id, std::string const& instance_id,\n std::string const& database_id) {\n // Use a truncated exponential backoff with jitter to wait between\n // retries:\n // https://en.wikipedia.org/wiki/Exponential_backoff\n // https://cloud.google.com/storage/docs/exponential-backoff\n auto client = spanner::Client(spanner::MakeConnection(\n spanner::Database(project_id, instance_id, database_id),\n google::cloud::Options{}\n .set\u003cspanner::SpannerRetryPolicyOption\u003e(\n std::make_shared\u003cspanner::LimitedTimeRetryPolicy\u003e(\n /*maximum_duration=*/std::chrono::seconds(60)))\n .set\u003cspanner::SpannerBackoffPolicyOption\u003e(\n std::make_shared\u003cspanner::ExponentialBackoffPolicy\u003e(\n /*initial_delay=*/std::chrono::milliseconds(500),\n /*maximum_delay=*/std::chrono::seconds(64),\n /*scaling=*/1.5))));\n\n std::int64_t rows_inserted;\n auto commit_result = client.Commit(\n [&client, &rows_inserted](\n spanner::Transaction txn) -\u003e StatusOr\u003cspanner::Mutations\u003e {\n auto insert = client.ExecuteDml(\n std::move(txn),\n spanner::SqlStatement(\n \"INSERT INTO Singers (SingerId, FirstName, LastName)\"\n \" VALUES (20, 'George', 'Washington')\"));\n if (!insert) return std::move(insert).status();\n rows_inserted = insert-\u003eRowsModified();\n return spanner::Mutations{};\n });\n if (!commit_result) throw std::move(commit_result).status();\n std::cout \u003c\u003c \"Rows inserted: \" \u003c\u003c rows_inserted;\n }\n\n###### See Also\n\n[`LimitedTimeRetryPolicy`](/cpp/docs/reference/spanner/2.11.0/namespacegoogle_1_1cloud_1_1spanner) and [`LimitedErrorCountRetryPolicy`](/cpp/docs/reference/spanner/2.11.0/namespacegoogle_1_1cloud_1_1spanner) for alternative retry policies.\n\n###### See Also\n\n[`ExponentialBackoffPolicy`](/cpp/docs/reference/spanner/2.11.0/namespacegoogle_1_1cloud_1_1spanner) to configure different parameters for the exponential backoff policy."]]