A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cloud.google.com/cpp/docs/reference/spanner/latest/spanner-retry-policies below:

C++ Client Libraries | Google Cloud

Skip to main content

Stay organized with collections Save and categorize content based on your preferences.

Override Retry, Backoff, and Re-Run Policies

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 duration

The SpannerRetryPolicyOption controls:

You can provide your own class for this option. The library also provides two built-in policies:

In most cases, only kUnavailable and kResourceExhausted are treated as a transient errors.

See Also

google::cloud::spanner::SpannerRetryPolicyOption

See Also

google::cloud::spanner::RetryPolicy

See Also

google::cloud::spanner::LimitedErrorCountRetryPolicy

See Also

google::cloud::spanner::LimitedTimeRetryPolicy

Controlling the backoff algorithm

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.

See Also

google::cloud::spanner::SpannerBackoffPolicyOption

See Also

google::cloud::spanner::BackoffPolicy

See Also

google::cloud::spanner::ExponentialBackoffPolicy

Example

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:

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

See Also

google::cloud::spanner::LimitedErrorCountTransactionRerunPolicy

See Also

google::cloud::spanner::LimitedTimeTransactionRerunPolicy

More Information See Also

google::cloud::Options

See Also

google::cloud::RetryPolicy

See Also

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."],[[["This document provides information on how to override default retry, backoff, and re-run policies in the Google Cloud Spanner C++ library."],["The `SpannerRetryPolicyOption` controls which errors are considered transient and how long the library will retry, with built-in options like `LimitedErrorCountRetryPolicy` and `LimitedTimeRetryPolicy`."],["The `SpannerBackoffPolicyOption` controls the delay before retrying after a transient error, with `ExponentialBackoffPolicy` as the only built-in option, which implements exponential backoff with jitter."],["The `TransactionRerunPolicy` controls how commits of read-write transactions are reattempted, with built-in policies `LimitedErrorCountTransactionRerunPolicy` and `LimitedTimeTransactionRerunPolicy`."],["The latest version of the documentation is 2.37.0-rc, and the page provides documentation links to a number of different versions all the way down to 2.11.0."]]],[]]


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