{
    "slug": "message_broker",
    "term": "Message Broker",
    "category": "messaging",
    "difficulty": "intermediate",
    "short": "A message broker is middleware that receives, routes, and delivers messages between producers and consumers — decoupling services and enabling async communication at scale.",
    "long": "Message brokers: Kafka (high-throughput log, replay), RabbitMQ (feature-rich routing, AMQP), Redis Streams (lightweight, Kafka-like), SQS/SNS (AWS managed). Core concepts: producer sends to broker, consumer reads from queue/topic, broker persists until acknowledged. Benefits: decoupling (producers don't know consumers), durability (messages persist), load levelling, retries, dead letter queues. Patterns: point-to-point queue (one consumer gets each message), pub/sub topic (all subscribers get each message). PHP: php-amqplib (RabbitMQ), rdkafka, aws-sdk (SQS), Redis Streams via Predis/PhpRedis.",
    "aliases": [],
    "tags": [
        "messaging",
        "broker",
        "async",
        "microservices"
    ],
    "misconception": "Message brokers and job queues are the same — job queues (Laravel Queue, Symfony Messenger) are application-level abstractions that can use message brokers as a transport.",
    "why_it_matters": "Message brokers are the backbone of microservice communication — they enable services to scale independently and survive each other's failures.",
    "common_mistakes": [
        "Not acknowledging messages — broker redelivers indefinitely.",
        "Ignoring message TTL — queue grows unboundedly on consumer failure.",
        "Using message broker where a job queue suffices — adds operational complexity."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "kafka_concepts",
        "rabbitmq_concepts",
        "pub_sub_pattern",
        "producer_consumer"
    ],
    "prerequisites": [
        "producer_consumer",
        "async_processing"
    ],
    "refs": [
        "https://www.rabbitmq.com/tutorials/amqp-concepts.html"
    ],
    "bad_code": "// Synchronous service calls — tight coupling:\n$orderService->process($order);\n$inventoryService->decrement($items); // Fails? Order partially processed",
    "good_code": "// Async via message broker:\n$bus->dispatch(new OrderPlaced($order->id));\n// OrderPlacedHandler runs asynchronously, retried on failure\n// InventoryHandler subscribes separately, independently retried",
    "quick_fix": "Choose broker based on needs: Kafka for high-throughput replay, RabbitMQ for flexible routing, Redis Streams for lightweight, SQS for managed. Always acknowledge messages.",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/message_broker",
        "html_url": "https://codeclaritylab.com/glossary/message_broker",
        "json_url": "https://codeclaritylab.com/glossary/message_broker.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": "[Message Broker](https://codeclaritylab.com/glossary/message_broker) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/message_broker"
            }
        }
    }
}