{
    "slug": "shared_kernel",
    "term": "Shared Kernel",
    "category": "architecture",
    "difficulty": "advanced",
    "short": "A DDD strategic pattern where two bounded contexts intentionally share a small subset of the domain model — requiring tight coordination between teams but reducing duplication.",
    "long": "A Shared Kernel is a deliberate coupling point: two contexts agree to share specific classes (value objects, domain events, DTOs) and coordinate on any changes. It is NOT accidentally shared code — it is a formal agreement. Best for: tightly related contexts owned by the same team, where the coordination cost is lower than maintaining two separate but nearly identical models. Anti-pattern when: teams are independent, the shared portion grows unchecked, or the shared code becomes a dumping ground for anything two contexts need.",
    "aliases": [
        "shared kernel DDD",
        "shared domain model"
    ],
    "tags": [
        "architecture",
        "ddd",
        "strategic-design"
    ],
    "misconception": "Shared Kernel means sharing a database or common library — a Shared Kernel is a specific, bounded, intentionally chosen subset of the domain model with formal team agreement, not a general utility library.",
    "why_it_matters": "Without a Shared Kernel, two contexts independently model the same Money value object — a Shared Kernel eliminates this duplication for concepts that genuinely belong to both contexts.",
    "common_mistakes": [
        "Shared Kernel that grows over time without governance — becomes a monolithic shared library.",
        "No team agreement on change process — one team changes the kernel, breaks the other.",
        "Sharing infrastructure code as a Shared Kernel — infrastructure utilities are not domain concepts.",
        "Using Shared Kernel to avoid duplicating convenience code — duplication is often better than coupling."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "bounded_context",
        "context_mapping",
        "conformist_pattern",
        "domain_driven_design"
    ],
    "prerequisites": [
        "bounded_context",
        "context_mapping",
        "domain_driven_design"
    ],
    "refs": [
        "https://www.domainlanguage.com/ddd/reference/"
    ],
    "bad_code": "// Shared kernel used as a dumping ground:\nnamespace SharedKernel;\n// Originally: just Money and CustomerId\n// 2 years later: 200 classes, utilities, helpers, configs\n// Every team change breaks others\n// No clear ownership — everyone and nobody owns it",
    "good_code": "// Disciplined shared kernel — minimal, governed:\nnamespace SharedKernel;\n\n// Only truly shared domain concepts:\nfinal class Money { /* amount + currency */ }\nfinal class CustomerId { /* UUID value object */ }\nfinal class OrderId { /* UUID value object */ }\n\n// Change process: both teams must review and approve\n// Version controlled independently — SharedKernel v2.1\n// Both contexts pin to the same version\n// Size limit: if > 20 classes, something is wrong",
    "quick_fix": "Keep the Shared Kernel as small as possible — only put in it what both contexts genuinely share and can agree to maintain together; a growing shared kernel becomes a source of coupling",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/shared_kernel",
        "html_url": "https://codeclaritylab.com/glossary/shared_kernel",
        "json_url": "https://codeclaritylab.com/glossary/shared_kernel.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": "[Shared Kernel](https://codeclaritylab.com/glossary/shared_kernel) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/shared_kernel"
            }
        }
    }
}