The Vanishing Thread and Postgresql TCP Connection Parameters
Instacart 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.
Related posts
- Pumas, Routers & Keepalives—Oh my!
Heroku Engineering mediumAdeptElevated 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.
- Production Postgres on Railway: From First Deploy to High Availability
Railway mediumAdeptRailway Postgres can grow from a single private-networked database to a production setup with volume backups, point-in-time recovery, PgBouncer connection pooling and Patroni-based high availability, demonstrated with an orders API that uses idempotency keys so retries stay safe. PITR restores into a separate service for validation, and the guide stresses measuring RPO/RTO end to end, including validation and reconnection time. Transaction pooling breaks session settings, LISTEN, advisory locks and named prepared statements, so migrations should use an unpooled URL. In one small planned switchover test (primary, two replicas, etcd, HAProxy), writes were interrupted for about 17 seconds. That is a single observation, not a guaranteed failover time.
- Incident Report: April 23rd, 2025
Railway mediumAdeptA Google Cloud SQL maintenance window took Railway's networking control plane Postgres read replica offline for about 10 minutes, destabilizing edge, private networking and TCP proxy traffic across all regions, worst in Asia. Control plane instances crashed on database connection errors because of improper error handling, and each restart evicted the in-memory routing cache, which could not be refetched while Postgres was down. Slow cache warming delayed recovery until 09:28 UTC. Planned fixes include regional control planes to limit blast radius, graceful handling of DB errors, a persistent routing cache as a fallback, faster startup, and Cloud SQL maintenance settings that keep primary and replica from being maintained at the same time.
- Using Neon’s Scale to Zero with Long-Running Applications
Neon mediumAdept
Neon's scale to zero suspends a Postgres compute after an idle window (only when no WAL senders are active and autovacuum isn't running), which severs connections held by long-running servers. A naive node-postgres Client crashes on the resulting unhandled error event. A client-side pg.Pool with an error handler tolerates the dropped connections and reconnects on demand, waking the database. In Apache Bench tests the pool beat per-request connections by more than 10x (about 46ms vs 678ms average latency, 370 vs 26 req/s), with the same pattern shown in a Fastify, Drizzle and HTMX example app.
- Clouds, caches and connection conundrums
incident.io mediumAdeptAfter migrating to Google Cloud, sporadic Postgres and memcached connection timeouts on GKE turned out to be node-wide, not process-local. Bursts of rapidly opened TCP connections, from concurrent cache sets, GCS URL generation and a runaway query fanning out external calls, spiked CPU in the Dataplane V2 anetd agent and caused dropped packets. Partial mitigations helped: static Postgres pools with a longer connection lifespan cut new connections from ~200/s to ~1/s, and raising memcache MaxIdleConns from 2 to 20 also reduced timeouts. The lasting fix is connection reuse plus limits on outbound network concurrency rather than disabling Dataplane V2. Network timeouts should be investigated at the node level, not just per pod.