Database Transactions

A transaction groups operations so they either all succeed or all fail — the foundation of data integrity when many users hit the database at once. The classic example is a money transfer: debit one account, credit another, with no possibility of one happening without the other.

Beyond atomicity, transactions let you tune how much concurrent operations can interfere via isolation levels — a direct trade-off between consistency and performance.

TL;DR

Quick Example

A transfer that's atomic — both updates commit together, or the whole thing rolls back:

Core Concepts

ACID

Isolation levels & anomalies

Higher isolation prevents more anomalies but reduces concurrency. Read Committed is a common default; use Serializable for the strictest correctness.

Locking

Best Practices

Common Mistakes

Long-running transactions

Not handling deadlocks

FAQ

What does ACID actually guarantee?

That a transaction is atomic (all-or-nothing), moves the database between valid states, is isolated from other concurrent transactions, and—once committed—durably survives crashes. Together these let you reason about concurrent writes safely.

Which isolation level should I use?

Start with the database default (often Read Committed) and raise it only where you need stronger guarantees. Use Repeatable Read or Serializable for operations that must not see non-repeatable or phantom reads, accepting more contention and possible retries.

Optimistic or pessimistic locking?

Optimistic locking (version checks) suits low-contention data — it avoids holding locks and retries on the rare conflict. Pessimistic locking (FOR UPDATE) suits hot rows where conflicts are frequent, trading concurrency for certainty.

What causes deadlocks and how do I handle them?

Deadlocks happen when two transactions wait on locks each other holds. The database detects this and aborts one. Handle it by retrying the aborted transaction with backoff, and reduce frequency by acquiring locks in a consistent order and keeping transactions short.

Related Topics

References