Skip to content

: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.

fix(usage-ingestion): make scale-ups take traffic (#92663)

Ben Whited3dccfa63ca0
diff --git a/rust/usage-ingestion/README.md b/rust/usage-ingestion/README.md
index ad6d8d4943f2..958321f31e5f 100644
--- a/rust/usage-ingestion/README.md
+++ b/rust/usage-ingestion/README.md
@@ -54,6 +54,21 @@ and the service takes its topic from `USAGE_INGESTION_TOPIC`.
The endpoint relies on an in-cluster network boundary while it is being validated.
Before it is exposed beyond trusted callers, it must authenticate the calling service.
+Every producer holds one long-lived HTTP/2 connection and never re-resolves the
+service address, so a new pod would take no traffic until a producer restarts.
+The server closes each connection after `USAGE_INGESTION_GRPC_MAX_CONNECTION_AGE_SECS`
+(60 by default) to force that re-resolution, which is what makes a scale-up reach
+the pods it adds.
+The personhog services carry the same setting under the same name, at 300 seconds.
+Without it, a scale-up leaves the new pods idle while the old ones throttle, and
+only replacing every pod redistributes the load.
+Set the variable to 0 to turn the behavior off, which is the escape hatch if the
+reconnects ever cost more than the imbalance they fix.
+A value between 1 and 9 is rejected at startup, because it would leave producers
+reconnecting instead of sending.
+This only spreads traffic if the Service is a ClusterIP; a headless Service hands
+the client one address it can reconnect to.
+
Partition count can be raised later without risk, which is not true of an
ordered stream.
ClickHouse deduplicates by record identity and picks a winner by
diff --git a/rust/usage-ingestion/src/config.rs b/rust/usage-ingestion/src/config.rs
index d3da52a5f266..92fa4e739574 100644
--- a/rust/usage-ingestion/src/config.rs
+++ b/rust/usage-ingestion/src/config.rs
@@ -1,3 +1,5 @@
+use std::time::Duration;
+
use common_kafka::config::KafkaConfig;
use envconfig::Envconfig;
@@ -40,6 +42,13 @@ pub struct Config {
default = "clickhouse_billing_usage_records"
)]
pub topic: String,
+ /// Maximum age of a gRPC connection in seconds before the server sends GOAWAY.
+ /// Producers reconnect transparently, which restaggers them across the pods.
+ /// 0 = disabled (connections live indefinitely).
+ /// Shorter than personhog's 300 because a producer holds one connection and this fleet
+ /// runs near its CPU request when the load lands unevenly.
+ #[envconfig(from = "USAGE_INGESTION_GRPC_MAX_CONNECTION_AGE_SECS", default = "60")]
+ pub grpc_max_connection_age_secs: u64,
}
impl Config {
@@ -47,9 +56,23 @@ impl Config {
if self.max_batch_size == 0 || self.max_batch_size > 5_000 {
return Err("USAGE_INGESTION_MAX_BATCH_SIZE must be between 1 and 5000".to_string());
}
+ // A few seconds would make every producer spend its time reconnecting.
+ if self.grpc_max_connection_age_secs > 0 && self.grpc_max_connection_age_secs < 10 {
+ return Err(
+ "USAGE_INGESTION_GRPC_MAX_CONNECTION_AGE_SECS must be 0 or at least 10".to_string(),
+ );
+ }
Ok(())
}
+ pub fn grpc_max_connection_age(&self) -> Option<Duration> {
+ if self.grpc_max_connection_age_secs == 0 {
+ None
+ } else {
+ Some(Duration::from_secs(self.grpc_max_connection_age_secs))
+ }
+ }
+
/// A setting dropped from here reverts to a `KafkaConfig` default instead of failing.
pub fn kafka_config(&self) -> KafkaConfig {
KafkaConfig {
@@ -90,6 +113,7 @@ mod tests {
kafka_producer_linger_ms: 100,
max_batch_size: 500,
topic: "clickhouse_billing_usage_records".to_string(),
+ grpc_max_connection_age_secs: 60,
}
}
diff --git a/rust/usage-ingestion/src/main.rs b/rust/usage-ingestion/src/main.rs
index 16040b8c13f9..ab49c975400a 100644
--- a/rust/usage-ingestion/src/main.rs
+++ b/rust/usage-ingestion/src/main.rs
@@ -116,7 +116,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing::info!(address = %config.grpc_address, "Starting usage-ingestion gRPC service");
// This listener is limited to trusted in-cluster callers. Add authenticated caller identity
// before exposing it beyond that boundary because records affect tenant billing.
- Server::builder()
+ // Producers pin one HTTP/2 connection for the life of the process, so a scale-up takes no
+ // traffic until connections churn. A periodic GOAWAY makes them re-resolve the service.
+ // ponytail: tonic 0.12 adds no jitter here. Move to client-side round-robin if the
+ // synchronized reconnect shows up as a latency sawtooth.
+ let mut builder = Server::builder();
+ if let Some(age) = config.grpc_max_connection_age() {
+ builder = builder.max_connection_age(age);
+ }
+ builder
.layer(GrpcMetricsLayer)
.add_service(UsageIngestionServer::new(service))
.serve(config.grpc_address.parse()?)