如何最小化 Kafka Streams 应用程序中的延迟?

How to minimize latency in a Kafka Streams application?

我的 Kafka Streams 应用程序从发送消息到发送关于不同主题的响应消息通常需要大约 100 毫秒。我可以调整哪些配置选项或可以使用哪些最佳实践来最大程度地减少延迟?

这似乎与生产者配置有关linger.ms

来自(http://kafka.apache.org/documentation/#producerconfigs

The producer groups together any records that arrive in between request transmissions into a single batched request. Normally this occurs only under load when records arrive faster than they can be sent out. However in some circumstances the client may want to reduce the number of requests even under moderate load. This setting accomplishes this by adding a small amount of artificial delay—that is, rather than immediately sending out a record the producer will wait for up to the given delay to allow other records to be sent so that the sends can be batched together. This can be thought of as analogous to Nagle's algorithm in TCP. This setting gives the upper bound on the delay for batching: once we get batch.size worth of records for a partition it will be sent immediately regardless of this setting, however if we have fewer than this many bytes accumulated for this partition we will 'linger' for the specified time waiting for more records to show up. This setting defaults to 0 (i.e. no delay). Setting linger.ms=5, for example, would have the effect of reducing the number of requests sent but would add up to 5ms of latency to records sent in the absense of load.

Kafka Streams 将此值设置为 100 毫秒(普通生产者默认值为 0 毫秒)以增加吞吐量。

您可以通过 StreamsConfig 参数 producer.linger.ms 减小该值。建议在 Streams 中为生产者配置添加前缀 producer. 以隔离 producer/consumer 配置。您可以使用 StreamsConfig.producerPrefix(ProducerConfig.LINGER_MS_CONFIG) 作为最方便的参数名称:)