{
    "slug": "polyglot_persistence",
    "term": "Polyglot Persistence",
    "category": "general",
    "difficulty": "advanced",
    "short": "Using multiple different database technologies in a single application — each chosen for what it does best rather than forcing all data into one general-purpose store.",
    "long": "Different data types have different optimal storage characteristics: relational data (users, orders) fits PostgreSQL; session/cache fits Redis; search fits Elasticsearch; document data fits MongoDB; time-series metrics fit InfluxDB or TimescaleDB; graph relationships fit Neo4j. Polyglot persistence picks the best tool for each use case. The challenge: operational overhead of maintaining multiple systems, consistency across systems, and transaction management across boundaries. Best applied incrementally when a clear mismatch with the current store exists.",
    "aliases": [
        "multi-database",
        "polyglot storage",
        "best-fit persistence"
    ],
    "tags": [
        "general",
        "architecture",
        "database"
    ],
    "misconception": "More databases always means better architecture — polyglot persistence adds operational complexity; each additional store requires expertise, monitoring, backup, and failure handling. Start with one good store and add others only when justified.",
    "why_it_matters": "A full-text search implemented in MySQL with LIKE queries that takes 2 seconds would take 50ms in Elasticsearch — the right store for the use case eliminates entire classes of performance problems.",
    "common_mistakes": [
        "Adding a new database for every new feature — operational complexity compounds quickly.",
        "Cross-database transactions — they are impossible without careful application-level sagas.",
        "Not considering team expertise — a MongoDB cluster maintained by a team unfamiliar with it is dangerous.",
        "Premature polyglot — solve the problem with your existing store first, add a new one only when you hit a real wall."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "database_sharding",
        "elasticsearch_basics",
        "meilisearch_typesense",
        "cqrs"
    ],
    "prerequisites": [
        "database_indexing",
        "redis_patterns",
        "elasticsearch_basics"
    ],
    "refs": [
        "https://martinfowler.com/bliki/PolyglotPersistence.html"
    ],
    "bad_code": "// MySQL full-text search — hitting limits:\n$results = $pdo->query(\n    \"SELECT * FROM glossary WHERE body LIKE '%\" . $term . \"%'\n     OR title LIKE '%\" . $term . \"%'\"\n);\n// 2 second query on 10,000 terms, no ranking, no typo tolerance",
    "good_code": "// Polyglot: MySQL for data, Meilisearch for search:\n// MySQL: source of truth for term content\n$term = Term::find($slug);\n\n// Meilisearch: optimised for full-text search\n$searchResults = $meilisearch->index('glossary')\n    ->search($query, ['limit' => 20]);\n// 5ms response, typo-tolerant, ranked by relevance\n\n// Keep in sync via queue:\nTermSaved::dispatch($term); // Listener re-indexes in Meilisearch",
    "quick_fix": "Use the right database for each use case: MySQL/PostgreSQL for relational OLTP, Redis for caching/sessions/queues, Elasticsearch/Meilisearch for full-text search, S3 for files — PHP can connect to all of them",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/polyglot_persistence",
        "html_url": "https://codeclaritylab.com/glossary/polyglot_persistence",
        "json_url": "https://codeclaritylab.com/glossary/polyglot_persistence.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": "[Polyglot Persistence](https://codeclaritylab.com/glossary/polyglot_persistence) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/polyglot_persistence"
            }
        }
    }
}