This shows how to override the retry policies for spanner_admin::InstanceAdminClient:
namespace spanner = ::google::cloud::spanner;
[](std::string const& project_id) {
// An instance admin client is controlled by three policies. The retry
// policy determines for how long the client will retry transient failures.
auto retry_policy =
google::cloud::spanner_admin::InstanceAdminLimitedTimeRetryPolicy(
/*maximum_duration=*/std::chrono::minutes(25))
.clone();
// The backoff policy controls how long does the client waits to retry after
// a transient failure. Here we configure a truncated exponential backoff
// with jitter:
// https://en.wikipedia.org/wiki/Exponential_backoff
// https://cloud.google.com/storage/docs/exponential-backoff
auto backoff_policy = spanner::ExponentialBackoffPolicy(
/*initial_delay=*/std::chrono::seconds(2),
/*maximum_delay=*/std::chrono::minutes(10),
/*scaling=*/2.0)
.clone();
// The polling policy controls how the client waits for long-running
// operations (such as creating new instances). `GenericPollingPolicy<>`
// combines existing policies. In this case, keep polling until the
// operation completes (with success or error) or 45 minutes, whichever
// happens first. Initially pause for 10 seconds between polling requests,
// increasing the pause by a factor of 4 until it becomes 2 minutes.
auto polling_policy =
spanner::GenericPollingPolicy<>(
spanner::LimitedTimeRetryPolicy(
/*maximum_duration=*/std::chrono::minutes(45)),
spanner::ExponentialBackoffPolicy(
/*initial_delay=*/std::chrono::seconds(10),
/*maximum_delay=*/std::chrono::minutes(2),
/*scaling=*/4.0))
.clone();
auto client = google::cloud::spanner_admin::InstanceAdminClient(
google::cloud::spanner_admin::MakeInstanceAdminConnection(),
google::cloud::Options{}
.set<google::cloud::spanner_admin::InstanceAdminRetryPolicyOption>(
std::move(retry_policy))
.set<
google::cloud::spanner_admin::InstanceAdminBackoffPolicyOption>(
std::move(backoff_policy))
.set<
google::cloud::spanner_admin::InstanceAdminPollingPolicyOption>(
std::move(polling_policy)));
// Use the client as usual.
std::cout << "Available configs for project " << project_id << "\n";
auto project = google::cloud::Project(project_id);
for (auto& cfg : client.ListInstanceConfigs(project.FullName())) {
if (!cfg) throw std::move(cfg).status();
std::cout << cfg->name() << "\n";
}
std::cout << "End of available configs\n";
}
This shows how to create a custom idempotency policy:
class CustomIdempotencyPolicy : public google::cloud::spanner_admin::
InstanceAdminConnectionIdempotencyPolicy {
public:
~CustomIdempotencyPolicy() override = default;
std::unique_ptr<
google::cloud::spanner_admin::InstanceAdminConnectionIdempotencyPolicy>
clone() const override {
return std::make_unique<CustomIdempotencyPolicy>(*this);
}
// Override inherited functions to define as needed.
};
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-22 UTC."],[[["\u003cp\u003eThe page provides access to documentation for various versions of the \u003ccode\u003espanner_admin\u003c/code\u003e library, ranging from version 2.11.0 up to the latest release candidate 2.37.0-rc.\u003c/p\u003e\n"],["\u003cp\u003eUsers can learn how to override the default retry policies for the \u003ccode\u003espanner_admin::InstanceAdminClient\u003c/code\u003e, including customizing the retry, backoff, and polling behaviors.\u003c/p\u003e\n"],["\u003cp\u003eThe content demonstrates how to set policies using \u003ccode\u003eInstanceAdminRetryPolicyOption\u003c/code\u003e, \u003ccode\u003eInstanceAdminBackoffPolicyOption\u003c/code\u003e, and \u003ccode\u003eInstanceAdminPollingPolicyOption\u003c/code\u003e within the client's options.\u003c/p\u003e\n"],["\u003cp\u003eThe page also includes a code example for creating a custom idempotency policy by inheriting and overriding functions in \u003ccode\u003eInstanceAdminConnectionIdempotencyPolicy\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["Version 2.20.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/spanner/latest/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.41.0](/cpp/docs/reference/spanner/2.41.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.40.0](/cpp/docs/reference/spanner/2.40.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.39.0](/cpp/docs/reference/spanner/2.39.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.38.0](/cpp/docs/reference/spanner/2.38.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.37.0](/cpp/docs/reference/spanner/2.37.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.36.0](/cpp/docs/reference/spanner/2.36.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.35.0](/cpp/docs/reference/spanner/2.35.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.34.0](/cpp/docs/reference/spanner/2.34.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.33.0](/cpp/docs/reference/spanner/2.33.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.32.0](/cpp/docs/reference/spanner/2.32.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.31.0](/cpp/docs/reference/spanner/2.31.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.30.0](/cpp/docs/reference/spanner/2.30.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.29.0](/cpp/docs/reference/spanner/2.29.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.28.0](/cpp/docs/reference/spanner/2.28.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.27.0](/cpp/docs/reference/spanner/2.27.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.26.0](/cpp/docs/reference/spanner/2.26.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.25.1](/cpp/docs/reference/spanner/2.25.1/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.24.0](/cpp/docs/reference/spanner/2.24.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.23.0](/cpp/docs/reference/spanner/2.23.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.22.1](/cpp/docs/reference/spanner/2.22.1/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.21.0](/cpp/docs/reference/spanner/2.21.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.20.0](/cpp/docs/reference/spanner/2.20.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.19.0](/cpp/docs/reference/spanner/2.19.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.18.0](/cpp/docs/reference/spanner/2.18.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.17.0](/cpp/docs/reference/spanner/2.17.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.16.0](/cpp/docs/reference/spanner/2.16.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.15.1](/cpp/docs/reference/spanner/2.15.1/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.14.0](/cpp/docs/reference/spanner/2.14.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.13.0](/cpp/docs/reference/spanner/2.13.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.12.0](/cpp/docs/reference/spanner/2.12.0/spanner_admin_1_1InstanceAdminClient-retry-snippet)\n- [2.11.0](/cpp/docs/reference/spanner/2.11.0/spanner_admin_1_1InstanceAdminClient-retry-snippet) \n\nOverride spanner_admin::InstanceAdminClient Retry Policies\n==========================================================\n\nThis shows how to override the retry policies for `spanner_admin::InstanceAdminClient`: \n\n namespace spanner = ::google::cloud::spanner;\n [](std::string const& project_id) {\n // An instance admin client is controlled by three policies. The retry\n // policy determines for how long the client will retry transient failures.\n auto retry_policy =\n google::cloud::spanner_admin::InstanceAdminLimitedTimeRetryPolicy(\n /*maximum_duration=*/std::chrono::minutes(25))\n .clone();\n // The backoff policy controls how long does the client waits to retry after\n // a transient failure. Here we configure a truncated exponential backoff\n // with jitter:\n // https://en.wikipedia.org/wiki/Exponential_backoff\n // https://cloud.google.com/storage/docs/exponential-backoff\n auto backoff_policy = spanner::ExponentialBackoffPolicy(\n /*initial_delay=*/std::chrono::seconds(2),\n /*maximum_delay=*/std::chrono::minutes(10),\n /*scaling=*/2.0)\n .clone();\n // The polling policy controls how the client waits for long-running\n // operations (such as creating new instances). `GenericPollingPolicy\u003c\u003e`\n // combines existing policies. In this case, keep polling until the\n // operation completes (with success or error) or 45 minutes, whichever\n // happens first. Initially pause for 10 seconds between polling requests,\n // increasing the pause by a factor of 4 until it becomes 2 minutes.\n auto polling_policy =\n spanner::GenericPollingPolicy\u003c\u003e(\n spanner::LimitedTimeRetryPolicy(\n /*maximum_duration=*/std::chrono::minutes(45)),\n spanner::ExponentialBackoffPolicy(\n /*initial_delay=*/std::chrono::seconds(10),\n /*maximum_delay=*/std::chrono::minutes(2),\n /*scaling=*/4.0))\n .clone();\n auto client = google::cloud::spanner_admin::InstanceAdminClient(\n google::cloud::spanner_admin::MakeInstanceAdminConnection(),\n google::cloud::Options{}\n .set\u003cgoogle::cloud::spanner_admin::InstanceAdminRetryPolicyOption\u003e(\n std::move(retry_policy))\n .set\u003c\n google::cloud::spanner_admin::InstanceAdminBackoffPolicyOption\u003e(\n std::move(backoff_policy))\n .set\u003c\n google::cloud::spanner_admin::InstanceAdminPollingPolicyOption\u003e(\n std::move(polling_policy)));\n\n // Use the client as usual.\n std::cout \u003c\u003c \"Available configs for project \" \u003c\u003c project_id \u003c\u003c \"\\n\";\n auto project = google::cloud::Project(project_id);\n for (auto& cfg : client.ListInstanceConfigs(project.FullName())) {\n if (!cfg) throw std::move(cfg).status();\n std::cout \u003c\u003c cfg-\u003ename() \u003c\u003c \"\\n\";\n }\n std::cout \u003c\u003c \"End of available configs\\n\";\n }\n\nThis shows how to create a custom idempotency policy: \n\n class CustomIdempotencyPolicy : public google::cloud::spanner_admin::\n InstanceAdminConnectionIdempotencyPolicy {\n public:\n ~CustomIdempotencyPolicy() override = default;\n std::unique_ptr\u003c\n google::cloud::spanner_admin::InstanceAdminConnectionIdempotencyPolicy\u003e\n clone() const override {\n return std::make_unique\u003cCustomIdempotencyPolicy\u003e(*this);\n }\n // Override inherited functions to define as needed.\n };"]]