The symptom and its nearby causes#
The pattern is specific: errors such as ECONNRESET, "connection reset by peer", a broken pipe, or a driver's "connection closed" appear on the first query or request after a gap, while busy hours run clean. Retries succeed because they open a fresh connection. Nights, weekends and low-traffic replicas suffer most.
Three causes produce the same error:
- A middlebox idle timeout. A NAT gateway, load balancer or connection-tracking table drops an idle flow, and the next packet on it is answered with a reset or vanishes.
- The server closed the connection. The database or HTTP server has its own idle timeout and closed first. With HTTP that is a keep-alive race. HTTP keep-alive reuse race
- The server went away. A restart, failover or deploy closed every connection at once. Those errors cluster at one moment across all clients, not after each connection's own idle gap.
Why the middlebox forgets#
Address translation and stateful load balancing need a table entry per flow, and every provider expires idle entries. The numbers differ, so check each hop between the client and the dependency.
| Hop | Idle timeout | What the client sees after it |
|---|---|---|
| AWS NAT gateway | 350 seconds, fixed | A reset when it continues the connection |
| AWS Network Load Balancer, TCP listener | 350 seconds by default, 60 to 6000 configurable | A reset when it sends data |
| AWS NLB, TLS listener | 350 seconds, fixed | A keepalive from either side makes the NLB probe both |
| Google Cloud NAT, established TCP | 1200 seconds by default | The mapping is removed |
On AWS, a NAT gateway connection idle for 350 seconds or more times out, and the gateway then returns a reset (not a FIN) to the resource that tries to continue it. Its IdleTimeoutCount CloudWatch metric counts these. NAT gateway troubleshooting · NAT gateway metrics
A Network Load Balancer stops tracking a TCP flow after its idle timeout, and a client or target that sends data afterwards receives a reset. The TCP listener default is 350 seconds and can be set between 60 and 6000 seconds through the tcp.idle_timeout.seconds listener attribute; TLS listeners keep 350 seconds. NLB connection idle timeout · describe-listener-attributes
Security groups track connections too. On Nitro v6 instance types the default idle timeout for established TCP connections is 350 seconds; other instance types default to 432,000 seconds (five days). Connection tracking timeouts
Google Cloud NAT removes a mapping after the TCP established connection idle timeout, 1200 seconds unless you set --tcp-established-idle-timeout. Cloud NAT timeouts
Observe it without changing anything#
Start with timing. For each failing request, find when that connection last carried traffic. Many pools log connection age or idle time at debug level; if yours does not, compare the error times with traffic gaps per instance. A cluster of failures just past 350 seconds of idle time on AWS, or just past 1200 seconds behind Cloud NAT, is the signature.
Then read the hop's timeout and counters. describe-listener-attributes · get-metric-statistics
aws elbv2 describe-listener-attributes \
--profile PROFILE --region REGION --listener-arn LISTENER_ARN --no-cli-pager
aws cloudwatch get-metric-statistics \
--profile PROFILE --region REGION --namespace AWS/NATGateway \
--metric-name IdleTimeoutCount --dimensions Name=NatGatewayId,Value=NAT_GATEWAY_ID \
--start-time START --end-time END --period 300 --statistics Sum --no-cli-pager
gcloud compute routers nats describe NAT_NAME \
--router ROUTER --region REGION --project PROJECT \
--format 'value(tcpEstablishedIdleTimeoutSec)'
An empty value for tcpEstablishedIdleTimeoutSec means the default applies. routers resource · gcloud compute routers nats describe
On the client host, check whether the pool's sockets use TCP keepalive at all. ss -o prints a keepalive timer on sockets that have one. ss(8)
ss -tno state established 'dst 10.0.12.34'
sysctl net.ipv4.tcp_keepalive_time
Linux sends keepalive probes only on sockets with SO_KEEPALIVE set, and only after tcp_keepalive_time of idleness, 7200 seconds by default. Turning keepalive on with the default idle time does nothing for a 350-second timeout; the socket needs TCP_KEEPIDLE below it. tcp(7)
Decision table#
| Observation | Points to |
|---|---|
| Failures follow idle gaps just above 350 s, path through a NAT gateway or NLB | AWS middlebox idle timeout |
| Failures follow idle gaps above 1200 s, egress through Cloud NAT | Cloud NAT mapping removed |
IdleTimeoutCount rises on the NAT gateway in the same windows | NAT gateway idle timeout |
| Failures follow gaps that match the server's own idle setting | The server closed first |
| Every client fails at one moment, regardless of idle time | Server restart or failover |
Keep connections younger than the timeout#
Two settings work, and they solve different halves. A pool idle timeout or maximum lifetime below the middlebox's limit retires connections before they go stale. HikariCP's documentation says maxLifetime should be several seconds shorter than any database or infrastructure-imposed connection time limit; Go's database/sql offers SetConnMaxIdleTime and SetConnMaxLifetime. TCP keepalive below the limit keeps the middlebox's entry fresh: AWS suggests keepalive under 350 seconds for NAT gateways, and an NLB resets its idle timer on keepalive packets. For PostgreSQL clients, libpq's keepalives_idle sets the idle time per connection. HikariCP settings · database/sql · libpq keepalives
A worked example with two outcomes#
This example is illustrative. The fictional payments service runs in private subnets and calls a card processor at api.processor.example.com through a NAT gateway, with an HTTP client pool that keeps idle connections for 10 minutes. Every morning, the first few calls after 06:00 UTC fail with ECONNRESET, then everything works.
In the first outcome, the failing connections were idle between 6 and 9 minutes, all above 350 seconds. IdleTimeoutCount on the NAT gateway shows a burst overnight. The NAT gateway expired the flows; the processor never saw the requests. Dropping the pool's idle timeout to 300 seconds removes the morning errors.
In the second outcome, the same pool talks to the processor over a private link with no NAT gateway or NLB in the path. The failing connections were idle for just over 120 seconds, which matches the processor's documented server-side idle close. The server closed first, so the client needs an idle timeout below 120 seconds and a retry for idempotent requests.
Limits#
Pool logs rarely record a connection's idle time, so you may have to infer it from traffic gaps. A middlebox that removes state without sending a reset leaves the client writing into silence until its read timeout or TCP's retransmission limit fires, which looks like a slow dependency more than a reset. NAT metrics are per gateway, not per connection. These checks do not cover database-side idle limits; read the dependency's own settings for those. More runtime dependency failures and how to separate them are on the hub page.
Questions#
Why does the first request in the morning fail and the retry work?
The pooled connection sat idle past a middlebox's idle timeout overnight. The first write on it gets a reset; the retry opens a fresh connection that the middlebox tracks again.
Does enabling TCP keepalive fix NAT gateway resets?
Only if the keepalive idle time is below 350 seconds. The Linux default waits 7200 seconds before the first probe, which is far too late.
Is the Cloud NAT idle timeout the same as AWS?
No. Cloud NAT's established TCP idle timeout defaults to 1200 seconds and is configurable per NAT; the AWS NAT gateway's 350 seconds cannot be changed.