kafka中多个partition怎么保证跨区消费是有序的

官方说明

这个问题严格来说是肯定有的,kafka只能保证分区内的有序性。

下面是kafka作者Jay Kreps的blog中介绍kafka设计思想的一段话:

Each partition is a totally ordered log, but there is no global ordering between partitions (other than perhaps some wall-clock time you might include in your messages). The assignment of the messages to a particular partition is controllable by the writer, with most users choosing to partition by some kind of key (e.g. user id). Partitioning allows log appends to occur without co-ordination between shards and allows the throughput of the system to scale linearly with the Kafka cluster size.

(每个分区都是一个完全有序的日志,但是分区之间没有全局排序(除了你的消息中可能包含的一些挂钟时间)。将消息分配给特定分区是可由编写者控制的,大多数用户选择通过某种密钥(例如用户id)进行分区。分区允许在没有分片之间协调的情况下发生日志追加,并允许系统的吞吐量与Kafka群集大小线性地扩展。)

解决方法

严格说,kafka是无法保证全局消息有序的,没有这个机制,只能局部有序。

但是如果只有一个分片和一个消息的生产者,那么就相当于消息全局有序了。如果有多个消息生产者,就算只有一个分片,如果这些生产者的消息都发给这个分片,那kafka中的消息连局部有序都没有办法了。

文章来源

添加新评论