Skip to main content

Transaction, nested transaction, atomicity

Short answer

A transaction is an indivisible unit of operations with ACID properties, e.g., a bank transfer. Nested transactions are trees of sub-transactions that commit provisionally; a parent can retry if a child aborts. Atomicity is enforced by tentative updates, write-ahead logs for undo/redo, and strict two-phase locking with atomic commit.

Define transaction and nested transaction with examples. Explain how an atomicity is gained in transactions.

Transaction

A transaction is a sequence of operations on shared data that the system treats as one indivisible unit of work: either all operations take effect or none does. It is described by the ACID properties:

  • Atomicity: all or nothing.
  • Consistency: it moves the data from one consistent state to another.
  • Isolation: concurrent transactions do not see each other's intermediate results (appear serial).
  • Durability: once committed, results survive crashes (stored in permanent storage).

Example: a bank transfer debit A; credit B in one transaction.

Nested transaction

  • Nested transaction: a transaction made of sub-transactions in a tree. The top-level transaction contains child transactions, which may contain their own children. Sub-transactions at the same level can run concurrently, and a sub-transaction can abort without aborting its parent; the parent may retry or choose another action. Example: a book trip transaction with sub-transactions book flight, book hotel, book taxi. If the taxi booking fails, the parent can try another company.

Rules of nested transactions:

  • A sub-transaction commits only provisionally; its result becomes permanent only when all its ancestors commit up to the top level.
  • If a parent aborts, all its sub-transactions abort, even those that provisionally committed.
  • A child sees the parent's changes, but the parent does not see the child's until the child commits.

How atomicity is gained

Atomicity means that after a failure or abort no partial effect remains. It is achieved by:

  1. Private workspace / tentative versions: updates are made on tentative copies, and they become permanent only at commit.
  2. Write-ahead log: before any change, the old and new values are written to a log on stable storage. On abort or crash, uncommitted changes are undone, and committed ones redone from the log.
  3. Shadow paging: the database is switched to the new page table atomically at commit.
  4. Atomic commit (two-phase commit) for distributed transactions, so all servers commit or all abort.
  5. Concurrency control (strict 2PL), which stops other transactions from seeing uncommitted data and so avoids cascading aborts.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Distributed System chapter 8 questions →