WIP-kafka的高吞吐原因

kafka 是一个 高吞吐 的消息队列。 那么,kafka 是如何做到高吞吐的呢?

  1. 页缓存
  2. 顺序写
  3. 零拷贝
  4. 批量发

要弄清楚: 具体是怎么做的?

比如,页缓存,是如何存的?怎么使用?什么时候更新?会不会有丢失?如何监控命中率?

比如,批量发,批量是如何打包的、如何存储的?

  • 页缓存,当程序挂了,数据不会丢,当 OS 挂了,数据就丢了。
  • kafka 的大多数应用场景都是 读写 1:1 的。
  • kafka 用的是操作系统的 读写缓存,但有个弊端,就是 写缓存 的可靠性不好保证。大家日常的缓存使用方式,是 只读缓存
  • 批量发送,除了在 发送的时候会进行打包,实际在存储的时候,也是直接存打包好的,消费的时候,同样是返回打包好的。 一切交给客户端处理,有时也是一种不错策略。

压测

一些压测工具的调研:

  1. https://github.com/jamiealquiza/sangrenel (sarama)
  2. kafka 自带 kafka-producer-perf-test.sh
  3. https://openmessaging.cloud/docs/benchmarks/
  4. https://github.com/kmgowda/SBK
  5. https://github.com/brianfrankcooper/YCSB (not only kafka)
  6. https://github.com/six-ddc/plow (http)
  7. https://github.com/akopytov/sysbench (mysql、pg、操作系统)
  8. https://github.com/bojand/ghz (grpc)
  9. https://github.com/GoogleCloudPlatform/PerfKitBenchmarker (云服务)

看板一般用 grafana 做看板,也可以参考 这个项目

应用中的经验

golang 下使用 sarama :

发现 sarama 有 metrics 记录,因此调研了一系列将 sarama 的 metrics 存到 promethues 的方法,由于 sarama 使用的是 https://github.com/rcrowley/go-metrics ,因此直接使用相关的包 https://github.com/deathowl/go-metrics-prometheus ,但由于记录的 metrics 直接通过 metrics 名做区分,而不是 label,在制作看板时不太灵活,之后可以使用 https://github.com/iamlongalong/saramaprom 作为exporter。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// config 为 sarama 的 config 实例
func exportMetrics() error {
ctx := context.Background()
err := saramaprom.ExportMetrics(ctx, config.MetricRegistry, saramaprom.Options{
Namespace: "mastergo",
Subsystem: "sarama",
FlushInterval: time.Second * 5,
Labels: map[string]string{
"appname": conf.DeepGet("app_name").String("merger-index"),
},
})
if err != nil {
return err
}

return nil
}

TODO

不错的学习资料

文档直通车


Gratitude is not only the greatest of virtues, but the parent of all the others.
Cicero


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!