# Override Retry, Backoff, and Re-Run Policies

3.7.0-rc 3.6.0 (latest) 3.5.0 3.4.0 3.3.0 3.2.0 2.48.0-rc 2.47.1 2.46.0 2.45.0 2.44.0 2.43.0 2.42.0 2.41.0 2.40.0 2.39.0 2.38.0 2.37.0 2.36.0 2.35.0 2.34.0 2.33.0 2.32.0 2.31.0 2.30.0 2.29.0 2.28.0 2.27.0 2.26.0 2.25.1 2.24.0 2.23.0 2.22.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.14.0 2.13.0 2.12.0 2.11.0 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`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/classgoogle_1_1cloud_1_1spanner_1_1Connection), object is created. Some of the policies can also be overridden when the corresponding [`Client`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/classgoogle_1_1cloud_1_1spanner_1_1Client) 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:

- 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.

In most cases, only [kUnavailable](https://docs.cloud.google.com/cpp/docs/reference/common/latest/namespacegoogle_1_1cloud_1a90e17f75452470f0f3ee1a06ffe58847.html) and [kResourceExhausted](https://docs.cloud.google.com/cpp/docs/reference/common/latest/namespacegoogle_1_1cloud_1a90e17f75452470f0f3ee1a06ffe58847.html) are treated as a transient errors.

###### See Also

[`google::cloud::spanner::SpannerRetryPolicyOption`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/structgoogle_1_1cloud_1_1spanner_1_1SpannerRetryPolicyOption)

###### See Also

[`google::cloud::spanner::RetryPolicy`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/classgoogle_1_1cloud_1_1spanner_1_1RetryPolicy)

###### See Also

[`google::cloud::spanner::LimitedErrorCountRetryPolicy`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/classgoogle_1_1cloud_1_1spanner_1_1LimitedErrorCountRetryPolicy)

###### See Also

[`google::cloud::spanner::LimitedTimeRetryPolicy`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/classgoogle_1_1cloud_1_1spanner_1_1LimitedTimeRetryPolicy)

## 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`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/structgoogle_1_1cloud_1_1spanner_1_1SpannerBackoffPolicyOption)

###### See Also

[`google::cloud::spanner::BackoffPolicy`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/namespacegoogle_1_1cloud_1_1spanner)

###### See Also

[`google::cloud::spanner::ExponentialBackoffPolicy`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/namespacegoogle_1_1cloud_1_1spanner)

## 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](about:invalid#zCSafez)). 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`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/classgoogle_1_1cloud_1_1spanner_1_1TransactionRerunPolicy)

###### See Also

[`google::cloud::spanner::LimitedErrorCountTransactionRerunPolicy`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/classgoogle_1_1cloud_1_1spanner_1_1LimitedErrorCountTransactionRerunPolicy)

###### See Also

[`google::cloud::spanner::LimitedTimeTransactionRerunPolicy`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/classgoogle_1_1cloud_1_1spanner_1_1LimitedTimeTransactionRerunPolicy)

## More Information

###### See Also

[`google::cloud::Options`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1Options.html)

###### See Also

[`google::cloud::RetryPolicy`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1RetryPolicy.html)

###### See Also

[`google::cloud::BackoffPolicy`](https://docs.cloud.google.com/cpp/docs/reference/common/latest/namespacegoogle_1_1cloud.html)
Follow these links to find examples for other spanner `*Client` classes:

- [`InstanceAdminClient`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/spanner_admin_1_1InstanceAdminClient-retry-snippet)
- [`DatabaseAdminClient`](https://docs.cloud.google.com/cpp/docs/reference/spanner/latest/spanner_admin_1_1DatabaseAdminClient-retry-snippet)