# Atomicity in ACID Transactions

> Atomicity is the A in ACID: the guarantee that a database transaction is treated as a single, indivisible unit. Either every operation inside it succeeds and is saved, or none of them take effect, so no partial update is ever left behind. Most relational databases enforce it with a transaction log, also called a write-ahead log.

Canonical: https://devsunite.com/resources/core-subjects/dbms/atomicity
Last updated: 2026-11-22

Subject: DBMS · Category: ACID Properties · 5 min read

Atomicity is the "A" in ACID, and it's the guarantee that a database transaction is treated as a single, indivisible unit: either every operation inside it succeeds and gets saved, or none of them do. There's no in-between state where some of a transaction's writes made it to disk and others didn't.

## Why atomicity matters

A transaction is usually made up of multiple steps. A classic example is transferring money between two bank accounts, which really means two separate writes: subtract the amount from one account, add it to the other. If the database crashes, loses power, or hits an error after the first write but before the second, you'd end up with money that simply vanished, deducted from one account and never credited to the other, unless something guarantees that both writes happen together or neither happens at all.

That "together or neither" guarantee is atomicity. Without it, every multi-step operation in a database would be one crash away from leaving the data in an inconsistent, half-finished state.

## How databases actually enforce atomicity

Most relational databases enforce atomicity using a transaction log (sometimes called a write-ahead log, or WAL). Before any change is applied to the actual database files, the database first writes a record of the intended change to this log. That log record includes enough information to redo the change if it didn't fully apply, or undo it if it needs to be rolled back.

If a transaction completes all its steps successfully, the database issues a **COMMIT**, which makes every change in that transaction permanent. If anything goes wrong partway through, whether it's an application-level error, a constraint violation, or the connection dropping, the database issues a **ROLLBACK**, which uses the log to undo every change the transaction had already made, returning the data to exactly the state it was in before the transaction started.

Crucially, this also covers crashes. If the database process itself crashes mid-transaction, when it restarts it reads the transaction log and figures out which transactions had committed and which hadn't, then rolls back anything that was left incomplete. This is called crash recovery, and it's the reason atomicity holds even in the worst case, not just when everything goes smoothly.

## A worked example

Take the bank transfer scenario concretely. Account A has $500, Account B has $200, and a transaction needs to move $150 from A to B.

The transaction consists of two writes:
1. `UPDATE accounts SET balance = balance - 150 WHERE account_id = 'A'`
2. `UPDATE accounts SET balance = balance + 150 WHERE account_id = 'B'`

**Successful case:** Both writes execute, the transaction commits, and the final state is A = $350, B = $350. Total money in the system stays $700 throughout, exactly as it was before the transfer.

**Failure case, no atomicity:** Suppose write 1 executes and the system crashes before write 2 runs. Without atomicity, A is now $350 and B is still $200. Total money in the system is $550, $150 has simply disappeared from the books.

**Failure case, with atomicity:** Same crash, same timing, but now the database's crash recovery process reads the transaction log on restart, sees that this transaction never reached COMMIT, and rolls back write 1. A goes back to $500, B stays at $200, total is back to $700. From the outside, it's as if the transfer never happened at all, which is exactly the point.

## Where this matters in real systems

Every mainstream relational database, PostgreSQL, MySQL, SQL Server, Oracle, implements atomicity through some form of write-ahead logging, and it's not optional or a performance toggle; it's foundational to what makes them trustworthy for financial data, inventory systems, or anything else where a half-applied write would cause real damage.

This also explains why batch operations in application code are usually wrapped explicitly in a transaction block (`BEGIN` ... `COMMIT`) rather than run as separate independent statements. An e-commerce checkout, for instance, typically needs to deduct inventory, create an order record, and charge a payment method as one atomic unit. If any one of those three fails, the whole checkout needs to roll back together, otherwise you could end up with a charged customer and no order, or an order with no inventory deducted.

Atomicity is also why "eventually consistent" NoSQL databases are a genuinely different tradeoff, not just a different flavor of the same guarantee. Many of them intentionally give up strict multi-write atomicity across documents in exchange for higher availability and throughput, which is a real design decision with real consequences, not a missing feature.

## What interviewers actually ask about atomicity

- **"What does atomicity actually guarantee, in one sentence?"** All-or-nothing execution: every operation in a transaction succeeds, or none of them take effect.
- **"How does a database implement atomicity under the hood?"** The expected answer involves a transaction log or write-ahead log, plus COMMIT/ROLLBACK semantics.
- **"What happens if the database crashes in the middle of a transaction?"** This is testing whether the candidate knows atomicity survives crashes too, via log-based recovery on restart, not just application-level errors.
- **"Give a real example of why atomicity matters."** The bank transfer example is the classic one for a reason, it's concrete and the failure mode (money disappearing) is immediately understandable.
- **"How is atomicity different from consistency?"** Atomicity is about the transaction completing fully or not at all; consistency is about the data satisfying all defined rules and constraints before and after the transaction. They're related but not the same guarantee.

## Key takeaways

- Atomicity guarantees a transaction's operations either all succeed together or none of them take effect.
- Databases enforce this using a transaction log, with COMMIT making changes permanent and ROLLBACK undoing them.
- Atomicity also protects against crashes mid-transaction, through log-based recovery on restart.
- The classic example is a bank transfer: two writes that must succeed or fail as a single unit.
- Systems that relax atomicity (some NoSQL databases) are making a deliberate availability tradeoff, not skipping a step by accident.

## Related

- [Previous: What Is a Transaction in a Database?](https://devsunite.com/resources/core-subjects/dbms/what-is-a-transaction)
- [Next: Consistency in ACID Transactions](https://devsunite.com/resources/core-subjects/dbms/consistency)
- [More in ACID Properties: Isolation in ACID Transactions](https://devsunite.com/resources/core-subjects/dbms/isolation)
- [More in ACID Properties: Durability in ACID Transactions](https://devsunite.com/resources/core-subjects/dbms/durability)
- [More in ACID Properties: ACID Properties: Complete Overview](https://devsunite.com/resources/core-subjects/dbms/complete-overview)
- [All 174 DBMS interview questions](https://devsunite.com/resources/core-subjects/dbms)
