This page describes Spanner batch write requests and how you can use them to modify your Spanner data.
You can use Spanner batch write to insert, update, or delete multiple rows in your Spanner tables. Spanner batch write supports low latency writes without a read operation, and returns responses as mutations are applied in batches. To use batch write, you group related mutations together, and all mutations in a group are committed atomically. The mutations across groups are applied in an unspecified order and are independent of one another (non-atomic). Spanner doesn't need to wait for all mutations to be applied before sending a response, which means that batch write allows for partial failure. You can also execute multiple batch writes at a time. For more information, see How to use batch write.
Use cases
Spanner batch write is especially useful if you want to commit a large number of writes without a read operation, but don't require an atomic transaction for all your mutations.
If you want to batch your DML requests, use batch DML to modify your Spanner data. For more information on the differences between DML and mutations, see Comparing DML and mutations.
For single mutation requests, we recommend using a locking read-write transaction.
Limitations
Spanner batch write has the following limitations:
Spanner batch write is not available using the Google Cloud console or Google Cloud CLI. It is only available using REST and RPC APIs and the Spanner Java client library.
Replay protection is not supported using batch write. It's possible for mutations to be applied more than once, and a mutation that is applied more than once might result in a failure. For example, if an insert mutation is replayed, it might produce an already exists error, or if you use generated or commit timestamp-based keys in the mutation, it might result in additional rows being added to the table. We recommend structuring your writes to be idempotent to avoid this issue.
You can't rollback a completed batch write request. You can cancel an in-progress batch write request. If you cancel an in-progress batch write, mutations in uncompleted groups are rolled back. Mutations in completed groups are committed to the database.
The maximum size for a batch write request is the same as the limit for a commit request. For more information, see Limits for creating, reading, updating, and deleting data.
How to use batch write
To use batch write, you must have the spanner.databases.write
permission on
the database that you want to modify. You can batch write mutations
non-atomically in a single call using a REST
or RPC API
request call.
You should group the following mutation types together when using batch write:
- Inserting rows with the same primary key prefix in both the parent and child tables.
- Inserting rows into tables with a foreign key relationship between the tables.
- Other types of related mutations depending on your database schema and application logic.
You can also batch write using the Spanner Java client library.
The following code example updates the Singers
table with new rows.
Java
What's next
- Learn more about Spanner transactions.