前言
翻譯加整理~
How to choose the number of topics/partitions in a Kafka cluster
如何確定Topic需要多少個(gè)Partitions
一般情況是數(shù)據(jù)吞吐決定收苏,這里的吞吐的單位是MB/s牢硅,這里暫時(shí)不考慮kafka服務(wù)端的單partition的吞吐瓶頸晦溪,而是考慮Producer和Consumer兩端的吞吐
Producer
生產(chǎn)者的吞吐和以下幾個(gè)配置有關(guān):
- batching size
- compression codec
- acks
- replication factor
一般情況下,一個(gè)Producer的吞吐在10MB/s左右
Consumer
Consumer的吞吐和用戶邏輯強(qiáng)相關(guān),所以需要consumer的業(yè)務(wù)邏輯實(shí)現(xiàn)方來(lái)評(píng)估consumer的吞吐能力
確定partition數(shù)目
Given:
- p : producer throughput in MB/S
- c : consumer throughput in MB/s
- t : overall throughtput in MB/s
Result:
NumOfPartition = max(t/p, t/c)
動(dòng)態(tài)增加Partitions
Partition是可以動(dòng)態(tài)增加的,但是需要盡量在業(yè)務(wù)接入最初,對(duì)parttion數(shù)目做準(zhǔn)確評(píng)估,因?yàn)椴皇撬械臉I(yè)務(wù)場(chǎng)景都適合做動(dòng)態(tài)增加Partition數(shù)目操作蜒程。 對(duì)于Keyed messge绅你, 可以配置消息會(huì)按照key的hash值做partition的路由,這也保證了相同的key的消息的消費(fèi)是保序的昭躺。如果動(dòng)態(tài)增加partition數(shù)目忌锯,可能會(huì)導(dǎo)致亂序問(wèn)題。 對(duì)于這樣的業(yè)務(wù)場(chǎng)景领炫,一個(gè)安全的擴(kuò)容方案是先停掉所有的producer偶垮, consumer全部消費(fèi)完數(shù)據(jù)后,再做 add partition操作帝洪,然后在恢復(fù)producer的寫(xiě)入
partition數(shù)目過(guò)多帶來(lái)的問(wèn)題
- 增加open file handles
- 增加Broker宕機(jī)恢復(fù)時(shí)間
- 增加延遲
對(duì)每臺(tái)Broker來(lái)說(shuō)似舵,partition的數(shù)目不應(yīng)該超過(guò) 100 * (num of brokers in cluster) * (replication-factor), 對(duì)于個(gè)10臺(tái)broker,replication-factor=2的集群葱峡,單機(jī)partition的數(shù)目不應(yīng)該超過(guò) 2000個(gè)~
結(jié)論
確定Topic的一個(gè)合適的Partition數(shù)目很重要砚哗,太少了, producer或者consumer會(huì)出現(xiàn)讀寫(xiě)平靜砰奕,太多了蛛芥,會(huì)引起其他問(wèn)題
參考文章
How to choose the number of topics/partitions in a Kafka cluster