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. With its default optimistic concurrency, 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. With pessimistic concurrency,
read operations use snapshots, but exclusive locks apply to data read from FOR
UPDATE queries or lock_scanned_ranges=exclusive hints, and data written with
DML queries. Pessimistic concurrency also reduces the likelihood of write-write
conflicts. For more
information, see Isolation level overview and
Concurrency control.
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
C++
C#
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.
You can use Spanner's drivers to set isolation level and read
lock mode as a connection parameter at the connection level or as a SET
statement option at the transaction level. For more information about each
driver, see Overview of drivers.
You can also configure the locking concurrency for each isolation level. For more information, see Concurrency control.
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 about Concurrency control.
Learn how to use SELECT FOR UPDATE in repeatable read isolation.
Learn more about Spanner serializability and external consistency, see TrueTime and external consistency.