:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(usage): follow WarpStream's producer recommendations (#92634)
Ben White54d8a2a4582f
diff --git a/rust/usage-ingestion/src/config.rs b/rust/usage-ingestion/src/config.rsindex 0d45c6ef40b6..d3da52a5f266 100644--- a/rust/usage-ingestion/src/config.rs+++ b/rust/usage-ingestion/src/config.rs@@ -1,5 +1,17 @@+use common_kafka::config::KafkaConfig;use envconfig::Envconfig;+/// WarpStream's recommended librdkafka producer settings.+/// <https://docs.warpstream.com/warpstream/kafka/configure-kafka-client/tuning-for-performance>+const BATCH_SIZE: u32 = 16_000_000;+const BATCH_NUM_MESSAGES: u32 = 100_000;+/// librdkafka caps a produce request at this, so it has to exceed `BATCH_SIZE`.+const MESSAGE_MAX_BYTES: u32 = 64_000_000;+const MAX_IN_FLIGHT_PER_CONNECTION: u32 = 1_000_000;+const METADATA_REFRESH_INTERVAL_MS: u32 = 60_000;++const _: () = assert!(MESSAGE_MAX_BYTES > BATCH_SIZE);+#[derive(Envconfig, Clone)]pub struct Config {#[envconfig(from = "USAGE_INGESTION_GRPC_ADDRESS", default = "0.0.0.0:7143")]@@ -12,6 +24,14 @@ pub struct Config {pub kafka_hosts: String,#[envconfig(from = "KAFKA_TLS", default = "false")]pub kafka_tls: bool,+ /// Only "none", "gzip", "snappy" and "lz4" work. "zstd" needs an rdkafka feature the+ /// workspace does not enable, so librdkafka refuses it when it builds the producer.+ #[envconfig(from = "KAFKA_COMPRESSION_CODEC", default = "lz4")]+ pub kafka_compression_codec: String,+ /// These records never fill a batch, so linger decides when a produce goes out and lands+ /// in ingest latency. Lower it if that matters more than batching.+ #[envconfig(from = "KAFKA_PRODUCER_LINGER_MS", default = "100")]+ pub kafka_producer_linger_ms: u32,#[envconfig(from = "USAGE_INGESTION_MAX_BATCH_SIZE", default = "500")]pub max_batch_size: usize,// Overridable so a test environment can use the suffixed topic its Kafka engine table reads.@@ -29,4 +49,87 @@ impl Config {}Ok(())}++ /// A setting dropped from here reverts to a `KafkaConfig` default instead of failing.+ pub fn kafka_config(&self) -> KafkaConfig {+ KafkaConfig {+ kafka_hosts: self.kafka_hosts.clone(),+ kafka_tls: self.kafka_tls,+ kafka_client_id: "usage-ingestion".to_string(),+ kafka_compression_codec: self.kafka_compression_codec.clone(),+ kafka_producer_linger_ms: self.kafka_producer_linger_ms,+ kafka_producer_batch_size: Some(BATCH_SIZE),+ kafka_producer_batch_num_messages: Some(BATCH_NUM_MESSAGES),+ kafka_producer_message_max_bytes: Some(MESSAGE_MAX_BYTES),+ kafka_producer_max_in_flight_requests_per_connection: Some(+ MAX_IN_FLIGHT_PER_CONNECTION,+ ),+ kafka_producer_topic_metadata_refresh_interval_ms: Some(METADATA_REFRESH_INTERVAL_MS),+ // WarpStream serves a different agent per connection, which collapses throughput+ // when librdkafka pins an idempotent producer's sequence numbers to one.+ kafka_producer_enable_idempotence: Some(false),+ // acks and the partitioner stay at librdkafka's defaults, which match WarpStream's+ // advice: acks=all keeps a billing record durable, and these records carry no key.+ ..Default::default()+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ fn config() -> Config {+ Config {+ grpc_address: "0.0.0.0:7143".to_string(),+ metrics_address: "0.0.0.0:7144".to_string(),+ database_url: "postgres://localhost/test".to_string(),+ kafka_hosts: "localhost:9092".to_string(),+ kafka_tls: false,+ kafka_compression_codec: "lz4".to_string(),+ kafka_producer_linger_ms: 100,+ max_batch_size: 500,+ topic: "clickhouse_billing_usage_records".to_string(),+ }+ }++ #[test]+ fn the_producer_follows_the_warpstream_recommendations() {+ let kafka = config().kafka_config();++ assert_eq!(kafka.kafka_compression_codec, "lz4");+ assert_eq!(kafka.kafka_producer_linger_ms, 100);+ assert_eq!(kafka.kafka_producer_batch_size, Some(BATCH_SIZE));+ assert_eq!(+ kafka.kafka_producer_batch_num_messages,+ Some(BATCH_NUM_MESSAGES)+ );+ assert_eq!(+ kafka.kafka_producer_message_max_bytes,+ Some(MESSAGE_MAX_BYTES)+ );+ assert_eq!(+ kafka.kafka_producer_max_in_flight_requests_per_connection,+ Some(MAX_IN_FLIGHT_PER_CONNECTION)+ );+ assert_eq!(+ kafka.kafka_producer_topic_metadata_refresh_interval_ms,+ Some(METADATA_REFRESH_INTERVAL_MS)+ );+ assert_eq!(kafka.kafka_producer_enable_idempotence, Some(false));+ }++ #[test]+ fn the_environment_overrides_compression_and_linger() {+ let config = Config {+ kafka_compression_codec: "none".to_string(),+ kafka_producer_linger_ms: 5,+ ..config()+ };++ let kafka = config.kafka_config();++ assert_eq!(kafka.kafka_compression_codec, "none");+ assert_eq!(kafka.kafka_producer_linger_ms, 5);+ }}diff --git a/rust/usage-ingestion/src/main.rs b/rust/usage-ingestion/src/main.rsindex bf56372b9a62..16040b8c13f9 100644--- a/rust/usage-ingestion/src/main.rs+++ b/rust/usage-ingestion/src/main.rs@@ -4,7 +4,6 @@ use std::time::Duration;use axum::{routing::get, Router};use common_database::{get_pool_with_config, PoolConfig};use common_grpc::GrpcMetricsLayer;-use common_kafka::config::KafkaConfig;use common_kafka::kafka_producer::create_kafka_producer;use envconfig::Envconfig;use health::HealthRegistry;@@ -65,12 +64,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {)?;let resolver = Arc::new(PostgresOrganizationResolver::new(database));- let kafka_config = KafkaConfig {- kafka_hosts: config.kafka_hosts,- kafka_tls: config.kafka_tls,- kafka_client_id: "usage-ingestion".to_string(),- ..Default::default()- };+ let kafka_config = config.kafka_config();let health = Arc::new(HealthRegistry::new("usage-ingestion"));let producer_liveness = health.register("kafka_producer".to_string(), Duration::from_secs(30))