NAT Gateway Port Exhaustion Under Bursty Lambda Traffic
A burst of Lambda invocations exhausted NAT Gateway SNAT ports, causing connection failures to external APIs with confusing "connection refused" errors.
Overview
A tripled nightly batch pushed 800 Lambdas through one NAT Gateway to one external IP, ran it out of source ports, and cost 90 minutes of debugging the wrong system.
What happened
The pipeline ran Lambda functions in a VPC that called third-party APIs in nightly batches. It had been stable for months until a customer acquisition campaign changed the load overnight. The night it broke went like this.
- The campaign tripled the batch size overnight.
- The batch ran 800 concurrent Lambdas, each holding 3 persistent HTTPS connections to the external API.
- The source ports from the NAT Gateway to the API's IP ran out, a condition called SNAT port exhaustion.
- New connections were refused, which looked exactly like the API being down.
- The team spent 90 minutes debugging the external API.
- A network engineer finally spotted the error metric on the NAT Gateway.
Key concepts
- SNAT (Source NAT)
- Port exhaustion
- TIME_WAIT state
- VPC endpoint
Interview questions
- A Lambda function in a VPC is failing with connection errors to an external API at scale. How do you diagnose NAT Gateway port exhaustion?
Sign up free to read the full topic
More War Stories topics
- Connection Pool Exhaustion That Brought Down 12 Microservices
- DynamoDB Hot Partition That Throttled a Flash Sale
- Failover to a Lagging Replica Lost 4 Minutes of Orders
- Internal DNS Cache Poisoning That Redirected API Traffic
- Malicious npm Package Exfiltrated Environment Variables at Build Time
- N+1 Query Pattern That Collapsed Under Load
- Node.js Memory Leak That Required Daily Restarts
- Public S3 Bucket Exposed Customer PII for 8 Months
- Silent Data Corruption from an Off-By-One Migration
- Single-Region Dependency That Caused a Global Outage
- The Cascade That Took Down Three Services
- Third-Party API Outage That Was Mistaken for Own Code
Start free on Cariara · Coding problems · System design studio