Course contentsShow
← Software Engineering
Lesson 897 of 1,93328. Transactions and ConsistencyFree lesson

The Atomicity Property

How atomicity ensures all operations in a transaction complete successfully or none do, preventing partial updates.

The Atomicity Property

What you'll learn: How atomicity ensures all operations in a transaction complete successfully or none do, preventing partial updates.


The Core Idea

Atomicity is the "all-or-nothing" guarantee in database transactions. When you execute multiple operations as a single transaction, either every operation succeeds and the changes are saved permanently, or if anything fails, the entire transaction is rolled back as if nothing happened.

Without atomicity, you could end up with partial updates — a nightmare scenario where only some operations complete, leaving your data in an inconsistent state.

The Bank Transfer Analogy

Imagine transferring $100 from your checking account to savings:

  1. Subtract $100 from checking
  2. Add $100 to savings

If the system crashes after step 1 but before step 2, you'd lose $100! Atomicity prevents this. The transaction either completes both steps or neither, ensuring your money doesn't vanish into the void.

How It Works in Practice

When you start a transaction, the database tracks all changes in a temporary space. Only when you explicitly commit does it apply all changes permanently. If something goes wrong (crash, error, or explicit rollback), all tracked changes are discarded.

BEGIN TRANSACTION;
  UPDATE accounts SET balance = balance - 100 WHERE id = 1;
  UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

If either UPDATE fails, the entire transaction rolls back automatically — neither account changes.

Why It Matters

Atomicity protects data integrity during multi-step operations. It ensures your database never reflects half-completed work, which could violate business rules or corrupt relationships between records.

Key Takeaway: Atomicity guarantees that a transaction is an indivisible unit — all operations succeed together or fail together, preventing partial updates and maintaining data consistency.