{
    "slug": "message_ordering",
    "term": "Message Ordering Guarantees",
    "category": "messaging",
    "difficulty": "intermediate",
    "short": "Message ordering is only guaranteed within a single Kafka partition or RabbitMQ queue — multiple partitions or consumers break FIFO order across the full topic.",
    "long": "Kafka: messages within a partition are strictly ordered (by offset). Messages across partitions are not ordered. Partition key determines partition — same key = same partition = ordered. Consumer group with one consumer per partition = ordered processing. Multiple consumers on one partition: not supported (1 consumer per partition max). RabbitMQ: single queue = FIFO within the queue. Multiple consumers on one queue = no ordering. Priority queues alter order. SQS Standard: no order guarantee. SQS FIFO: ordered within a message group, limited throughput. For ordering across entities: use the entity ID as the partition/group key.",
    "aliases": [],
    "tags": [
        "messaging",
        "ordering",
        "kafka",
        "reliability"
    ],
    "misconception": "Kafka guarantees global message ordering — it only guarantees order within a partition. For global ordering, use a single partition (kills parallelism).",
    "why_it_matters": "Incorrect assumptions about ordering cause processing of 'update' before 'create', or refund before payment — subtle but critical bugs in financial and order systems.",
    "common_mistakes": [
        "Using round-robin partition assignment for order-sensitive messages — breaks sequencing.",
        "Multiple RabbitMQ consumers on an ordered queue — no longer FIFO.",
        "SQS Standard for ordered processing — use FIFO queues."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "kafka_concepts",
        "consumer_group",
        "topic_partition",
        "at_least_once_delivery"
    ],
    "prerequisites": [
        "kafka_concepts"
    ],
    "refs": [
        "https://kafka.apache.org/documentation/#intro_topics"
    ],
    "bad_code": "// Random partition — order.updated may arrive before order.created:\n$producer->produce(RD_KAFKA_PARTITION_UA, 0, $message, null); // Random partition",
    "good_code": "// Same key = same partition = ordered:\n$producer->produce(RD_KAFKA_PARTITION_UA, 0, $message, $orderId); // Order by key\n\n// Kafka: max 1 consumer per partition for ordering:\n// consumer group size ≤ partition count",
    "quick_fix": "Use entity ID as Kafka partition key for ordered processing of entity events. Limit consumer count to partition count. Use SQS FIFO for managed ordering.",
    "severity": "high",
    "effort": "medium",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/message_ordering",
        "html_url": "https://codeclaritylab.com/glossary/message_ordering",
        "json_url": "https://codeclaritylab.com/glossary/message_ordering.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 Ordering Guarantees](https://codeclaritylab.com/glossary/message_ordering) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/message_ordering"
            }
        }
    }
}