请仔细了解这张图,尤为注意有标志的几个关注点。咱们会不止一次回到这张图上算法
Kafka到底可以应用在高可用的业务上?官方给出的答案是确定的,最新版,已经支持消息队列的事务,但咱们对其性能是有疑问的。
Kafka根据配置的ACK级别,其性能表现将特别大,为了找到其适用场景,特作此测试,以便应用kafka时可以灵活应对。
测试过程还探讨了许多丢消息的场景。相对于大多数仅仅针对kafka集群自己的测试,本测试还介绍了丢消息的业务场景。整个方案应该是一个总体,才可以达到最高级别的高可用,不因该区别对待。apache
broker:微信
3台机器 8 core 16G 1T SSD Centos 6.8 kafka_2.12-0.10.2.0 broker jvm参数配置:Xms=8G Xmx=8G
client:网络
8 core 16G Centos 6.8
原创文章,转载注明出处 (http://sayhiai.com)session
集群高可靠性配置:架构
zookeeper.connection.timeout.ms=15000 zookeeper.session.timeout.ms=15000 default.replication.factor=3 num.partitions=6 min.insync.replicas=2 unclean.leader.election.enable=false log.flush.interval.ms=1000
ack并发
acks= all retries = 3 request.timeout.ms=5000
消息大小:1024byte异步
原创文章,转载注明出处 (http://sayhiai.com)jvm
下线一个节点,测试故障的恢复时间和故障期间的服务水平工具
将 replica.lag.time.max.ms 从 10s 调整为 60s(延长时间方便观察),而后 kill Broker 0,挑选 3 个 partition,观察 ISR 变化以下:
其中,第二 / 三阶段入队成功率受损:
而实际观察,第二 / 三阶段期间彻底没吞吐,缘由是压测工具不断报链接失败,中止了写入。
Kafka Broker leader 是经过 Controller 选举出来的,ISR 列表是 leader 维护的。
前者的的租约是 Controller 定义的,后者的租约是 Broker 配置 replica.lag.time.max.ms 指定的。
因此,第二阶段持续时间较短,是 Controller 的租约时间决定的,第三阶段持续时间较长,是 replica.lag.time.max.ms 决定的。
当 Broker 0 被 kill 时,前者影响原本 Broker 0 是 leader 的 1/3 partitions 的入队成功率,后者影响 Broker 0 做为 follower 的 2/3 partitions 的入队成功率。
kafka在failover期间,会有大约10秒的不可用时间,该时间由 replica.lag.time.max.ms 决定。所以应用程序须要处理此种状况下的异常信息,设置合理的重试次数和退避算法。
原创文章,转载注明出处 (http://sayhiai.com)
测试脚本:
./kafka-producer-perf-test.sh --topic test003 --num-records 1000000 --record-size 1024 --throughput -1 --producer.config ../config/producer.properties
不限制并发吞吐量
[root@l-monitor-logstash2.pub.prod.aws.dm bin]# time ./kafka-producer-perf-test.sh --topic ack001 --num-records 1000000 --record-size 1024 --throughput -1 --producer.config ../config/producer.properties [2017-09-14 21:26:57,543] WARN Error while fetching metadata with correlation id 1 : {ack001=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient) 81112 records sent, 16219.2 records/sec (15.84 MB/sec), 1416.2 ms avg latency, 1779.0 max latency. 92070 records sent, 18414.0 records/sec (17.98 MB/sec), 1671.7 ms avg latency, 1821.0 max latency. 91860 records sent, 18368.3 records/sec (17.94 MB/sec), 1670.3 ms avg latency, 1958.0 max latency. 91470 records sent, 18294.0 records/sec (17.87 MB/sec), 1672.3 ms avg latency, 2038.0 max latency. 91050 records sent, 18202.7 records/sec (17.78 MB/sec), 1678.9 ms avg latency, 2158.0 max latency. 92670 records sent, 18534.0 records/sec (18.10 MB/sec), 1657.6 ms avg latency, 2223.0 max latency. 89040 records sent, 17808.0 records/sec (17.39 MB/sec), 1715.0 ms avg latency, 2481.0 max latency. 86370 records sent, 17274.0 records/sec (16.87 MB/sec), 1767.5 ms avg latency, 2704.0 max latency. 91290 records sent, 18254.3 records/sec (17.83 MB/sec), 1670.2 ms avg latency, 2553.0 max latency. 92220 records sent, 18444.0 records/sec (18.01 MB/sec), 1658.1 ms avg latency, 2626.0 max latency. 90240 records sent, 18048.0 records/sec (17.63 MB/sec), 1669.9 ms avg latency, 2733.0 max latency. 1000000 records sent, 17671.591150 records/sec (17.26 MB/sec), 1670.61 ms avg latency, 2764.00 ms max latency, 1544 ms 50th, 2649 ms 95th, 2722 ms 99th, 2753 ms 99.9th. real 0m57.409s user 0m14.544s sys 0m2.072s
限制吞吐量 1w
[root@l-monitor-logstash2.pub.prod.aws.dm bin]# time ./kafka-producer-perf-test.sh --topic ack003 --num-records 1000000 --record-size 1024 --throughput 10000 --producer.config ../config/producer.properties [2017-09-15 10:51:53,184] WARN Error while fetching metadata with correlation id 1 : {ack003=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient) [2017-09-15 10:51:53,295] WARN Error while fetching metadata with correlation id 4 : {ack003=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient) 49766 records sent, 9953.2 records/sec (9.72 MB/sec), 34.9 ms avg latency, 358.0 max latency. 50009 records sent, 10001.8 records/sec (9.77 MB/sec), 23.9 ms avg latency, 39.0 max latency. 50060 records sent, 10008.0 records/sec (9.77 MB/sec), 23.9 ms avg latency, 49.0 max latency. 49967 records sent, 9991.4 records/sec (9.76 MB/sec), 23.6 ms avg latency, 38.0 max latency. 50014 records sent, 10000.8 records/sec (9.77 MB/sec), 24.0 ms avg latency, 51.0 max latency. 50049 records sent, 10007.8 records/sec (9.77 MB/sec), 23.5 ms avg latency, 37.0 max latency. 49978 records sent, 9995.6 records/sec (9.76 MB/sec), 23.5 ms avg latency, 44.0 max latency. 49803 records sent, 9958.6 records/sec (9.73 MB/sec), 23.7 ms avg latency, 47.0 max latency. 50229 records sent, 10045.8 records/sec (9.81 MB/sec), 23.6 ms avg latency, 46.0 max latency. 49980 records sent, 9996.0 records/sec (9.76 MB/sec), 23.5 ms avg latency, 36.0 max latency. 50061 records sent, 10010.2 records/sec (9.78 MB/sec), 23.6 ms avg latency, 36.0 max latency. 49983 records sent, 9996.6 records/sec (9.76 MB/sec), 23.4 ms avg latency, 37.0 max latency. 49978 records sent, 9995.6 records/sec (9.76 MB/sec), 23.9 ms avg latency, 55.0 max latency. 50061 records sent, 10012.2 records/sec (9.78 MB/sec), 24.3 ms avg latency, 55.0 max latency. 49981 records sent, 9996.2 records/sec (9.76 MB/sec), 23.5 ms avg latency, 42.0 max latency. 49979 records sent, 9991.8 records/sec (9.76 MB/sec), 23.8 ms avg latency, 39.0 max latency. 50077 records sent, 10013.4 records/sec (9.78 MB/sec), 23.6 ms avg latency, 41.0 max latency. 49974 records sent, 9994.8 records/sec (9.76 MB/sec), 23.4 ms avg latency, 36.0 max latency. 50067 records sent, 10011.4 records/sec (9.78 MB/sec), 23.8 ms avg latency, 65.0 max latency. 49963 records sent, 9992.6 records/sec (9.76 MB/sec), 23.5 ms avg latency, 54.0 max latency. 1000000 records sent, 9997.300729 records/sec (9.76 MB/sec), 24.24 ms avg latency, 358.00 ms max latency, 23 ms 50th, 28 ms 95th, 39 ms 99th, 154 ms 99.9th. real 1m40.808s user 0m16.620s sys 0m1.260s 更多... 吞吐量5k 1000000 records sent, 4999.275105 records/sec (4.88 MB/sec), 22.94 ms avg latency, 127.00 ms max latency, 23 ms 50th, 27 ms 95th, 31 ms 99th, 41 ms 99.9th. 吞吐量2w 1000000 records sent, 18990.827430 records/sec (18.55 MB/sec), 954.74 ms avg latency, 2657.00 ms max latency, 739 ms 50th, 2492 ms 95th, 2611 ms 99th, 2650 ms 99.9th. 吞吐量3w 1000000 records sent, 19125.212768 records/sec (18.68 MB/sec), 1527.07 ms avg latency, 3020.00 ms max latency, 1582 ms 50th, 2815 ms 95th, 2979 ms 99th, 3011 ms 99.9th.
12分区,2.6w吞吐量
[root@l-monitor-logstash2.pub.prod.aws.dm bin]# time ./kafka-producer-perf-test.sh --topic ack001 --num-records 1000000 --record-size 1024 --throughput 26000 --producer.config ../config/producer.properties 129256 records sent, 25840.9 records/sec (25.24 MB/sec), 31.9 ms avg latency, 123.0 max latency. 129794 records sent, 25953.6 records/sec (25.35 MB/sec), 28.6 ms avg latency, 73.0 max latency. 130152 records sent, 26025.2 records/sec (25.42 MB/sec), 28.3 ms avg latency, 64.0 max latency. 130278 records sent, 26045.2 records/sec (25.43 MB/sec), 28.1 ms avg latency, 55.0 max latency. 130106 records sent, 26010.8 records/sec (25.40 MB/sec), 27.9 ms avg latency, 45.0 max latency. 130080 records sent, 26005.6 records/sec (25.40 MB/sec), 27.7 ms avg latency, 41.0 max latency. 130093 records sent, 26013.4 records/sec (25.40 MB/sec), 74.5 ms avg latency, 343.0 max latency. 1000000 records sent, 25904.051394 records/sec (25.30 MB/sec), 38.33 ms avg latency, 343.00 ms max latency, 28 ms 50th, 122 ms 95th, 242 ms 99th, 321 ms 99.9th. real 0m39.395s user 0m12.204s sys 0m1.616s
cpu与内存无任何变化。网络rx/tx :170Mbps/120Mbps,磁盘IoUtil: 6%。1百万数据能在2分钟内完成。
影响提交效率的缘由主要有:partition数量 + 超时时长 + 消息大小 + 吞吐量
最终结论:假定网络状态良好,在ack=all模式、超时10秒、重试3次、分区为6的状况下,可以承受1.3w/s的消息请求,其写入平均耗时不超过30ms,最大耗时不超过500ms。想要增长TPS,能够增长partition到12,可以达到2.6w/s的高效写入。
kafka生产和消费理论上不受消息堆积影响,消息堆积只是占用磁盘空间,这里的消息堆积是指topic中的消息数,和消息是否消费无关
原创文章,转载注明出处 (http://sayhiai.com)
kafka采用基于时间的SLA(服务水平保证),重要消息保存3天。
基本配置:消息1k大小,ack=all,即全部副本都同步的状况。为确保消息可靠,所有采用3个副本。
注意:生产端,考虑一种场景,单条发送,而后调用future.get()确认,TPS会急剧下降到2k如下,请确认确实须要这么作,不然,使用异步提交,callback调用的方式。相对于ACK模式1.6w的TPS,普通模式提交,可以达到13w(主要是网络和IO瓶颈,带宽占满)。当吞吐量限制在1w左右而且开启ACK(很是符合咱们的业务特征),kafka是高效且高可用的,平均耗时仅24毫秒,生产者的最佳实践是将超时设置成10秒,重试3次。消费者一样是高效的,6个partition、ack模式,平均耗时在20毫秒左右,具体处理耗时取决于消费端的处理能力。
=2节点当机(机房断电等),服务不可用。故障恢复须要两个节点达到同步状态,与总体数据量相关。磁盘每秒fsync,极端状况(所有当机),最多会丢失1秒数据。
原创文章,转载注明出处 (http://sayhiai.com)
原创文章,转载注明出处 (http://sayhiai.com)
订阅微信公众号,小姐姐,教你玩架构~