This page describes how to use repeatable read isolation in Spanner.
Repeatable read is an isolation level that ensures that all read operations within a transaction see a consistent snapshot of the database as it existed at the start of the transaction. In Spanner, this isolation level is implemented using a technique that is also commonly called snapshot isolation. This approach is beneficial in high read-write concurrency scenarios where numerous transactions read data that other transactions might be modifying. By using a fixed snapshot, repeatable read avoids the performance impacts of the more rigorous serializable isolation level. Reads can execute without acquiring locks and without blocking concurrent writes, which results in potentially fewer aborted transactions that might need to be retried due to serialization conflicts. For more information, see Isolation level overview.
Set the isolation level
You can set the isolation level on read-write transactions at the database client-level or the transaction-level using the following methods:
Client libraries
Go
Java
Node.js
Python
REST
You can use the TransactionOptions.isolation_level REST API to set the isolation level on read-write and read-only transactions at the transaction-level. The valid options are TransactionOptions.SERIALIZABLE and TransactionOptions.REPEATABLE_READ. By default, Spanner sets the isolation level to serializable isolation.
Limitations
The following set of limitations exist in the repeatable read isolation Preview.
- You might experience issues if your schema has check constraints.
- There is a known issue that prevents check constraints from being validated, which can result in constraint violations when transactions commit. Therefore we don't recommend using repeatable read isolation in Preview if your schema has check constraints.
- You might experience issues if concurrent schema changes occur in your database while transactions are executing.
- If your DML statements use the
last_statementoption and a concurrent schema change occurs while the DML statement executes, it might internally retry and return an error stating that the DML was retried incorrectly after thelast_statementoption was set. Retrying the transaction after the schema change applies resolves this issue. - If requests in a transaction experience a
DEADLINE_EXCEEDEDerror from the client, retry the transaction after the schema change applies to resolve the issue.
- If your DML statements use the
Unsupported use cases
- You can't set repeatable read isolation on partitioned DML transactions.
- All read-only transactions already operate at a fixed snapshot and don't require locks, so setting repeatable read isolation in this transaction type doesn't change any behavior.
- You can't set repeatable read isolation on read-only, single-use, and partition operations using the Spanner client libraries Spanner client libraries won't have the option to set the repeatable read isolation on read-only, single-use and partition query operations.
What's next
Learn more about isolation levels.
Learn how to use SELECT FOR UPDATE in repeatable read isolation.
Learn more about Spanner serializability and external consistency, see TrueTime and external consistency.