{
    "slug": "mysql_query_cache",
    "term": "MySQL Query Cache (Deprecated)",
    "category": "database",
    "difficulty": "intermediate",
    "short": "A server-side cache for SELECT results removed in MySQL 8.0 — it caused severe scalability issues under concurrent writes.",
    "long": "MySQL's built-in query cache stored SELECT results and invalidated the entire table's cached results on any write. Under write-heavy workloads it became a contention point — every INSERT/UPDATE/DELETE invalidated potentially thousands of cached queries. It was deprecated in MySQL 5.7.20 and removed in 8.0. Modern alternatives are application-level caches (Redis, Memcached) which give control over cache keys, TTLs, and invalidation logic.",
    "aliases": [
        "MySQL query cache removed",
        "query_cache_size MySQL 8",
        "MySQL caching"
    ],
    "tags": [
        "mysql",
        "database",
        "performance",
        "caching"
    ],
    "misconception": "Enabling MySQL query cache improves performance. On write-heavy workloads, the global mutex on query cache invalidation actually degrades performance significantly.",
    "why_it_matters": "Developers who learned MySQL pre-8 sometimes wonder why query_cache_size is no longer available. Application-level caching with Redis provides better control without the write-contention problem.",
    "common_mistakes": [
        "Expecting query_cache_size to exist in MySQL 8 — it was fully removed.",
        "Relying on implicit database-level caching instead of explicit application caching.",
        "Using SELECT SQL_CACHE in queries targeting MySQL 8 — silently ignored."
    ],
    "when_to_use": [
        "Implement application-level caching with Redis or Memcached — gives full control over cache keys and TTLs."
    ],
    "avoid_when": [
        "Do not set query_cache_size in MySQL 8 config — it causes startup errors as the option was fully removed.",
        "Do not rely on database-level caching — application-level caching is more predictable and scalable."
    ],
    "related": [
        "asset_caching",
        "slow_query_log",
        "mysql_indexes_explained"
    ],
    "prerequisites": [
        "mysql_indexes_explained"
    ],
    "refs": [
        "https://dev.mysql.com/doc/refman/5.7/en/query-cache.html"
    ],
    "bad_code": "-- MySQL 8: query_cache_size no longer exists\n-- query_cache_size = 64M -- This config option causes startup error in MySQL 8",
    "good_code": "// Application-level caching with Redis (preferred)\n$cacheKey = 'user:' . $userId;\n$user = $redis->get($cacheKey);\nif ($user === null) {\n    $stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');\n    $stmt->execute([$userId]);\n    $user = $stmt->fetch();\n    $redis->setex($cacheKey, 300, json_encode($user)); // 5 min TTL\n}\nreturn json_decode($user, true);",
    "quick_fix": "Implement application-level caching with Redis or Memcached instead of relying on MySQL's removed query cache",
    "effort": "medium",
    "created": "2026-03-31",
    "updated": "2026-03-31",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/mysql_query_cache",
        "html_url": "https://codeclaritylab.com/glossary/mysql_query_cache",
        "json_url": "https://codeclaritylab.com/glossary/mysql_query_cache.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": "[MySQL Query Cache (Deprecated)](https://codeclaritylab.com/glossary/mysql_query_cache) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/mysql_query_cache"
            }
        }
    }
}