Recipe · Works on: Cockroach Continuum, CockroachDB Cloud, self-hosted · Tested: weekly in CI against the latest CockroachDB · Stack: Python, TypeScript, Go, Java · Code:
recipes/transaction-patterns v1.0.0
Retry errors come with
SERIALIZABLE isolation itself: to guarantee a serial order, a database has to refuse
some transactions, and the client runs them again
(Transaction retry error reference).
CockroachDB runs every transaction at SERIALIZABLE unless told otherwise, while PostgreSQL’s default is
READ COMMITTED, so code that never saw a retry error elsewhere can meet one here.
The code
Every language has the same two functions:
- The retry loop (
run_transaction,runTransaction,RunTransaction): begins a transaction at the isolation level you pass, runs your function, commits, and on SQLSTATE40001rolls back and runs the whole function again after an exponential backoff with jitter, up to a retry limit. It retries nothing else, including40003(“result is ambiguous”), which is only safe to retry for an idempotent transaction. - The request (
request_leaveand friends): reads the day’s schedule, optionally withFOR UPDATE, and takes the doctor off call only if another doctor stays on. It has a hook between the read and the write that the demo and tests use to start the other doctor’s request at exactly that moment.
schema.sql holds the two tables from the CockroachDB docs’ isolation examples. Each test uses its own random
day, so the suites can share a database.
Official retry helpers, and why this project writes its own
The CockroachDB docs recommend a library’s own retry helper where one exists. This project uses one small loop in every language instead, so that all four behave the same and you can read exactly what a retry does:- Go:
crdbpgx.ExecuteTxfromcockroach-goretries inside the transaction fromSAVEPOINT cockroach_restart, and a Go test uses it to keep the rule. In v2.4.3, one thing to know: with a backoff policy (ExpBackoffRetryPolicy) it restarts with a plainBEGIN, so the retry runs at the session’s default isolation level, not the one inTxOptions. - Python:
run_transactionfromsqlalchemy-cockroachdbworks with the CockroachDB SQLAlchemy dialect. The payments ledger project uses it. - Java and Node.js: there’s no Cockroach Labs helper; the docs’ JDBC and node-postgres examples write a loop like this one.
Get the code
The code is in the Cockroach Cookbook repository. To fetch only this project at the version this page describes:Run it locally
You need thecockroach binary, v26.1 or
later, and the toolchain for the language you want.
Each suite has 8 tests, including one that uses the
inject_retry_errors_enabled session variable to check the
retry limit; Go’s also runs the race through crdbpgx.ExecuteTx.
Run it on CockroachDB Basic
These steps use a CockroachDB Basic cluster, which existing CockroachDB Cloud organizations can create. New organizations are on Cockroach Continuum: create a cluster with the Continuum quickstart and use its connection string in the same steps. Continuum pricing differs from the Basic prices on this page; see Continuum pricing.
- Create a Basic cluster and a SQL
user, then run
CREATE DATABASE txn_patterns;in a SQL shell on it. - Set
DATABASE_URLto that user’s connection string with/txn_patternsas the database andsslmode=verify-full, then run the demo and tests as above.
READ COMMITTED needs the sql.txn.read_committed_isolation.enabled cluster setting, which is true by
default; when it’s false, READ COMMITTED transactions run as SERIALIZABLE.
What it costs
CockroachDB Basic charges for Request Units (RUs) and storage, and scales to zero when idle: $0.20 per million RUs and $0.50 per GiB-month, with a $15 monthly allowance for pay-as-you-go organizations (Plan a CockroachDB Basic cluster, October 2026). A demo run is a few dozen short statements, and a test run a few hundred.Choosing an isolation level
- Start at
SERIALIZABLE, CockroachDB’s default, with the retry loop: it keeps rules like this one without any locking code, from the first deploy on a laptop node or a Basic cluster to a multi-region cluster. - If your app was built for
READ COMMITTEDand you can’t add retries,READ COMMITTEDon CockroachDB is the documented choice, but then every read a decision depends on needsFOR UPDATEorFOR SHARE.

