{
    "slug": "outbox_pattern",
    "term": "Transactional Outbox Pattern",
    "category": "messaging",
    "difficulty": "advanced",
    "short": "The Outbox pattern atomically saves domain events to an 'outbox' DB table in the same transaction as business data — a relay then publishes to the message broker, preventing lost events.",
    "long": "Problem: update DB and publish message — if broker publish fails after DB commit, event is lost. Solution: write to an 'outbox' table in the same DB transaction as the domain data. A separate relay process reads the outbox and publishes to the broker, deleting rows after confirmed publish. This achieves atomic write-to-DB and guaranteed eventual publish. CDC (Change Data Capture): use Debezium to read DB transaction log and publish to Kafka — no outbox table needed. Polling relay: simpler, slight delay. Event store: Kafka as the source of truth (event sourcing). Implementations: Symfony Messenger Doctrine transport, Transactionally Outbox libraries.",
    "aliases": [],
    "tags": [
        "messaging",
        "outbox",
        "consistency",
        "microservices",
        "patterns"
    ],
    "misconception": "A DB transaction plus broker publish are atomic — they're not. The broker publish can fail after the DB commits, losing the event silently.",
    "why_it_matters": "Outbox pattern closes the dual-write consistency gap — the most common source of lost events in event-driven microservices.",
    "common_mistakes": [
        "Dual-write without outbox — publish to broker then DB commit (or vice versa) can leave them inconsistent.",
        "Not cleaning up old outbox rows — table grows unboundedly.",
        "Relay not idempotent — duplicate publish on relay restart."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "message_broker",
        "exactly_once_delivery",
        "event_sourcing_messaging",
        "at_least_once_delivery"
    ],
    "prerequisites": [
        "message_broker",
        "database_transactions"
    ],
    "refs": [
        "https://microservices.io/patterns/data/transactional-outbox.html"
    ],
    "bad_code": "// Dual-write — inconsistent on failure:\n$db->beginTransaction();\n$db->insert('orders', $orderData);\n$db->commit();\n$broker->publish('order.created', $event); // Fails? Event lost.",
    "good_code": "// Outbox pattern:\n$db->beginTransaction();\n$db->insert('orders', $orderData);\n$db->insert('outbox', [\n    'topic' => 'order.created',\n    'payload' => json_encode($event),\n    'created_at' => now(),\n]);\n$db->commit();\n// Relay process reads outbox and publishes asynchronously\n// Idempotent: broker publish + delete outbox row",
    "quick_fix": "Add outbox table to every service that publishes events. Write to outbox in same transaction as domain data. Build idempotent relay process. Delete rows after confirmed publish.",
    "severity": "high",
    "effort": "high",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/outbox_pattern",
        "html_url": "https://codeclaritylab.com/glossary/outbox_pattern",
        "json_url": "https://codeclaritylab.com/glossary/outbox_pattern.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": "[Transactional Outbox Pattern](https://codeclaritylab.com/glossary/outbox_pattern) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/outbox_pattern"
            }
        }
    }
}