Free sample lesson · ACID 完全指南:四個保證與 Isolation Level 實務

Atomicity 與 rollback

Lesson 1 of the Wondering course ACID 完全指南:四個保證與 Isolation Level 實務

All or Nothing

A transaction groups several database operations into one unit of work.

The rule is simple. Either every operation takes effect, or none of them do.

That guarantee is called atomicity. An atom cannot be split, and neither can a finished transaction.

Think of a bank transfer. Money leaving one account and arriving in another must succeed together.

A transfer shown as one indivisible unit of work

Notice that both the debit and the credit sit inside a single sealed boundary, so neither can happen without the other.

The Half-Done Disaster

Imagine the transfer stops halfway.

The first account is already debited $100. The second account never receives it.

The money simply vanishes. The database now holds a state that should never exist.

Two possible endings for one transfer
  • Atomic
  • Half-done

Atomicity exists to make this partial state impossible, not merely unlikely.

Commit Or Rollback

Every transaction ends in exactly one of two ways.

Commit makes all changes permanent and visible to others.

Rollback undoes every change made inside the transaction, returning the database to how it looked before the transaction began.

How a transaction ends
  • Begin transaction
  • Run operations: Several operations run as one unit of work.
  • All succeeded?
  • Commit: All changes become permanent and visible.
  • Rollback: Every change is undone.

There is no third ending. A transaction cannot finish partly applied.

How Rollback Works

Rollback is not magic. The database keeps a record of what it changed.

Before a row is modified, the old value is written to an undo log.

If the transaction fails, the database walks that log backwards and restores each old value.

Once every change is reversed, the transaction is closed as if it never happened.

Undoing a failed transaction
  • Record old: Before a row is modified, the old value is written to an undo log.
  • Apply change: The operation modifies the row inside the transaction.
  • Failure hits: Something goes wrong and the transaction cannot finish.
  • Walk backwards: The database reads the undo log backwards and restores each old value.
  • Close out: The transaction ends as if it never happened.

What Triggers A Rollback

Rollback is not only for crashes.

Your own code can call it on purpose, for example when stock runs out mid-order.

The database can force it when a constraint is violated or a deadlock is detected.

The system can trigger it after a crash, replaying undo logs during recovery.

Who triggers a rollback

A Common Misconception

Many people assume a rollback restores the whole database to an earlier point in time.

It does not. Rollback only undoes the changes made by that one transaction.

Other transactions that committed in the meantime stay untouched.

Rollback is surgical, not a time machine.

If rollback undoes changes, why is other work safe?

Using This In Practice

Wrap operations that must succeed together inside one transaction.

Keep transactions short. Long ones hold locks and raise the chance of deadlock rollbacks.

Always handle the failure path. If your code catches an error, decide clearly whether to commit or roll back.

Never assume a write succeeded just because no error was printed.

Habits that keep transactions safe
  • Design
  • Group what must succeed together: One unit of work, one transaction.
  • Keep transactions short: Long ones hold locks and invite deadlock rollbacks.
  • Failure handling
  • Decide commit or roll back: Every caught error needs an explicit ending.
  • Verify the write: Silence is not proof of success.

Every transaction ends exactly one way: commit or rollback.

Check yourself

A bank transfer that debits one account and credits another should be treated as one atomic transaction.

  • True
  • False

Answer: True

This is correct. Atomicity requires all operations in a transaction to take effect together, preventing a half-completed transfer.

A transaction can _____ to make all changes permanent, or _____ to undo its changes. Before modifying a row, the database writes the old value to an _____.

Answer: A transaction can commit to make all changes permanent, or rollback to undo its changes. Before modifying a row, the database writes the old value to an undo log.

Commit permanently applies the transaction's changes. Rollback reverses them, using the undo log that stored old values before modification.

An order transaction debits inventory for an item, then discovers that a required constraint is violated. What should the database do to preserve atomicity?

  • Commit the inventory debit and ignore the violation
  • Rollback every change made by the order transaction
  • Rollback all changes made by every transaction
  • Keep the transaction open until another order succeeds

Answer: Rollback every change made by the order transaction

The database should roll back the transaction, undoing the inventory debit and any other changes made within that transaction. This prevents a partially applied order.

Keep going

This is lesson 1 of 7 in ACID 完全指南:四個保證與 Isolation Level 實務. Next up:

  1. Consistency 與 CAP 的 C 有何不同
  2. Durability 與 write-ahead log
  3. 三種常見併發異常:dirty read、non-repeatable read、phantom read
  4. 常見 isolation level 比較(Read Committed、Repeatable Read、Serializable)
  5. 如何根據場景選擇 isolation level

Start this course free

Wondering turns any topic into short lessons you can read, watch, or listen to, with practice questions and an AI tutor.