Skip to main content
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
One small rule, kept four ways, in four languages, on CockroachDB. A hospital needs at least one doctor on call every day. Two doctors on call for the same day each ask for that day off at the same moment. Each request reads the schedule, sees another doctor on call, and takes its own doctor off. Whether the hospital ends the day with nobody on call depends on the isolation level and on how the code reads. It goes with the Cockroach University course Transactions on CockroachDB: SERIALIZABLE, READ COMMITTED and Retries. Each language runs the same race: Betty reads the schedule, Abe’s whole request runs and commits, then Betty decides and writes. The demo prints the outcome, and the tests check it. Results, the same in Python, TypeScript, Go and Java, on CockroachDB v26.1: 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 SQLSTATE 40001 rolls back and runs the whole function again after an exponential backoff with jitter, up to a retry limit. It retries nothing else, including 40003 (“result is ambiguous”), which is only safe to retry for an idempotent transaction.
  • The request (request_leave and friends): reads the day’s schedule, optionally with FOR 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.ExecuteTx from cockroach-go retries inside the transaction from SAVEPOINT 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 plain BEGIN, so the retry runs at the session’s default isolation level, not the one in TxOptions.
  • Python: run_transaction from sqlalchemy-cockroachdb works 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 the cockroach binary, v26.1 or later, and the toolchain for the language you want.
Then, in the language’s directory: 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.
  1. Create a Basic cluster and a SQL user, then run CREATE DATABASE txn_patterns; in a SQL shell on it.
  2. Set DATABASE_URL to that user’s connection string with /txn_patterns as the database and sslmode=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 COMMITTED and you can’t add retries, READ COMMITTED on CockroachDB is the documented choice, but then every read a decision depends on needs FOR UPDATE or FOR SHARE.

License

The code is Apache-2.0 licensed.