{
    "slug": "backends_for_frontends",
    "term": "Backends for Frontends (BFF)",
    "category": "architecture",
    "difficulty": "advanced",
    "short": "A dedicated API layer per frontend client (mobile app, web app, third-party) — each BFF aggregates and transforms microservice data for its specific client's needs.",
    "long": "Rather than one general-purpose API serving all clients, the BFF pattern creates a thin backend layer per frontend type. The mobile BFF returns compact responses optimised for bandwidth; the web BFF aggregates multiple services into one round trip; the partner BFF exposes only allowed data. Benefits: frontend teams own their BFF, optimise responses without affecting other clients, evolve client-specific contracts independently, and handle client-specific auth concerns. Common with microservices architectures where different clients need very different data shapes.",
    "aliases": [
        "BFF",
        "backend for frontend",
        "client-specific API",
        "API aggregation layer"
    ],
    "tags": [
        "architecture",
        "microservices",
        "api-design"
    ],
    "misconception": "BFF creates code duplication — shared business logic stays in backend services; BFF only handles aggregation, transformation, and client-specific concerns — the duplication is intentional.",
    "why_it_matters": "Without BFF, mobile clients over-fetch large responses, or the general API grows complex conditional logic to serve every client type — BFF keeps both the API and clients clean.",
    "common_mistakes": [
        "Putting business logic in the BFF — BFF should only orchestrate and transform, not implement rules.",
        "One BFF for all clients — defeats the purpose; each client type should have its own.",
        "BFF team owned by the backend — frontend teams should own their BFF for true independence.",
        "Not versioning the BFF — BFF contracts must be versioned like any other API."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "api_gateway",
        "microservices",
        "graphql_vs_rest",
        "api_versioning"
    ],
    "prerequisites": [
        "api_design",
        "microservices",
        "graphql"
    ],
    "refs": [
        "https://samnewman.io/patterns/architectural/bff/"
    ],
    "bad_code": "// One API trying to serve everyone:\nGET /api/product/42\n// Mobile needs: name, price, thumbnail (3 fields)\n// Web needs: full details, reviews, related (20 fields)\n// Partner needs: name, sku, stock only (3 different fields)\n// Response: all 25 fields to everyone\n// Mobile: 10kb response, uses 500 bytes — 95% wasted",
    "good_code": "// Separate BFFs per client:\n// mobile-bff/product/42:\nGET /mobile/products/42 → { name, price, thumbnail }  // 200 bytes\n\n// web-bff/product/42 — aggregates from 3 services:\nGET /web/products/42 → { name, price, images, reviews, related }  // Full\n\n// partner-bff/product/42:\nGET /partner/products/42 → { name, sku, stockLevel }  // Partner contract",
    "quick_fix": "Create a thin PHP BFF that aggregates data from multiple internal services for a specific client (mobile, web, partner) — tailor the API shape to what each client actually needs",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/backends_for_frontends",
        "html_url": "https://codeclaritylab.com/glossary/backends_for_frontends",
        "json_url": "https://codeclaritylab.com/glossary/backends_for_frontends.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": "[Backends for Frontends (BFF)](https://codeclaritylab.com/glossary/backends_for_frontends) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/backends_for_frontends"
            }
        }
    }
}