Transactions in code
We decide where transactions begin and end in a service, which isolation level to ask for, and what to do when the database refuses to commit. The database guarantees atomicity, but only for the boundaries the code draws. We will cover transaction boundaries and the unit of work, isolation levels with the anomalies each one allows, and retrying serialization failures and deadlocks.
#Section 12Transaction boundaries
A transaction groups statements so that they all take effect or none do. The database fundamentals guide covers how PostgreSQL achieves that. This section is about the decision every service makes, which code sits between BEGIN and COMMIT.
Wren places one transaction around each use case, in the service layer from Unit V. OrderService.place opens the transaction, the repositories for orders, order items, idempotency records and the outbox all run their statements inside it, and the service commits at the end. If inserting the items fails, the order row disappears too. The controller does not open transactions, because it does not know which writes belong together, and repositories do not either, because one repository cannot see what the others are doing.
The usual tool is a unit of work object or a context manager, with unit_of_work() as uow:, which borrows a connection, begins a transaction, hands the same connection to every repository, commits if the block finishes and rolls back if it raises. Frameworks offer the same thing as annotations, such as Spring’s @Transactional. They are convenient, and they hide a trap, since a method calling another annotated method on the same object may bypass the proxy and get no transaction at all.
The most important rule is what stays out. A transaction should contain database work and nothing else. Wren’s first version of order placement opened a transaction, inserted the order, called the payment provider to charge the card, and committed. The provider takes about 800 ms, so the transaction held its locks for 812 ms, connections were held for 812 ms, and when the provider had a slow day the pool emptied. Worse, if the commit failed after the charge succeeded, the customer paid for an order that did not exist.
The fixed version charges first using an idempotency key, then opens a short transaction to record the order and the payment result. Unit VIII’s outbox pattern handles the cases where something must happen reliably after the commit.
#Section 13Choosing an isolation level
When transactions run at the same time, each sees some mix of its own work and other transactions’ committed work. The isolation level controls how much of the others it can see. Weaker levels allow more anomalies, results that could not occur if transactions ran one at a time. The SQL standard defines four levels by the anomalies they forbid. PostgreSQL implements three distinct behaviours.
Read committed is PostgreSQL’s default. Each statement sees data committed before that statement began, so it never sees uncommitted data, a dirty read. Two reads of the same row within one transaction may differ if someone commits in between, a non-repeatable read, and a repeated range query may find new rows, a phantom. Repeatable read gives the whole transaction one snapshot taken at its first statement. PostgreSQL’s version also prevents phantoms, and if two transactions update the same row, the second fails rather than overwriting, which prevents the lost update of Part V. Serializable, implemented in PostgreSQL 9.1 (2011) with serializable snapshot isolation, guarantees that the outcome equals some one-at-a-time order, and aborts a transaction with SQLSTATE 40001 when it cannot.
The anomaly that surprises people is write skew. Wren’s rule says at least one courier must stay on shift in each zone. Asha and Ben are the only two on shift, and both tap “end shift” at the same moment. Each transaction counts couriers on shift, sees 2, decides one can leave, and updates its own row. The rows differ, so repeatable read sees no conflict, and both commit. Zero couriers remain.
Under serializable, PostgreSQL notices that the reads and writes cannot be ordered and aborts one with 40001. Under read committed, the alternative is a lock on something both transactions must touch, such as the zone row, with SELECT ... FOR UPDATE, so the second transaction waits and then counts again with a fresh snapshot.
Wren uses read committed for most work, with atomic statements and explicit locks where invariants matter, and serializable for a few flows such as courier shifts, where the rule spans rows. Serializable costs some throughput and requires retries, the subject of the next section.
#Section 14Retrying serialization failures and deadlocks
Under serializable or repeatable read, PostgreSQL may abort a transaction with SQLSTATE 40001, serialization failure, to protect the guarantee. At any isolation level it may abort one with 40P01, deadlock detected. Neither is a bug in the database. Both are the database telling the application to try again, and the application must be ready to do so.
A deadlock happens when two transactions each hold a lock the other needs. Wren’s bulk status update locks order 123 then 124. A concurrent cancellation locks 124 then 123. Each waits for the other forever. PostgreSQL checks for such cycles after a transaction has waited deadlock_timeout, 1 s by default, and aborts one victim so the other can proceed. The cheapest prevention is to lock rows in a consistent order, for example by sorting ids, so a cycle cannot form.
Retrying correctly has one non-obvious rule. Retry the whole transaction from BEGIN, re-reading everything, never just the statement that failed. The transaction’s earlier reads came from a snapshot that is now stale, and decisions made from them may be wrong. Code should therefore wrap the entire unit of work in a retry loop that catches only 40001 and 40P01. Other errors, such as a unique violation or a check constraint, will fail the same way again.
Retries need limits and spacing. Wren retries at most three times, waiting a random time between half and all of 10 ms, then 20 ms, then 40 ms. The randomness, called jitter, keeps two transactions that collided from colliding again in lockstep, and the growing wait, exponential backoff, keeps a hot row from turning retries into a storm. If all attempts fail, the request returns 503 and a retryable error.
Retried transactions must also be safe to repeat. If the transaction body sends an email or calls an API, a retry sends it twice. Keep side effects outside the transaction, or record them in an outbox row that commits with the data, so a retry of the transaction is invisible to the outside world.