{
    "slug": "db_isolation_levels",
    "term": "Transaction Isolation Levels",
    "category": "database",
    "difficulty": "advanced",
    "short": "SQL standards defining how and when changes made by one transaction are visible to others, trading consistency for concurrency.",
    "long": "The four standard isolation levels from weakest to strongest: READ UNCOMMITTED (dirty reads allowed), READ COMMITTED (no dirty reads, default in PostgreSQL), REPEATABLE READ (no non-repeatable reads, default in MySQL InnoDB), SERIALIZABLE (full isolation, no phantom reads). Higher isolation prevents more anomalies but increases lock contention. Most applications work correctly with READ COMMITTED; SERIALIZABLE is reserved for financial operations requiring strict consistency.",
    "aliases": [
        "isolation levels",
        "database isolation",
        "ACID isolation"
    ],
    "tags": [
        "database",
        "transactions",
        "acid",
        "concurrency"
    ],
    "misconception": "READ COMMITTED is always safe — it still allows non-repeatable reads (same row read twice in a transaction returns different values if another transaction commits between reads).",
    "why_it_matters": "Wrong isolation level causes phantom reads, non-repeatable reads, or dirty reads — subtle data integrity bugs that only manifest under concurrent load and are nearly impossible to debug after the fact.",
    "common_mistakes": [
        "Assuming MySQL and PostgreSQL use the same default — MySQL uses REPEATABLE READ, PostgreSQL uses READ COMMITTED.",
        "Using READ UNCOMMITTED in production — dirty reads return data that may be rolled back.",
        "SERIALIZABLE on every transaction — massive lock contention; use only where strictly required.",
        "Not understanding that isolation level is set per-session or per-transaction, not globally."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "db_transactions",
        "acid_properties",
        "race_condition",
        "optimistic_locking"
    ],
    "prerequisites": [
        "acid_properties",
        "db_transactions",
        "race_condition"
    ],
    "refs": [
        "https://www.postgresql.org/docs/current/transaction-iso.html",
        "https://dev.mysql.com/doc/refman/8.0/en/innodb-transaction-isolation-levels.html"
    ],
    "bad_code": "// READ UNCOMMITTED — sees uncommitted data from other transactions:\n$pdo->exec('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED');\n$pdo->beginTransaction();\n$balance = $pdo->query('SELECT balance FROM accounts WHERE id = 1')->fetchColumn();\n// $balance may reflect a transaction that will be rolled back — dirty read",
    "good_code": "// SERIALIZABLE for financial operations:\n$pdo->exec('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');\n$pdo->beginTransaction();\ntry {\n    $balance = $pdo->query('SELECT balance FROM accounts WHERE id = 1 FOR UPDATE')->fetchColumn();\n    if ($balance >= $amount) {\n        $pdo->exec('UPDATE accounts SET balance = balance - ' . $amount . ' WHERE id = 1');\n    }\n    $pdo->commit();\n} catch (Exception $e) { $pdo->rollBack(); throw $e; }",
    "quick_fix": "Use READ COMMITTED for most PHP web applications — it prevents dirty reads without the performance overhead of REPEATABLE READ's gap locks",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/db_isolation_levels",
        "html_url": "https://codeclaritylab.com/glossary/db_isolation_levels",
        "json_url": "https://codeclaritylab.com/glossary/db_isolation_levels.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": "[Transaction Isolation Levels](https://codeclaritylab.com/glossary/db_isolation_levels) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/db_isolation_levels"
            }
        }
    }
}