Apache Kafka 在并行向代理发送多条消息时如何维护顺序?

How Apache Kafka maintain order when it sends multiple messages to broker in parallel?

默认情况下,max.in.flight.requests.per.connection == 5,表示 Kafka 将最多并行发送 5 条消息给 broker。如果这些消息并行进入同一个分区,它如何管理顺序?

如果您设置 retries > 0.

,Kafka 无法保证这些消息的顺序

如果您希望获得排序保证,您需要将此值设置为 max.in.flight.requests.per.connection == 1

producer configs retries:

上的 Kafka 文档给出了解释

Allowing retries without setting max.in.flight.requests.per.connection to 1 will potentially change the ordering of records because if two batches are sent to a single partition, and the first fails and is retried but the second succeeds, then the records in the second batch may appear first.

默认情况下,max.in.flight.requests.per.connection > 1,批次可以重新排序。

如果你想保证顺序,你可以:

  • 设置max.in.flight.requests.per.connection=1。根据您的吞吐量,这可能会对性能产生重大影响。

  • 通过将 enable.idempotence 设置为 true 使用幂等生产者。这使得仍然可以利用 max.in.flight.requests.per.connection=5 但也强制执行批量排序。启用幂等性后,生产者会为所有批次分配序列号,以便经纪人可以验证他们的订单。性能影响通常很小。