When it is safe to do so, the library automatically retries requests that fail due to a transient error. The library then usesexponential backoffto backoff before trying again. Which operations are considered safe to retry, which errors are treated as transient failures, the details of the exponential backoff algorithm, and for how long the library retries are all configurable via policies.
This document provides examples showing how to override the default policies.
The policies can be set when the*Connectionobject is created. The library provides default policies for any policy that is not set. The application can also override some (or all) policies when the*Clientobject is created. This can be useful if multiple*Clientobjects share the same*Connectionobject, but you want different retry behavior in some of the clients. Finally, the application can override some retry policies when calling a specific member function.
The library uses three different options to control the retry loop. The options have per-client names.
Configuring the transient errors and retry duration
The*RetryPolicyOptioncontrols:
Which errors are to be treated as transient errors.
How long the library will keep retrying transient errors.
You can provide your own class for this option. The library also provides two built-in policies:
*LimitedErrorCountRetryPolicy: stops retrying after a specified number of transient errors.
*LimitedTimeRetryPolicy: stops retrying after a specified time.
Note that a library may have more than one version of these classes. Their name match the*Clientand*Connectionobject they are intended to be used with. Some*Clientobjects treat different error codes as transient errors. In most cases, onlykUnavailableis treated as a transient error.
Controlling the backoff algorithm
The*BackoffPolicyOptioncontrols how long the client library will wait before retrying a request that failed with a transient error. You can provide your own class for this option.
The only built-in backoff policy isExponentialBackoffPolicy. This class implements a truncated exponential backoff algorithm, with jitter. In summary, it doubles the current backoff time after each failure. The actual backoff time for an RPC is chosen at random, but never exceeds the current backoff. The current backoff is doubled after each failure, but never exceeds (or is "truncated") if it reaches a prescribed maximum.
Controlling which operations are retryable
The*IdempotencyPolicyOptioncontrols which requests are retryable, as some requests are never safe to retry.
Only one built-in idempotency policy is provided by the library. The name matches the name of the client it is intended for. For example,FooBarClientwill useFooBarIdempotencyPolicy. This policy is very conservative.
Example
For example, this will override the retry policies forconfig_v1::ConfigClient:
auto options =
google::cloud::Options{}
.set<google::cloud::config_v1::
ConfigConnectionIdempotencyPolicyOption>(
CustomIdempotencyPolicy().clone())
.set<google::cloud::config_v1::ConfigRetryPolicyOption>(
google::cloud::config_v1::ConfigLimitedErrorCountRetryPolicy(3)
.clone())
.set<google::cloud::config_v1::ConfigBackoffPolicyOption>(
google::cloud::ExponentialBackoffPolicy(
/*initial_delay=*/std::chrono::milliseconds(200),
/*maximum_delay=*/std::chrono::seconds(45),
/*scaling=*/2.0)
.clone());
auto connection = google::cloud::config_v1::MakeConfigConnection(options);
// c1 and c2 share the same retry policies
auto c1 = google::cloud::config_v1::ConfigClient(connection);
auto c2 = google::cloud::config_v1::ConfigClient(connection);
// You can override any of the policies in a new client. This new client
// will share the policies from c1 (or c2) *except* for the retry policy.
auto c3 = google::cloud::config_v1::ConfigClient(
connection,
google::cloud::Options{}
.set<google::cloud::config_v1::ConfigRetryPolicyOption>(
google::cloud::config_v1::ConfigLimitedTimeRetryPolicy(
std::chrono::minutes(5))
.clone()));
// You can also override the policies in a single call:
// c3.SomeRpc(..., google::cloud::Options{}
// .set<google::cloud::config_v1::ConfigRetryPolicyOption>(
// google::cloud::config_v1::ConfigLimitedErrorCountRetryPolicy(10).clone()));
This assumes you have created a custom idempotency policy. Such as:
class CustomIdempotencyPolicy
: public google::cloud::config_v1::ConfigConnectionIdempotencyPolicy {
public:
~CustomIdempotencyPolicy() override = default;
std::unique_ptr<google::cloud::config_v1::ConfigConnectionIdempotencyPolicy>
clone() const override {
return std::make_unique<CustomIdempotencyPolicy>(*this);
}
// Override inherited functions to define as needed.
};
This will override the polling policies forconfig_v1::ConfigClient
// The polling policy controls how the client waits for long-running
// operations. `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 options =
google::cloud::Options{}
.set<google::cloud::config_v1::ConfigPollingPolicyOption>(
google::cloud::GenericPollingPolicy<
google::cloud::config_v1::ConfigRetryPolicyOption::Type,
google::cloud::config_v1::ConfigBackoffPolicyOption::Type>(
google::cloud::config_v1::ConfigLimitedTimeRetryPolicy(
/*maximum_duration=*/std::chrono::minutes(45))
.clone(),
google::cloud::ExponentialBackoffPolicy(
/*initial_delay=*/std::chrono::seconds(10),
/*maximum_delay=*/std::chrono::minutes(2),
/*scaling=*/4.0)
.clone())
.clone());
auto connection = google::cloud::config_v1::MakeConfigConnection(options);
// c1 and c2 share the same polling policies.
auto c1 = google::cloud::config_v1::ConfigClient(connection);
auto c2 = google::cloud::config_v1::ConfigClient(connection);
[[["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\u003eThe library automatically retries requests that fail due to transient errors, using exponential backoff for subsequent attempts, with configurable policies for retry behavior, transient error identification, and backoff algorithm details.\u003c/p\u003e\n"],["\u003cp\u003eRetry policies, such as \u003ccode\u003e*RetryPolicyOption\u003c/code\u003e, \u003ccode\u003e*BackoffPolicyOption\u003c/code\u003e, and \u003ccode\u003e*IdempotencyPolicyOption\u003c/code\u003e, can be set during the creation of \u003ccode\u003e*Connection\u003c/code\u003e objects, \u003ccode\u003e*Client\u003c/code\u003e objects, or even on individual member function calls, offering granular control over retry logic.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003e*RetryPolicyOption\u003c/code\u003e controls which errors are considered transient and the duration of retries, with built-in policies like \u003ccode\u003e*LimitedErrorCountRetryPolicy\u003c/code\u003e and \u003ccode\u003e*LimitedTimeRetryPolicy\u003c/code\u003e, and \u003ccode\u003eExponentialBackoffPolicy\u003c/code\u003e is the only built in backoff policy.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003e*IdempotencyPolicyOption\u003c/code\u003e determines which requests are safe to retry, and the library provides a conservative default policy that only one built in Idempotency policy is provided by the library.\u003c/p\u003e\n"],["\u003cp\u003eThe polling policy for long-running operations uses \u003ccode\u003eGenericPollingPolicy<>\u003c/code\u003e that combines retry and backoff policies, allowing configurable wait times and scaling factors between polling requests.\u003c/p\u003e\n"]]],[],null,["Version latestkeyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/config/latest/config-override-retry)\n- [2.41.0](/cpp/docs/reference/config/2.41.0/config-override-retry)\n- [2.40.0](/cpp/docs/reference/config/2.40.0/config-override-retry)\n- [2.39.0](/cpp/docs/reference/config/2.39.0/config-override-retry)\n- [2.38.0](/cpp/docs/reference/config/2.38.0/config-override-retry)\n- [2.37.0](/cpp/docs/reference/config/2.37.0/config-override-retry)\n- [2.36.0](/cpp/docs/reference/config/2.36.0/config-override-retry)\n- [2.35.0](/cpp/docs/reference/config/2.35.0/config-override-retry)\n- [2.34.0](/cpp/docs/reference/config/2.34.0/config-override-retry)\n- [2.33.0](/cpp/docs/reference/config/2.33.0/config-override-retry)\n- [2.32.0](/cpp/docs/reference/config/2.32.0/config-override-retry)\n- [2.31.0](/cpp/docs/reference/config/2.31.0/config-override-retry)\n- [2.30.0](/cpp/docs/reference/config/2.30.0/config-override-retry)\n- [2.29.0](/cpp/docs/reference/config/2.29.0/config-override-retry)\n- [2.28.0](/cpp/docs/reference/config/2.28.0/config-override-retry)\n- [2.27.0](/cpp/docs/reference/config/2.27.0/config-override-retry)\n- [2.26.0](/cpp/docs/reference/config/2.26.0/config-override-retry)\n- [2.25.1](/cpp/docs/reference/config/2.25.1/config-override-retry)\n- [2.24.0](/cpp/docs/reference/config/2.24.0/config-override-retry)\n- [2.23.0](/cpp/docs/reference/config/2.23.0/config-override-retry)\n- [2.22.1](/cpp/docs/reference/config/2.22.1/config-override-retry)\n- [2.21.0](/cpp/docs/reference/config/2.21.0/config-override-retry)\n- [2.20.0](/cpp/docs/reference/config/2.20.0/config-override-retry)\n- [2.19.0](/cpp/docs/reference/config/2.19.0/config-override-retry)\n- [2.18.0](/cpp/docs/reference/config/2.18.0/config-override-retry)\n- [2.17.0](/cpp/docs/reference/config/2.17.0/config-override-retry)\n- [2.16.0](/cpp/docs/reference/config/2.16.0/config-override-retry) \n\nOverride Retry, Backoff, and Idempotency Policies\n=================================================\n\nWhen it is safe to do so, the library automatically retries requests that fail due to a transient error. The library then uses [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff) to backoff before trying again. Which operations are considered safe to retry, which errors are treated as transient failures, the details of the exponential backoff algorithm, and for how long the library retries are all configurable via policies.\n\nThis document provides examples showing how to override the default policies.\n\nThe policies can be set when the `*Connection` object is created. The library provides default policies for any policy that is not set. The application can also override some (or all) policies when the `*Client` object is created. This can be useful if multiple `*Client` objects share the same `*Connection` object, but you want different retry behavior in some of the clients. Finally, the application can override some retry policies when calling a specific member function.\n\nThe library uses three different options to control the retry loop. The options have per-client names.\n\nConfiguring the transient errors and retry duration\n---------------------------------------------------\n\nThe `*RetryPolicyOption` controls:\n\n- Which errors are to be treated as transient errors.\n- How long the library will keep retrying transient errors.\n\nYou can provide your own class for this option. The library also provides two built-in policies:\n\n- `*LimitedErrorCountRetryPolicy`: stops retrying after a specified number of transient errors.\n- `*LimitedTimeRetryPolicy`: stops retrying after a specified time.\n\nNote that a library may have more than one version of these classes. Their name match the `*Client` and `*Connection` object they are intended to be used with. Some `*Client` objects treat different error codes as transient errors. In most cases, only [kUnavailable](https://cloud.google.com/cpp/docs/reference/common/latest/namespacegoogle_1_1cloud_1a90e17f75452470f0f3ee1a06ffe58847.html) is treated as a transient error.\n\nControlling the backoff algorithm\n---------------------------------\n\nThe `*BackoffPolicyOption` controls how long the client library will wait before retrying a request that failed with a transient error. You can provide your own class for this option.\n\nThe only built-in backoff policy is [`ExponentialBackoffPolicy`](https://cloud.google.com/cpp/docs/reference/common/latest/namespacegoogle_1_1cloud.html). This class implements a truncated exponential backoff algorithm, with jitter. In summary, it doubles the current backoff time after each failure. The actual backoff time for an RPC is chosen at random, but never exceeds the current backoff. The current backoff is doubled after each failure, but never exceeds (or is \"truncated\") if it reaches a prescribed maximum.\n\nControlling which operations are retryable\n------------------------------------------\n\nThe `*IdempotencyPolicyOption` controls which requests are retryable, as some requests are never safe to retry.\n\nOnly one built-in idempotency policy is provided by the library. The name matches the name of the client it is intended for. For example, `FooBarClient` will use `FooBarIdempotencyPolicy`. This policy is very conservative.\n\nExample\n-------\n\nFor example, this will override the retry policies for `config_v1::ConfigClient`: \n\n auto options =\n google::cloud::Options{}\n .set\u003cgoogle::cloud::config_v1::\n ConfigConnectionIdempotencyPolicyOption\u003e(\n CustomIdempotencyPolicy().clone())\n .set\u003cgoogle::cloud::config_v1::ConfigRetryPolicyOption\u003e(\n google::cloud::config_v1::ConfigLimitedErrorCountRetryPolicy(3)\n .clone())\n .set\u003cgoogle::cloud::config_v1::ConfigBackoffPolicyOption\u003e(\n google::cloud::ExponentialBackoffPolicy(\n /*initial_delay=*/std::chrono::milliseconds(200),\n /*maximum_delay=*/std::chrono::seconds(45),\n /*scaling=*/2.0)\n .clone());\n auto connection = google::cloud::config_v1::MakeConfigConnection(options);\n\n // c1 and c2 share the same retry policies\n auto c1 = google::cloud::config_v1::ConfigClient(connection);\n auto c2 = google::cloud::config_v1::ConfigClient(connection);\n\n // You can override any of the policies in a new client. This new client\n // will share the policies from c1 (or c2) *except* for the retry policy.\n auto c3 = google::cloud::config_v1::ConfigClient(\n connection,\n google::cloud::Options{}\n .set\u003cgoogle::cloud::config_v1::ConfigRetryPolicyOption\u003e(\n google::cloud::config_v1::ConfigLimitedTimeRetryPolicy(\n std::chrono::minutes(5))\n .clone()));\n\n // You can also override the policies in a single call:\n // c3.SomeRpc(..., google::cloud::Options{}\n // .set\u003cgoogle::cloud::config_v1::ConfigRetryPolicyOption\u003e(\n // google::cloud::config_v1::ConfigLimitedErrorCountRetryPolicy(10).clone()));\n\nThis assumes you have created a custom idempotency policy. Such as: \n\n class CustomIdempotencyPolicy\n : public google::cloud::config_v1::ConfigConnectionIdempotencyPolicy {\n public:\n ~CustomIdempotencyPolicy() override = default;\n std::unique_ptr\u003cgoogle::cloud::config_v1::ConfigConnectionIdempotencyPolicy\u003e\n clone() const override {\n return std::make_unique\u003cCustomIdempotencyPolicy\u003e(*this);\n }\n // Override inherited functions to define as needed.\n };\n\nThis will override the polling policies for `config_v1::ConfigClient` \n\n\n // The polling policy controls how the client waits for long-running\n // operations. `GenericPollingPolicy\u003c\u003e` combines existing policies.\n // In this case, keep polling until the operation completes (with success\n // or error) or 45 minutes, whichever happens first. Initially pause for\n // 10 seconds between polling requests, increasing the pause by a factor\n // of 4 until it becomes 2 minutes.\n auto options =\n google::cloud::Options{}\n .set\u003cgoogle::cloud::config_v1::ConfigPollingPolicyOption\u003e(\n google::cloud::GenericPollingPolicy\u003c\n google::cloud::config_v1::ConfigRetryPolicyOption::Type,\n google::cloud::config_v1::ConfigBackoffPolicyOption::Type\u003e(\n google::cloud::config_v1::ConfigLimitedTimeRetryPolicy(\n /*maximum_duration=*/std::chrono::minutes(45))\n .clone(),\n google::cloud::ExponentialBackoffPolicy(\n /*initial_delay=*/std::chrono::seconds(10),\n /*maximum_delay=*/std::chrono::minutes(2),\n /*scaling=*/4.0)\n .clone())\n .clone());\n\n auto connection = google::cloud::config_v1::MakeConfigConnection(options);\n\n // c1 and c2 share the same polling policies.\n auto c1 = google::cloud::config_v1::ConfigClient(connection);\n auto c2 = google::cloud::config_v1::ConfigClient(connection);\n\nMore Information\n----------------\n\n###### See Also\n\n[`google::cloud::Options`](https://cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1Options.html)\n\n###### See Also\n\n[`google::cloud::BackoffPolicy`](https://cloud.google.com/cpp/docs/reference/common/latest/namespacegoogle_1_1cloud.html)\n\n###### See Also\n\n[`google::cloud::ExponentialBackoffPolicy`](https://cloud.google.com/cpp/docs/reference/common/latest/namespacegoogle_1_1cloud.html)"]]