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

Start free on Cariara · Coding problems · System design studio