Recipe · 20 minutes · Works on: Cockroach Continuum, CockroachDB Cloud, self-hosted · Verified on: CockroachDB v26.4.1, Dec 1, 2026
What you’ll fix
A stock reservation that reads a row, decides in the application, and writes it back. Under load it fails withSQLSTATE 40001 (restart transaction), or it sits waiting behind another transaction. You’ll reproduce both, find the blocker, and fix the write so concurrent requests queue briefly and succeed.
CockroachDB runs transactions at SERIALIZABLE isolation by default. When two transactions touch the same row, one of them either waits for the other’s lock or is asked to retry. That conflict is called transaction contention. A little is normal. A lot shows up as retry errors and slow requests.
Start a cluster
You need two SQL shells connected to the same cluster.- Cockroach Continuum
- Local (self-hosted)
Create a free cluster by following the Continuum quickstart, then open two SQL shells with the connection string from the Console.
Create the schema
Reproduce the retry error
Run these in order, switching shells where shown. Both transactions read100, and both try to write 99.
Shell A fails:
99 instead of 98: a lost update. Run ROLLBACK; in shell A.
Reproduce a blocked write
Reset the row withUPDATE inventory SET qty = 100 WHERE sku = 'sku-1';, then:
Shell B waits for shell A’s lock. In a real application, shell A is the request that opened a transaction and then called a slow service before committing.
Find the blocker
In the Console. Open the Transactions page and look at Active Executions. The waiting transaction showsWaiting in the Status column, and its details name the transaction blocking it. Use the Cloud Console Transactions page, or the DB Console Transactions page for self-hosted clusters. After the fact, the Insights page flags these as High Contention.
In SQL. From a third shell, join the cluster’s locks to the transactions that hold or want them:
granted = t holds the lock: that session is the blocker. The row with granted = f is waiting for it. Reading these tables needs the VIEWACTIVITY privilege, which admin users have.
If the blocker is stuck, cancel its session. Its transaction rolls back and the waiting update completes:
crdb_internal.transaction_contention_events records each wait as a LOCK_WAIT event, and crdb_internal.cluster_contended_indexes shows which indexes saw the most contention.
Fix it
Pick the first fix that fits. Each was run against the same two-shell setup. 1. Do it in one statement. Let the database do the read, the check, and the write. A single statement is an implicit transaction that CockroachDB can retry for you, and it holds the lock only for as long as the statement runs:qty drops by exactly 8. No rows returned means out of stock.
2. If you must read first, lock the row as you read it. SELECT ... FOR UPDATE takes the lock at the read, so the second transaction waits instead of failing later:
FOR UPDATE on both reads. Shell B’s read now waits at step 4. Finish shell A with its UPDATE and COMMIT, and shell B’s read returns 99. Its update then writes 98. Nothing fails, and no update is lost.
3. Keep transactions short. Never call another service, wait on a user, or sleep inside a transaction. Do that work before BEGIN or after COMMIT.
4. Always handle 40001 anyway. Contention can’t be removed entirely. Wrap explicit transactions in a client-side retry loop, with backoff. Pair it with an idempotency key so a retry can never apply twice.
How it fails
Trade-offs and when not to use it
FOR UPDATEturns retries into waiting. That is usually better, but a long transaction holding the lock now blocks everyone. Keep the locked section short.- The single statement only works when the decision fits in SQL. If the business rule needs data from another service, fetch it before the transaction.
READ COMMITTEDchanges the trade-off. UnderREAD COMMITTEDisolation you see fewer retry errors, but read-then-write patterns can lose updates unless you lock withFOR UPDATE.
On Cockroach Continuum with Cockroach Aegis (private preview): Aegis reports contention as it happens and explains which transactions conflict. See Understand transaction contention.
Give it to your agent
Go deeper
- Transaction retry error reference
- Transactions and
SELECT FOR UPDATE - Performance best practices: transaction contention
- Performance Tuning Recipes, where this recipe came from
Practice transactions in Cockroach University
Free developer path with hands-on browser labs.

