{
    "slug": "serverless_functions",
    "term": "Serverless Functions",
    "category": "cloud",
    "difficulty": "intermediate",
    "short": "Event-triggered, stateless functions managed by a cloud provider — you deploy code, the provider handles servers, scaling, and availability.",
    "long": "Serverless (Lambda, Cloud Functions, Azure Functions) runs code in response to events (HTTP request, queue message, cron schedule) without provisioning or managing servers. Billing is per invocation and execution time — idle costs nothing. Cold starts add latency when a function hasn't run recently. Functions must be stateless — any persistent state lives in a database or cache. PHP is supported via custom runtimes (Bref) on AWS Lambda.",
    "aliases": [
        "Lambda",
        "Cloud Functions",
        "FaaS",
        "function as a service",
        "Bref"
    ],
    "tags": [
        "cloud",
        "serverless",
        "aws",
        "php"
    ],
    "misconception": "Serverless means no servers — the servers exist, you just don't manage them; 'serverless' means no server management, not no servers.",
    "why_it_matters": "Serverless eliminates server management, scales to zero, and costs nothing at idle — ideal for event-driven workloads, but cold starts and statelessness require architectural adjustments.",
    "common_mistakes": [
        "Storing state in the function's memory or filesystem — functions are ephemeral; use Redis or a database.",
        "Ignoring cold start latency — functions not invoked recently take 200-2000ms to initialise.",
        "Monolithic serverless functions — small, single-purpose functions scale and deploy independently.",
        "Not setting memory/timeout limits — unlimited execution time runs up costs on stuck functions."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "cloud_computing_models",
        "cloud_auto_scaling",
        "php_deployment_pipeline",
        "processes_vs_threads"
    ],
    "prerequisites": [
        "cloud_native_patterns",
        "aws_fundamentals",
        "containerisation"
    ],
    "refs": [
        "https://bref.sh/",
        "https://aws.amazon.com/lambda/"
    ],
    "bad_code": "// Stateful serverless — state lost between invocations:\n$counter = 0; // Top-level 'global' variable\nfunction handler($event) {\n    global $counter;\n    $counter++; // Resets to 0 on every cold start — not persistent\n    return ['count' => $counter];\n}",
    "good_code": "// Stateless — state in Redis:\nfunction handler($event) {\n    $redis = new Redis();\n    $redis->connect(getenv('REDIS_HOST'));\n    $count = $redis->incr('invocation_count');\n    return ['count' => $count]; // Persists across invocations\n}\n// Bref (PHP on Lambda): composer require bref/bref",
    "quick_fix": "Use Bref (bref.sh) to deploy PHP on AWS Lambda — it handles the Lambda runtime; keep functions small, stateless, and under 50MB; use SQS triggers for queue processing",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/serverless_functions",
        "html_url": "https://codeclaritylab.com/glossary/serverless_functions",
        "json_url": "https://codeclaritylab.com/glossary/serverless_functions.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": "[Serverless Functions](https://codeclaritylab.com/glossary/serverless_functions) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/serverless_functions"
            }
        }
    }
}