SERIALIZABLE is the default isolation level
CockroachDB runs SERIALIZABLE by default. It is the strongest ANSI isolation level. It guarantees that concurrent transactions produce a result equivalent to some serial order. Under this guarantee, the following anomalies cannot occur:
- Lost updates. Two buyers decrement the last unit and both commit. This is the overselling bug.
- Write skew. Two transactions each read a consistent view and write based on it, but their combined effect violates an invariant.
- Phantom reads. A row appears or disappears mid-transaction.
Handling 40001 retry errors
Because the database does not permit a non-serializable outcome, a transaction that would violate serializability is aborted and must be retried. All such errors share SQLSTATE 40001 with a message containing “restart transaction.” The application must wrap transactions in a retry loop. This is required for every transaction, and the loop is the same in each case:
[!NOTE] One exception:Many drivers and frameworks, including Cockroach Labs’ own adapters, provide this loop. Use the provided loop where one exists, but do not run transactions without one.RETRY_COMMIT_DEADLINE_EXCEEDEDMost40001errors should be retried, and the loop does not need to distinguish the subtypes. The exception isRETRY_COMMIT_DEADLINE_EXCEEDED, where retrying “may not be advisable” because the transaction is too long. The fix is to shorten the transaction rather than retry it. Keep order and inventory transactions small.
READ COMMITTED for extreme contention
READ COMMITTED is GA in v24.1 (preview in v23.2). Under it, CockroachDB retries individual statements transparently, so the transaction never returns 40001 to the client, and no client retry loop is required. In exchange, it permits non-repeatable reads, phantoms, lost updates, and write skew, which are the anomalies that SERIALIZABLE forbids.
READ COMMITTED is therefore useful where the contention overhead of SERIALIZABLE is significant, such as a highly contended SKU, but only when correctness is re-established with explicit locks or guards:
- A single guarded
UPDATE ... WHERE available >= qtyis safe underREAD COMMITTEDbecause the row is locked and re-checked at write time. - A read-then-write split across two statements is not safe under
READ COMMITTEDwithoutSELECT ... FOR UPDATE.
BEGIN TRANSACTION ISOLATION LEVEL READ COMMITTED) or per session or role (default_transaction_isolation). It is enabled cluster-wide by default via sql.txn.read_committed_isolation.enabled.
[!CAUTION] The rule forREAD COMMITTEDREAD COMMITTEDdoes not weaken invariants without cost. Keep every guard (WHERE … >= …) and everyFOR UPDATE.READ COMMITTEDremoves serialization retries; it does not remove the need to prevent lost updates in the application. When you move a transaction toREAD COMMITTED, document which anomalies the application now owns (lost update, write skew, phantoms) and how each is prevented.
Reducing contention
The documented levers, roughly in order of reach:- Touch fewer rows and use smaller transactions. The less a transaction reads, and the shorter it is open, the smaller the conflict window.
- Lock early with
SELECT … FOR UPDATE. Acquire the exclusive lock up front so that contenders queue in read order instead of colliding and retrying. The documentation notes that this increases throughput and decreases tail latency for contended operations. - Read historical data with
AS OF SYSTEM TIME. Historical and follower reads do not conflict with writes, which suits history and dashboards. - Use
UPSERTinstead of read-then-insert or update where applicable. - Send the transaction as a single batch so that CockroachDB can auto-retry more of it server-side.
Locking details worth knowing
UPDATEandUPSERTalready take an implicitFOR UPDATEon the rows they touch.FOR SHAREtakes a shared (multi-reader) lock. It is primarily forREAD COMMITTED, because shared locks are not enabled by default forSERIALIZABLE.- Under
SERIALIZABLE,FOR UPDATElocks are by default fast, in-memory, unreplicated locks. They are best-effort and can be dropped on a lease transfer or range split. Serializability is still guaranteed in that case; only the queueing benefit is lost. Setenable_durable_locking_for_serializable = trueif the locks must survive those events, at a latency cost. This caveat does not apply underREAD COMMITTED. - Wait policies:
SKIP LOCKEDskips already-locked rows, which is useful for queue-like scans, andNOWAITfails instead of waiting.
Claiming work without blocking (SKIP LOCKED)
For queue-like workloads, such as a worker pool draining the outbox, reclaiming expired reservations, or processing jobs, each worker should claim a different row without blocking on rows another worker already holds. SELECT ... FOR UPDATE SKIP LOCKED locks and returns only currently unlocked rows, skipping the rest:

