> ## Documentation Index
> Fetch the complete documentation index at: https://docs.automq.com/llms.txt
> Use this file to discover all available pages before exploring further.

# AutoMQ vs. Apache Kafka 性能和成本对比

> AutoMQ revolutionizes Kafka storage with cloud-native architecture, ensuring a 14x cost reduction while maintaining 100% Kafka API compatibility and superior scalability.

AutoMQ 基于云存储对 Apache Kafka 的存储层进行云原生重构，不仅 100% 兼容 Apache Kafka 的 API，并且在 1GiB/s 流量下，相比 Apache Kafka 取得了 17x 成本优势。每月 TCO 从 Apache Kafka $226,671 下降到 $12,899。费用明细如下表所示：

| 对比项<br />     | AutoMQ<br />   | Apache Kafka<br /> |
| ------------- | -------------- | ------------------ |
| 计算<br />      | \$2859<br />   | \$18,447<br />     |
| 存储<br />      | \$5,961<br />  | \$69,984<br />     |
| S3 API<br />  | \$3,952<br />  | \$0<br />          |
| 跨可用区流量费<br /> | \$128<br />    | \$138,240<br />    |
| 总计<br />      | \$12,899<br /> | \$226,671<br />    |

## Benchmark 准备

使用 1 GiB/s 的流量向一个有 256 分区的 Topic 进行生产消费压测。

### AutoMQ 集群

AutoMQ 可以通过 Bitnami Kafka Helm Chart 在 k8s 上部署。可以参考 [Install AutoMQ by Helm Chart](https://github.com/AutoMQ/automq/blob/main/chart/bitnami/README.md) 在 AWS 上部署一个 3 可用区的 AutoMQ 集群：

* 总共 6 个节点，3 个 Server 和 3 个 Broker，均匀分布在 3 个可用区；

* 物理节点机型选择 m7g.4xlarge：规格 16C64G、网络基线 960 MiB/s、价格 \$476.544 / 月。

### 压力机

压力机在 3 个可用区各部署 1 台 m7g.4xlarge，模拟多可用区的业务负载。使用 AutoMQ 提供的 `automq-perf-test.sh` 压测脚本进行压测，配置如下

```bash theme={null}

KAFKA_HEAP_OPTS="-Xmx32g -Xms32g" nohup ./bin/automq-perf-test.sh --bootstrap-server $bootstrap_server  --record-size 1024 --topics 1 --partitions-per-topic 256 --topic-prefix perf --producers-per-topic 20 --groups-per-topic 1 --consumers-per-group 20 --send-rate 349525 --warmup-duration 2 --test-duration 30 --producer-configs batch.size=1048576 linger.ms=100 buffer.memory=67108864 max.request.size=33554432 client.id='automq_az=apse1-az1' --consumer-configs fetch.max.bytes=104857600 max.partition.fetch.bytes=104857600 client.id='automq_az=apse1-az1' --reset &> nohup.log &

KAFKA_HEAP_OPTS="-Xmx32g -Xms32g" nohup ./bin/automq-perf-test.sh --bootstrap-server $bootstrap_server ... client.id='automq_az=apse1-az2' --await-topic-ready false &> nohup.log &

KAFKA_HEAP_OPTS="-Xmx32g -Xms32g" nohup ./bin/automq-perf-test.sh --bootstrap-server $bootstrap_server ... client.id='automq_az=apse1-az3' --await-topic-ready false &> nohup.log &

```

通过该配置，压力机会模拟如下场景：

* 总共 60 个 Producer 和 60 个 Consumer，均匀的分布在 3 个可用区，通过 `client.id=automq_az=apse1-az1` 来标记客户端所属的可用区；

* 每秒发送 1,048,575 条 1KiB 大小的 Record，总共发送 1 GiB/s 的数据；

* 发送参数额外添加 `batch.size=1048576 linger.ms=100` 来获得更好的攒批效果，适配大多数 Kafka 的高吞吐场景。

## 成本

在数据保存 3 天（`log.retention.hours=72`）的场景下，以 AWS 的 us-east-1 地域为例，AutoMQ 的总持有成本为 \$12,899 每月。总持有成本包括：计算、存储、S3 API 和跨可用区流量。

* 计算：AutoMQ 在该压测场景使用了 6 台 m7g.xlarge，计算资源总成本为 6 \* $476.544 = $2,859 每月。

* 存储：流量 \* 3天 \* S3单价 = 1 \* (60 \* 60 \* 24 \* 3) \* 0.023 = \$5,961 每月。

* S3 API：在该流量下，平均 Get 1,278/s、Put 202.662/s，成本为 \$3,952 每月。

<img src="https://mintcdn.com/automq/C1RFhd9pcTVZzYyW/zh/automq/benchmarks/automq-vs-apache-kafka-benchmarks-and-cost/1.webp?fit=max&auto=format&n=C1RFhd9pcTVZzYyW&q=85&s=1a7ed917660ed050eceb08ec30bc666c" width="1280" height="765" data-path="zh/automq/benchmarks/automq-vs-apache-kafka-benchmarks-and-cost/1.webp" />

* 跨可用区流量：虽然 AutoMQ 实现了客户端只往同可用区的 Broker 收发消息，但是 Broker 之间仍旧会有少量的 RPC 请求来同步 KRaft 元数据和转发 ZoneRouterProduceRequest。我们使用 `iftop -t -s 60 -L 100` 在 Broker 节点上采集跨可用区流量。一个节点的跨可用区 Network In + Network Out 平均为 0.84 MiB/s，6 个节点的跨可用区流量成本为 0.84 / 1024 \* 6 \* (60 \* 60 \* 24 \* 30) \* 0.01 \~= \$127 每月。

AutoMQ 的成本总和为：计算 + 存储 + S3 API + 跨可用区流量 \~= \$12,899 每月。

## 相比 Apache Kafka 14x 成本降低

相同的负载在 Apache Kafka 则需要 \$65,385 每月，是 AutoMQ 的 14x 倍。总持有成本包括：计算、存储和跨可用区流量。

* 存储：存储 1 / 1024 \* (60 \* 60 \* 24 \* 3) = 253.125 TiB 的数据。3 副本存储则需要消耗 759.375 TiB 的空间。再考虑到 Buffer 和数据分布不均衡，假设 50% 的有效磁盘利用率，则需要准备 1,518.75 TiB 的磁盘空间。为了节省磁盘存储成本存储介质使用 st1，那么存储成本为 1518.75 \* 1024 \* 0.045 = \$69,984 每月。

* 计算：一块 EBS 最大 16TiB，那么至少需要 95 个 Volume， 对应 95 个 Broker。按照[生产实践](https://www.confluent.io/blog/design-and-deployment-considerations-for-deploying-apache-kafka-on-aws/#choosing-instance-types)最小的推荐机型为 r4.xlarge，成本为 \$18,447.1 每月。

* 跨可用区流量：在分区和流量均匀分布的情况下，有 2 / 3 的生产流量会跨可用区发送。消费可以通过 Fetch From Follower 免去跨可用区流量。Fetch From Follower 意味着对应的副本会分布在 3 个可用区，Broker ISR 之间又产生两份生产流量。总体上跨可用区流量为：生产流量 \* (2 / 3 + 2) \* 30d = 1024 \* (2 / 3 + 2) / 1024 \* (60 \* 60 \* 24 \* 30) \* 0.02 = \$138,240 每月。

Apache Kafka 的成本总和为：计算 + 存储 + 跨可用区流量 = \$226,671 每月。

除了静态成本降低以外，AutoMQ 的无状态架构还提供了更多的成本优势：

* AutoBalancing：AutoMQ 在 Controller 中内置负载均衡组件，会自动根据节点间的流量进行负载均衡，且分区迁移耗时不到 2s，无需运维人员手工负载均衡。

* No Overprovisioning：节点无状态和秒级分区迁移让 AutoMQ 可以分钟级（时间花费在资源准备）的进行伸缩，扩容后秒级完成负载均衡。AutoMQ 无需为高峰提前预留资源和小时级的负载均衡。

## 性能

每个 AutoMQ Broker 的 CPU 水位为 50%。有充足空闲的 CPU 来应对流量尖刺和更多的小包请求。

<img src="https://mintcdn.com/automq/C1RFhd9pcTVZzYyW/zh/automq/benchmarks/automq-vs-apache-kafka-benchmarks-and-cost/2.webp?fit=max&auto=format&n=C1RFhd9pcTVZzYyW&q=85&s=a7b6196b348328937834d24b5316bddb" width="3192" height="866" data-path="zh/automq/benchmarks/automq-vs-apache-kafka-benchmarks-and-cost/2.webp" />

AutoMQ 写 S3 默认使用 8MiB / 250ms 攒批参数。Producer 使用 ack=all，Record 持久化到 S3 后再返回给客户端成功。在该测试中，延迟表现如下：

| -<br />       | AVG<br />   | P50<br />   | P99<br />    | P999<br />   |
| ------------- | ----------- | ----------- | ------------ | ------------ |
| Produce<br /> | 472ms<br /> | 465ms<br /> | 823ms<br />  | 1049ms<br /> |
| E2E<br />     | 631ms<br /> | 602ms<br /> | 1189ms<br /> | 1549ms<br /> |

如果需要更低的延迟，可以调低配置 `s3.wal.path` 和 `automq.zonerouter.channels` 中 `batchInterval` 参数来降低攒批写入 S3 的等待延迟。不过在获得更低的延迟的同时，S3 API 的成本会随之上升。
