Pumas, Routers & Keepalives—Oh my!
Elevated tail latency seen by some Puma apps after enabling Heroku Router 2.0 turned out to be a Puma bug, not a router problem. Under load with roughly 200ms of server-side work, keepalive connections pushed p95/p99 response times above 2 seconds. The cause was traced by reproducing it locally with hey and ab, which showed the slowdown occurs with any keepalive client hitting Puma directly. Tuning Go's http.Transport MaxIdleConnsPerHost reduced connection churn but did not fix latency. Puma 7.0.4 or later fixes the bug.
Related posts
- Learn How to Lower Heroku Dyno Latency through Persistent Connections (Keep-alive)
Heroku Engineering mediumAdeptHTTP keep-alive connections cut latency by reusing TCP connections, so repeat requests skip the handshake and TCP slow start and run at full bandwidth. HTTP/1.0 opened one connection per request; HTTP/1.1 decoupled requests from connections through pipelining and keep-alive. Heroku Router 2.0 now uses keep-alive to dynos by default. Ruby apps that disabled keep-alives over earlier latency spikes should upgrade to Puma 7.1.0+ and can check settings with PUMA_LOG_CONFIG and the http-disable-keepalive-to-dyno labs flag.
- Upgrade to Puma 7 and Unlock the Power of Fair Scheduled Keep-alive
Heroku Engineering mediumAdeptPuma 6 and earlier let a single keep-alive connection monopolize a thread for up to 10 requests via the "fast inline" loop originally built for HTTP pipelining, so under overload about one in ten requests waited roughly 10x longer while later keep-alive requests cut the line. Puma 7 fixes this by requeuing pipelined and keep-alive requests at the back of the queue and removing the wait_until_not_full block on the accept loop. To keep load balanced across workers, it extends wait_for_less_busy_worker with a proportional sleep, so busier workers wait longer before accepting. Capacity is unchanged, but overload delay is now spread fairly; upgrading to 7.1.0+ is recommended, especially for Heroku Router 2.0 users who disabled keep-alives.
- Tips & Tricks for Migrating to Router 2.0
Heroku Engineering mediumAdeptHeroku Router 2.0 is now generally available and will become the default, so apps should be validated on a staging copy first by enabling the http-routing-2-dot-0 feature and restarting dynos to drop legacy-router connections. You can confirm which router is in use through the Via response header (heroku-router vs. vegur) or through router log fields: protocol now reads http2.0/http1.1, and there are new tls and tls_version fields. HTTP/2 is on by default for custom domains with valid TLS. It terminates at the router, which forwards HTTP/1.1 to dynos. Keepalives to dynos are always on, which lowers latency but triggers a Puma connection-handling bug that inflates tail latency. Both HTTP/2 and keepalives can be disabled through labs flags.
- The Vanishing Thread and Postgresql TCP Connection Parameters
Instacart Tech mediumAdeptInstacart traced Puma threads that hung for about 15 minutes, ignoring application and database timeouts, to TCP retransmission storms between the app and an AWS NLB in front of PgBouncer. Linux's default tcp_retries2 of 15 with exponential backoff yields roughly 15-minute waits before the OS reports 'Connection timed out'. Setting TCP_USER_TIMEOUT and keepalives per connection through libpq cut failure detection to about 13 seconds. The post also serves as a reference for configuring these parameters in connection strings, Rails, PgBouncer, the Postgres server, logical replication subscriptions and FDW servers. It explains how tcp_user_timeout overrides syn retries and keepalive probes, and how to infer which timeout fired from the elapsed time.
- Battling database performance
incident.io mediumAdeptIntermittent API timeouts at incident.io over two weeks traced back to requests blocking on Go's database/sql client-side connection pool, with no single slow query or pool to blame. The team shipped 24 changes along the way. These included materialised views, new indexes, query rewrites, a one-second lock timeout on transactions, and moving Slack webhook handling onto PubSub. They also built per-operation tracking of connection-hold time using an ngrok/sqlmw middleware and context counters that avoid double-counting inside transactions. The root cause turned out to be an unnecessary transaction wrapped around every Slack modal submission. Many short transactions added together had exhausted the pools, and removing it ended the timeouts.