【Kafka】Kafka的数据可靠性和一致性

    在大数据领域的,Kafka作为消息发布订阅系统,为同学们所熟悉,它有非常好的扩展性。能够在大数据环境下实现高吞吐量和高可用。那么Kafka是如何保证数据的可靠性和一致性的呢?

  • 可靠性

    可靠性方面,Kafka主要在Leader的选举、Broker的分布式部署、Partition的replication和Producer的acks三个方向进行保障。

Leader选举机制

    Kafka Leader主要从维护的ISR(in-sync replicas)列表中选出,那么什么是ISR列表呢?我们来看下面一张图

c08b84ab4ff33f7de012e263d9ae5139.jpeg

这里我们可以发现Kafka的每一个partition都维护了一个ISR列表,这个ISR官方的解释是:"isr" is the set of "in-sync" replicas. This is the subset of the replicas list that is currently alive and caught-up to the leader.

也就是说ISR是满足指定同步条件的replica的集合。它是所有replica的子集。为什么我这里说是满足了指定条件的replica呢,因为要进入ISR列表,一般需要满足两个条件,follower即落后的消息数不超过replica.lag.max.messages所配置的数值并且follower能够在指定的时间(replica.lag.time.max.ms)内向leader发送fetch请求,则不会将它从ISR列表中删除。这样在unclean.leader.election.enable=false,的情况下,如果leader挂了,则kafka会从ISR列表中选择第一个follower作为leader,这就保证了已经提交的数据的可靠性。

Broker的分布式部署

    Broker的分布式部署(一般三个以上实例),保证了数据在传输过程中,不会因为单Broker挂掉而导致数据丢失。

Partition的replications

    Kafka 从 0.8.0 版本开始引入了分区副本KAFKA-50的概念,我们在创建Kafka Topic的时候,通过指定replication-factor可以设置Partition的副本数,也可以在配置文件中通过参数:default.replication.factor,指定默认的分区副本数。

Producer的acks

    Producer通过配置的acks来判断消息是否发送成功。

我们来看官方的解释

The number of acknowledgments the producer requires the leader to have received before considering a request complete. This controls the durability of records that are sent. The following settings are allowed:

  • acks=0 If set to zero then the producer will not wait for any acknowledgment from the server at all. The record will be immediately added to the socket buffer and considered sent. No guarantee can be made that the server has received the record in this case, and the retriesconfiguration will not take effect (as the client won't generally know of any failures). The offset given back for each record will always be set to -1.

如果设置acks=0,表示不等待ack确认,则立刻认为发送成功,并进行下一次发送。在这种模式下,Kafka的吞吐量非常大,但是发送丢失数据的概率也随之变大,是很有可能丢失的。

  • acks=1 This will mean the leader will write the record to its local log but will respond without awaiting full acknowledgement from all followers. In this case should the leader fail immediately after acknowledging the record but before the followers have replicated it then the record will be lost.

如果设置acks=1,表示等待leader反馈接收成功后,则认为发送成功,并进行下一次发送。如果Leader在接收到消息后,仍未同步到Follower,此时发生了崩溃,是有可能导致数据丢失的。

  • acks=all This means the leader will wait for the full set of in-sync replicas to acknowledge the record. This guarantees that the record will not be lost as long as at least one in-sync replica remains alive. This is the strongest available guarantee. This is equivalent to the acks=-1 setting.

acks=all或者acks=-1这种情况是最安全的,当然效率也是最低的。Producer会等待Leader返回确认信息,而Leader会等待所有的副本均同步完成。想要深入研究的同学min.insync.replicas了解一下

  • 一致性

    为了保障不同的消费者在数据消费过程中的一致性,Kafka引入了High Water Mark 机制,当我们将隔离级别isolation.level设置为:read_committed时,Kafka会保证所有消费者所消费的消息都是在High Water Mark之下。所谓High Water Mark类似于木桶原理所示,水位线的最高点为已经同步到所有Follower的消息所对应的的offset。也就是ISR列表中偏移量最小的副本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值