> For the complete documentation index, see [llms.txt](https://eric-zhang-seattle.gitbook.io/mess-around/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eric-zhang-seattle.gitbook.io/mess-around/message-queue/kafka-overview/kafkaordering.md).

# Ordering

* [Ordering definition](#ordering-definition)
  * [Ordering across partition](#ordering-across-partition)
* [Flowchart for ordering guarantee within business domain](#flowchart-for-ordering-guarantee-within-business-domain)
  * [Cons - Uneven data distribution](#cons---uneven-data-distribution)
    * [Slot allocation](#slot-allocation)
    * [Consistent hashing](#consistent-hashing)
  * [Cons - Message disorder due to resizing](#cons---message-disorder-due-to-resizing)
    * [Problem](#problem)
    * [Solution](#solution)

## Ordering definition

* If the msg producing order is the same with msg consuming order, then msgs are in order.
  * The msg producing order is the order when msg arrives at broker.

![](/files/YL0LTGOw4lEA6Vm4uNev)

### Ordering across partition

* For Kafka, it could not guarantee the order across different partitions.
* Cross-partition ordering would typically need a role for coordinator.
  1. Suppose msg1 is produced before msg2 but msg2 arrives at consumer before msg1
  2. Then the coordinator needs to hold msg2 until msg1 is consumed.

## Flowchart for ordering guarantee within business domain

![](/files/egPoUpLLfYhrLmRKIAS3)

### Cons - Uneven data distribution

#### Slot allocation

* Steps
  1. Calculate a hashing value according to the business key.
  2. Calculate a slot index according to hashing\_value % slot\_num.
  3. Decide the mapping between slot and partition based on load.
* The mapping between slot and partition could be stored inside service registry.
* Redis uses 16385 slots, and the number of slots could be decided based on business cases.

![](/files/ZvWijhUHm1dQ3fPziaEV)

#### Consistent hashing

* Adjust the partition allocation on ring for data distribution.

![](/files/ftrHXwexjf74T6ktim0o)

### Cons - Message disorder due to resizing

#### Problem

* For example, message id = 3 and id = 7 will land on the same partition after resizing.
* However, when there are many backlogs on partition 0 for id = 3 and there is no backlog on partition 3 for id = 7 due to resizing, then id = 7 will be consumed before id = 3.

![](/files/0XBE0TWc2HYFKTFwKz2F)

#### Solution

* For all new partitions, wait for certain period which is long enough for old backlog messages to be consumed.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eric-zhang-seattle.gitbook.io/mess-around/message-queue/kafka-overview/kafkaordering.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
