Library reference docs
Namespaces
google::cloud::spanner
Classes
Client
Functions
DatabaseAdminClient
Functions
LimitedErrorCountTransactionRerunPolicy
LimitedTimeTransactionRerunPolicy
Structs
Functions
Operators
google::cloud::spanner_admin
Classes
DatabaseAdminClient
Functions
DatabaseAdminConnectionIdempotencyPolicy
DatabaseAdminLimitedErrorCountRetryPolicy
DatabaseAdminLimitedTimeRetryPolicy
InstanceAdminClient
Functions
InstanceAdminConnectionIdempotencyPolicy
InstanceAdminLimitedErrorCountRetryPolicy
InstanceAdminLimitedTimeRetryPolicy
Structs
google::cloud::spanner_admin_mocks
Stay organized with collections Save and categorize content based on your preferences.
When it is safe to do so, the library automatically retries requests that fail due to a transient error. The library then uses [exponential backoff] to delay before trying again. Which operations are considered safe to retry, which errors are treated as transient failures, the parameters of the exponential backoff algorithm, and the limits of library retries, are all configurable via policies.
The library provides defaults for any policy that is not set. This document provides examples showing how to override those default policies.
The policies can be set when a Connection
, object is created. Some of the policies can also be overridden when the corresponding 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 those clients. Finally, some retry policies can be overridden when calling a specific Client
member function.
The library uses two different policy options to control the retry loops.
Configuring the transient errors and retry durationThe SpannerRetryPolicyOption
controls:
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.In most cases, only kUnavailable and kResourceExhausted are treated as a transient errors.
See Alsogoogle::cloud::spanner::SpannerRetryPolicyOption
google::cloud::spanner::RetryPolicy
google::cloud::spanner::LimitedErrorCountRetryPolicy
google::cloud::spanner::LimitedTimeRetryPolicy
The SpannerBackoffPolicyOption
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.
The only built-in backoff policy is ExponentialBackoffPolicy
. 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.
google::cloud::spanner::SpannerBackoffPolicyOption
google::cloud::spanner::BackoffPolicy
google::cloud::spanner::ExponentialBackoffPolicy
For example, this will override the retry and backoff policies through options passed to spanner::MakeConnection()
:
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(16),
/*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;
}
Controlling which commits are rerunnable
The library also uses a special TransactionRerunPolicy
to control how the commit of a read-write transaction will be reattempted after a failure with a rerunnable status (typically kAborted). The lock priority of the commit increases after each rerun, meaning that the next attempt has a slightly better chance of success.
You can provide your own class for this policy. The library also provides two built-in policies:
LimitedErrorCountTransactionRerunPolicy
: stops rerunning the commit after a specified number of rerunnable errors.LimitedTimeTransactionRerunPolicy
: stops rerunnable the commit after a specified time.void CommitWithPolicies(google::cloud::spanner::Client client) {
using ::google::cloud::StatusOr;
namespace spanner = ::google::cloud::spanner;
auto commit = client.Commit(
[&client](spanner::Transaction txn) -> StatusOr<spanner::Mutations> {
auto update = client.ExecuteDml(
std::move(txn),
spanner::SqlStatement(
"UPDATE Albums SET MarketingBudget = MarketingBudget * 2"
" WHERE SingerId = 1 AND AlbumId = 1"));
if (!update) return std::move(update).status();
return spanner::Mutations{};
},
// Retry for up to 42 minutes.
spanner::LimitedTimeTransactionRerunPolicy(std::chrono::minutes(42))
.clone(),
// After a failure backoff for 2 seconds (with jitter), then triple the
// backoff time on each retry, up to 5 minutes.
spanner::ExponentialBackoffPolicy(std::chrono::seconds(2),
std::chrono::minutes(5), 3.0)
.clone());
if (!commit) throw std::move(commit).status();
std::cout << "commit-with-policies was successful\n";
}
See Also
google::cloud::spanner::TransactionRerunPolicy
google::cloud::spanner::LimitedErrorCountTransactionRerunPolicy
google::cloud::spanner::LimitedTimeTransactionRerunPolicy
google::cloud::BackoffPolicy
Follow these links to find examples for other spanner *Client
classes:
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-14 UTC.
[[["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-08-14 UTC."],[[["The document details how to configure retry, backoff, and re-run policies for the Spanner C++ client library, allowing customization of how the library handles transient errors and transaction reruns."],["Spanner offers `SpannerRetryPolicyOption` and `SpannerBackoffPolicyOption` to control transient error handling and backoff algorithms, respectively, with built-in options like `LimitedErrorCountRetryPolicy`, `LimitedTimeRetryPolicy`, and `ExponentialBackoffPolicy`."],["The library provides `TransactionRerunPolicy` to manage the retry behavior of read-write transactions, including built-in policies like `LimitedErrorCountTransactionRerunPolicy` and `LimitedTimeTransactionRerunPolicy` to control reruns after a failure."],["Policies can be set during the creation of `Connection` or `Client` objects, or even overridden during specific `Client` function calls, offering flexibility in applying different policies to different parts of the application."],["The library offers several links to help find examples for other spanner `*Client` classes such as `InstanceAdminClient` and `DatabaseAdminClient`."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4