CatWatchDog ˁ˚ᴥ˚ˀ - Custom Software Solutions, Expert Project Management.

ACID

Author: Dmitry Ro
Updated: 2023-12-13

Understanding the Pillars of ACID: Ensuring Database Reliability and Consistency

In the realm of databases, the ACID acronym stands tall as a cornerstone principle, ensuring reliability, consistency, and transactional integrity. Originally formulated to address the complexities of database transactions, ACID serves as a guideline for database systems to maintain data accuracy and reliability, even in the face of system failures or concurrent transactions. The Pillars of ACID

Atomicity

At the heart of ACID lies atomicity, emphasizing the "all or nothing" principle. It ensures that a transaction is treated as a single unit, either executing in its entirety or not at all. This property guarantees that if any part of a transaction fails, the entire transaction is rolled back, leaving the database in its original state. Atomicity prevents incomplete transactions from affecting the integrity of the data.

Consistency

Consistency guarantees that the database remains in a valid state before and after a transaction. It enforces predefined rules and constraints, ensuring that data modifications maintain the database’s integrity. For instance, if a transaction violates any integrity constraints or rules, the system prevents it from altering the database, maintaining a consistent state. Isolation

Isolation addresses the concurrent execution of transactions. It ensures that even when multiple transactions occur simultaneously, the database appears as if they are executing sequentially. This prevents interference between transactions and maintains data integrity. Isolation levels, such as Read Uncommitted, Read Committed, Repeatable Read, and Serializable, define the degree to which transactions are isolated from one another, balancing consistency and performance.

Durability

Durability guarantees that once a transaction is committed, its changes are permanent and survive system failures. It ensures that completed transactions persist in the system, even in the event of power outages, crashes, or other failures. This is typically achieved through mechanisms like write-ahead logging, ensuring that committed transactions are stored permanently, preventing data loss. Implementing ACID in Database Systems

Relational databases, like MySQL, PostgreSQL, and Oracle, often adhere closely to the ACID principles. These systems use various techniques such as logging, locking mechanisms, and multi-version concurrency control to ensure transactions maintain the ACID properties.

Example

        
        -- Create the accounts table
        CREATE TABLE accounts (
            account_id SERIAL PRIMARY KEY,
            balance DECIMAL(10, 2) NOT NULL CHECK (balance >= 0),
            last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        );

        -- Insert initial account data
        INSERT INTO accounts (balance) VALUES (1000.00), (500.00);

        -- Start a transaction to transfer funds between accounts
        BEGIN;

        -- Update the balances in a single transaction
        UPDATE accounts
        SET balance = balance - 200.00
        WHERE account_id = 1; -- Deduct $200 from account 1

        UPDATE accounts
        SET balance = balance + 200.00
        WHERE account_id = 2; -- Add $200 to account 2

        -- Ensure consistency with CHECK constraint
        -- This will prevent negative balances
        -- If this constraint fails, the transaction will roll back
        -- Adding this constraint is crucial for maintaining consistency
        ALTER TABLE accounts
        ADD CONSTRAINT check_positive_balance CHECK (balance >= 0);

        -- Commit the transaction
        COMMIT;
      

Explanation of PostgreSQL Example:

  • Atomicity: The entire set of UPDATE queries is wrapped within a transaction. If any UPDATE fails, the entire transaction will be rolled back, ensuring atomicity.

  • Consistency: The CHECK constraint check_positive_balance ensures that the balance in each account cannot go below zero, maintaining consistency.

  • Isolation: Assume multiple clients concurrently execute similar transactions to transfer funds between different accounts. PostgreSQL employs isolation mechanisms, such as locking, to ensure these transactions do not interfere with each other, preserving data integrity.

  • Durability: Once the transaction is committed, the changes made to the balances will persist even in the face of system failures, providing durability.

This example showcases how PostgreSQL ensures the ACID properties in a transactional scenario involving fund transfers between accounts.