如果MQ没有类似数据库事务结构和保证,是不可能达到消息投递100%可靠的,极端情况下消息投递要么丢失或重复。
下面咋们从producer,broker,consumer的角度分析一下Kafka中会出现哪些情况:
1.producer发送消息到Broker
目前生产者发送消息(request.required.acks)有三种方式。
acks = 0: producer不会等待broker发送ack ,因为发送消息网络超时或broker crash(1.Partition的Leader还没有commit消息 2.Leader与Follower数据不同步),既有可能丢失也可能会重发。
acks = 1: 当leader接收到消息之后发送ack,丢会重发,丢的概率很小
acks = -1: 当所有的follower都同步消息成功后发送ack. 丢失消息可能性比较低。
2.Consumer从Broker拉取消息
Kafka中有两种consumer接口,分别为Low-level API和High-levelAPI
(1). Low-level API SimpleConsumer
这套接口比较复杂的,使用者必须要考虑很多事情,优点就是对Kafka可以有完全的控制。
(2). High-level API ZookeeperConsumerConnector
High-level API使用比较简单,已经封装了对partition和offset的管理,默认是会定期自动commit offset,这样可能会丢数据的,因为consumer可能拿到数据没有处理完crash。 High-level API接口的特点,自动管理,使用简单,但是对Kafka的控制不够灵活。
3. Broker分析
(1). 对于broker,落盘的数据,除非磁盘坏了,一般不会丢的。
(2). 对于内存脏(没有flush磁盘)数据,broker重启会丢.
可以通过log.flush.interval.messages和log.flush.interval.ms来配置flush间隔,interval大丢的数据多些,小会影响性能
但在0.8.x版本以后,可以通过replica机制保证数据不丢,代价就是需要更多资源,尤其是磁盘资源,kafka当前支持GZip和Snappy压缩,来缓解这个问题。
是否使用replica取决于在可靠性和资源代价之间的平衡。
总结
Kafka只是能保证at-least once消息语义,即数据是可能重复的,这个在应用上需要可以容忍,
对于Kafka consumer,一般情况下推荐使用high-level API接口,最好不要直接使用low-level API,自己写起来比较麻烦和困难。
相关推荐
Apache Kafka实战.pdf..
This book is here to help you get familiar with Apache Kafka and to solve your challenges related to the consumption of millions of messages in publisher-subscriber architectures. It is aimed at ...
Spring for Apache Kafka API。 Spring for Apache Kafka 开发文档。
Apache Kafka源码剖析 PDF较大,分5份上传!一起解压即可。
apache kafka技术内幕 和 apacke kafka源码分析2本PDF 电子书 网盘下载
Apache Kafka Apache Kafka Apache Kafka Apache Kafka
Apache Kafka is a popular distributed streaming platform that acts as a messaging queue or an enterprise messaging system. It lets you publish and subscribe to a stream of records and process them in ...
Building Data Streaming Applications with Apache Kafka 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Apache Kafka 官方文档中文版,Apache Kafka 官方文档中文版
Kafka是一个对于像Hadoop的一样的日志数据和离线分析系统,但又要求实时处理的限制,这是一个可行的解决方案。 1.Kafka集群包含一个或多个服务器,这种服务器被称为broker 2.Partition是物理上的概念,每个Topic...
Streaming Architecture New Designs Using Apache Kafka and MapR Streams
Learning Apache Kafka Second Edition provides you with step-by-step, practical examples that help you take advantage of the real power of Kafka and handle hundreds of megabytes of messages per second ...
。Apache Kafka Cookbook(PACKT,2015)
with no previous knowledge of Apache Kafka. As the book progresses, the difficulty level increases. The second half of this cookbook is about configuration; this is advanced material for those who ...
Apache Kafka_源码分析
Apache Kafka(带书签)
Kafka 是由 LinkedIn 开发的一个分布式的消息系统, 使用 Scala 编写, 它以可水平扩展和高吞吐率而被广泛使用。 目前越来越多的开源分布式处理系统如Cloudera、 Apache Storm、Spark 都支持与 Kafka 集成。
Apache Kafka实战epu.rar
Apache Kafka 企业级消息队列的原理、基本架构、搭建 Kafka 集群、操作集群的两种方式、Apache Kafka 原理、消费策略、负载均衡等。