{
    "slug": "acid_properties",
    "term": "ACID Properties",
    "category": "general",
    "difficulty": "intermediate",
    "short": "The four guarantees of database transactions: Atomicity, Consistency, Isolation, and Durability.",
    "long": "ACID ensures reliable transaction processing: Atomicity — a transaction either fully succeeds or fully rolls back (no partial writes); Consistency — the database moves from one valid state to another, enforcing constraints; Isolation — concurrent transactions don't see each other's intermediate state (controlled by isolation levels: Read Uncommitted, Read Committed, Repeatable Read, Serializable); Durability — committed transactions survive crashes (via write-ahead logging). In PHP, manage transactions explicitly with PDO::beginTransaction(), commit(), and rollBack(), especially when multiple related writes must succeed together.",
    "aliases": [
        "ACID",
        "atomicity consistency isolation durability",
        "database transactions ACID"
    ],
    "tags": [
        "general",
        "database",
        "transactions",
        "fundamentals"
    ],
    "misconception": "All databases claiming ACID compliance provide the same guarantees. ACID isolation levels vary — READ COMMITTED, REPEATABLE READ, and SERIALIZABLE offer progressively stronger guarantees with progressively higher overhead. \"ACID compliant\" without specifying the default isolation level is meaningless.",
    "why_it_matters": "ACID guarantees that database transactions are reliable — without them, concurrent writes can corrupt data, partial failures can leave data in inconsistent states, and reads can see uncommitted changes.",
    "common_mistakes": [
        "Assuming NoSQL databases are ACID — most sacrifice isolation or durability for performance.",
        "Not wrapping multi-step operations in a transaction — a crash between steps leaves data inconsistent.",
        "Using READ UNCOMMITTED isolation — allows dirty reads of data that may be rolled back.",
        "Relying on application-level locking instead of database transactions for concurrency."
    ],
    "when_to_use": [
        "Financial transactions, inventory management, or any domain where partial writes are catastrophic.",
        "Any operation touching multiple rows or tables that must succeed or fail atomically.",
        "Concurrent workloads where isolation prevents dirty reads and phantom reads.",
        "Systems where durability guarantees are required — data must survive a crash without loss."
    ],
    "avoid_when": [
        "You need horizontal scalability across distributed nodes — full ACID across shards requires coordination overhead.",
        "The workload tolerates eventual consistency — sacrificing isolation for availability (BASE model) may be correct.",
        "Using NoSQL stores that offer only document-level atomicity — do not assume full ACID without verifying."
    ],
    "related": [
        "pdo",
        "race_condition",
        "eventual_consistency"
    ],
    "prerequisites": [
        "database_indexing",
        "db_transactions",
        "db_isolation_levels"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/ACID",
        "https://www.php.net/manual/en/pdo.transactions.php"
    ],
    "bad_code": "// Two separate queries without a transaction — partial failure leaves inconsistent state\n$pdo->exec(\"UPDATE accounts SET balance = balance - 100 WHERE id = 1\");\n$pdo->exec(\"UPDATE accounts SET balance = balance + 100 WHERE id = 2\");",
    "good_code": "try {\n  $pdo->beginTransaction();\n  $pdo->exec(\"UPDATE accounts SET balance = balance - 100 WHERE id = 1\");\n  $pdo->exec(\"UPDATE accounts SET balance = balance + 100 WHERE id = 2\");\n  $pdo->commit();\n} catch (Exception $e) {\n  $pdo->rollBack(); throw $e;\n}",
    "quick_fix": "Wrap related database operations in a transaction — if any step fails, rollback ensures the database stays consistent; never leave partial writes committed",
    "severity": "high",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-25",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/acid_properties",
        "html_url": "https://codeclaritylab.com/glossary/acid_properties",
        "json_url": "https://codeclaritylab.com/glossary/acid_properties.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[ACID Properties](https://codeclaritylab.com/glossary/acid_properties) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/acid_properties"
            }
        }
    }
}