Database Fundamentals

0% completed

Previous
Next
Atomicity

Atomicity is one of the core ACID properties that ensure the integrity of a database during transactions. It guarantees that each transaction is executed as a single, indivisible unit of work. This means a transaction will either complete all of its operations successfully or fail entirely, leaving the database unchanged.

What Does Atomicity Mean?

Image
  • All-or-Nothing Principle: Atomicity ensures that either all the operations in a transaction are applied or none of them are. There is no middle ground.
  • Commit and Rollback:
    • Commit: Successfully executing all operations in a transaction.
    • Rollback: Reverting all changes made during a transaction when a failure occurs.

Without atomicity, partial updates can leave the database in an inconsistent state, resulting in corrupted or incorrect data.

Example Scenario

Image

In the above example, there are two accounts, A and B:

  1. Initial State:

    • Account A: $30
    • Account B: $100
  2. Transaction Process:

    • Debit $10 from Account A (successful).
    • Credit $10 to Account B (failure due to an error).
  3. Final State:

    • Account A: $20 (incorrect partial execution).
    • Account B: $100 (unchanged, as credit failed).

Problem: Partial execution violates atomicity. The $10 is debited from A, but not credited to B. This leads to data inconsistency.

How Atomicity is Achieved in Databases

Databases implement atomicity through mechanisms like transaction logs, shadow copies, and recovery protocols. One of the simpler implementations of atomicity is the Shadow Copy technique.

Shadow Copy: An Atomicity Implementation

Shadow Copy is a technique where the database ensures atomicity by creating a separate copy of the database file for every transaction. Here’s how it works:

  1. Creating a Copy: When a transaction begins, the database creates a new copy of the database file (called the shadow copy).
  2. Performing Updates: Changes are made to the shadow copy, not the original file.
  3. Pointer Switch:
    • If the transaction is successful (commit), the database pointer is switched to the shadow copy, making it the new database.
    • If the transaction fails (rollback), the pointer remains on the original file, and the shadow copy is discarded.

Key Scenarios in Shadow Copy

  1. Successful Transaction:

    • All operations in the transaction complete.
    • The database pointer switches to the shadow copy, and the original copy is discarded.
  2. Failure in Pointer Update:

    • All operations succeed, but the pointer fails to update.
    • The transaction is aborted, and the original copy is retained.
  3. Failure During Execution:

    • If the transaction fails mid-way, the pointer remains on the original copy, discarding the incomplete shadow copy.

Limitations of Shadow Copy

  • Inefficiency: Creating a full copy of the database for each transaction consumes significant time and storage.
  • Single Transaction: Shadow copy assumes that only one transaction can run at a time, limiting concurrent processing.
  • Scalability: It does not scale well for high-throughput or multi-user environments.

Modern Alternatives for Atomicity

While shadow copy was an early implementation, modern databases achieve atomicity more efficiently using:

  1. Write-Ahead Logging (WAL): Logs changes before applying them to ensure rollback is possible.
  2. Two-Phase Commit (2PC): Ensures atomicity across distributed systems.
  3. Optimistic Concurrency Control: Tracks changes and applies them atomically only if no conflicts occur.

Why is Atomicity Important?

Atomicity ensures:

  1. Data Integrity: Prevents partial or corrupt updates during failures.
  2. Consistency: Maintains the database's expected state after transactions.
  3. Safe Retries: Developers can confidently retry a failed transaction without worrying about lingering changes.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next