請仔細瞭解這張圖,尤爲注意有標誌的幾個關注點。咱們會不止一次回到這張圖上算法
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)
訂閱微信公衆號,小姐姐,教你玩架構~