Glossary

Transactions

TL;DR

Database transactions are sequences of operations executed as a single unit of work, ensuring data consistency and reliability through ACID properties.


Concept

A database transaction is a logical unit of work that consists of one or more database operations executed as a single, indivisible operation. Transactions ensure that the database remains in a consistent state even in the presence of system failures or concurrent access by multiple users.

Key characteristics and concepts of transactions include:

  1. ACID Properties: Transactions adhere to four fundamental properties:

    • Atomicity: All operations within a transaction are treated as a single unit - either all succeed or all fail
    • Consistency: Transactions bring the database from one valid state to another, maintaining all integrity constraints
    • Isolation: Concurrent transactions execute independently without interfering with each other
    • Durability: Once a transaction is committed, its effects are permanently recorded in the database
  2. Transaction States: Transactions progress through different states:

    • Active: Transaction is executing operations
    • Partially Committed: Transaction has completed operations but not yet committed
    • Committed: Transaction has successfully completed and changes are permanent
    • Failed: Transaction cannot proceed due to errors
    • Aborted: Transaction has been rolled back and database is restored to its state before the transaction
  3. Transaction Operations:

    • BEGIN/START TRANSACTION: Marks the beginning of a transaction
    • COMMIT: Permanently saves all changes made during the transaction
    • ROLLBACK: Undoes all changes made during the transaction
    • SAVEPOINT: Creates intermediate points within a transaction for partial rollbacks
  4. Concurrency Control: Mechanisms to manage simultaneous transactions:

    • Locking: Prevents conflicts by temporarily restricting access to data
    • Timestamp Ordering: Orders transactions by timestamp to ensure serializability
    • Optimistic Concurrency Control: Assumes conflicts are rare and validates at commit time

Isolation levels determine how transactions interact with each other:

  • Read Uncommitted: Allows dirty reads (reading uncommitted changes)
  • Read Committed: Prevents dirty reads but allows non-repeatable reads
  • Repeatable Read: Prevents dirty and non-repeatable reads but allows phantom reads
  • Serializable: Highest isolation level that prevents all anomalies

Benefits of transactions include:

  • Data Integrity: Ensures database consistency and constraint enforcement
  • Error Recovery: Automatic rollback in case of failures
  • Concurrency Safety: Safe concurrent access by multiple users
  • Reliability: Predictable behavior even in failure scenarios

Transactions are commonly used for:

  • Financial operations (transfers, payments, billing)
  • Order processing systems
  • Inventory management
  • User registration and profile updates
  • Multi-step business processes
  • Data migration and batch operations

Organizations implement transactions to ensure data reliability, maintain business rule consistency, and provide fault tolerance in their database applications. They are fundamental to building robust, reliable database systems that can handle concurrent access and recover from failures gracefully.

Related words: Caching Indexing Redis