Business Challenge
Yesterday's post was the allocation mechanism. This one is the consequence: how many VMs a gateway actually serves, and what happens at the edge of that number. The arithmetic is simple. The failure is not, because it does not announce itself as a failure.
This is port exhaustion, and the reason it survives a full round of health checks is that nothing is unhealthy. If your gateway runs out of NAT IP addresses, Public NAT drops packets. The troubleshooting page is more precise about the mechanism — the loss happens because there are not enough available NAT source IP address and source port tuples for the VM to use at the time of the packet loss (port exhaustion), and a five-tuple (NAT source IP address, source port, and destination 3-tuple) cannot be reused within the TCP TIME_WAIT timeout.
Correct approachGo straight to the reason label rather than to the gateway's status: if there aren't enough available NAT tuples, the dropped_sent_packets_count reason is OUT_OF_RESOURCES. That counter is the diagnosis. The gateway will not report a problem, because from its point of view there isn't one — it was asked for a tuple it did not have, and it declined.
Two separate factors bite, and both halve what you expected. Google Cloud allocates double the number of required ports per VM for reliability, and four addresses in the range are never usable. Google's own worked example is the fastest way to see the combined effect: on a minimum-size subnet, if you set the minimum number of ports per VM instance to 1024, the gateway can provide NAT services for up to 126 VMs.
Correct approach
Put both the × 2 and the four unusable addresses in the formula before you choose a prefix, and remember that the minimum subnet size that you can create is eight IPv4 addresses, which is a subnet mask of /29. A /29 therefore yields four usable NAT addresses, not eight, and each of those is divided by twice your configured minimum.
Expected, and it is the trade the formula makes explicit. With a single manually assigned address and a minimum of 64 ports, the gateway can provide NAT services for up to 1,008 VMs. Raise the minimum and that collapses: if you set the minimum number of ports per VM to 4,096, each NAT IP address can support 15 VMs.
Correct approachFollow the documented order of operations, which puts demand reduction ahead of supply increase: before you increase the number of ports per VM, consider other strategies for reducing port usage. If you must raise it, add addresses in the same change — when using manual NAT IP address assignment, you must calculate the number of regional external IP addresses that you need for the Public NAT gateway.
Architecture
A floor division, three ceilings that are not the port count, and a failure mode with no error.
The capacity formula
Capacity is one division, and the instruction that goes with it matters: round down the result of any division operation to the closest integer. Addresses multiplied by 64,512 usable ports, divided by the ports allocated per VM, floored.
| Configuration | VMs served | What moved |
|---|---|---|
| 1 address, 64 ports per VM | 1,008 | baseline |
| 2 addresses, 64 ports per VM | 2,016 | addresses doubled, capacity doubled |
| 1 address, 4,096 ports per VM | 15 | the divisor dominates |
| Private NAT /29, 64 ports per VM | 2,016 | 4 usable addresses, divided by 64 × 2 |
| Private NAT /29, 1,024 ports per VM | 126 | the doubling, made visible |
| Private NAT /28, 64 ports per VM | 6,048 | one prefix bit, a factor of 3 |
That last row is worth a second look, because it is not the factor of two a prefix bit normally buys. Going from a /29 to a /28 takes the gateway from 2,016 to 6,048 VMs — a factor of 3, because the usable count is 2^(32-prefix) - 4 rather than 2^(32-prefix), so subtracting the four unusable addresses hurts small subnets disproportionately. At the smallest sizes, one extra bit is worth more than doubling.
Three ceilings, and none of them is the port count
The formula suggests you can buy your way out with addresses. There are limits on that, and they are not where people look.
Addresses per gateway. NAT IP addresses per gateway 300 manual addresses 2,500 auto-allocated addresses. The assignment method you chose for predictability — manual, so you can hand a fixed set of addresses to a partner's allowlist — costs you most of the headroom. At 300 manual addresses the absolute port ceiling of one gateway is 19,353,600, and no configuration change raises it.
Gateways. NAT gateways 50 per Cloud Router, and each network supports up to 5 Cloud Router instances per region, so you can have up to 250 Cloud NAT gateways per region per Virtual Private Cloud (VPC) network.
Subnet ranges. Subnet ranges 50 per gateway, when you enumerate ranges rather than selecting all of them.
And the sentence that stops the obvious workaround from being a workaround: NAT IP addresses can't be shared between NAT gateways. Splitting a busy subnet across two gateways gives you two independent pools, not one larger one, and a VM is served by exactly one of them. That is a partitioning decision about which workloads share a budget — not extra capacity for the same workload.
How exhaustion presents
There are two signals and they answer different questions. The per-VM counter tells you what happened: dropped_sent_packets_count, with a reason label where possible values are OUT_OF_RESOURCES, ENDPOINT_INDEPENDENCE_CONFLICT. Two different faults, identical symptoms, one metric — so reading the counter without the label tells you a packet was dropped and nothing more.
The gateway-level signal is coarser than it looks. nat/nat_allocation_failed indicates if there is a failure in allocating NAT IPs to any VM in the NAT gateway. A boolean, for the whole gateway, about any VM. Useful as an alert; useless as a diagnosis.
Dynamic allocation adds a failure mode of its own that is not exhaustion at all, and it is easy to mistake for one. Dynamic port allocation detects when a VM is close to being out of ports, and doubles the number of ports that are allocated to the VM. But: this helps ensure that ports aren't wasted, but can result in dropped packets while the number of allocated ports is increasing. So a fleet with plenty of spare capacity still drops packets during a ramp, and the remedies are about pacing rather than capacity — if you can ramp up connections more slowly, Cloud NAT has more time to allocate more ports, or you can configure the VMs with a larger value for tcp_syn_retries, which gives the system more time to establish the connection and increases the chances for the connection to succeed. That second one is not free: increasing the value for tcp_syn_retries can lead to latency because the system keeps dropping packets until the connection succeeds after multiple retries.
Why This Architecture Holds Up
Three independent facts about Cloud NAT's observability compound into something worth planning around, and none of them is hidden — they are just in three different pages.
The logs thin out under load. Cloud NAT does not log every single event. Specifically: during periods of heavy egress traffic, NAT logging is throttled, proportional to the machine type of the VM. And then the part that removes your ability to compensate: translation or error logs might be dropped, and it is not possible to determine what is omitted during throttling.
Heavy egress traffic is not an unrelated condition. It is the condition under which ports run out. So the sampling rate of your evidence falls as the incident intensifies, and you cannot correct for it, because the quantity omitted is unknowable. A drop count read from logs during an incident is a lower bound of unknown tightness.
The metrics are minutes old. Every per-VM NAT metric is sampled every 60 seconds. After sampling, data is not visible for up to 180 seconds — and for the gateway-level drop count, sampled every 60 seconds. After sampling, data is not visible for up to 225 seconds. Four minutes of latency on a problem that can appear and resolve inside one.
And the metric named for the thing you want does not measure it. This is the one I would not have believed without reading both pages. The tuning guide tells you to size from port usage: you can get this information by using the compute.googleapis.com/nat/port_usage metric. The metrics reference defines that metric as nat/port_usage GA (project) Port usage GAUGE, INT64, {port} gce_instance Maximum number of connections from a VM to a single internet endpoint (IP:port).
So "Port usage" is not ports consumed. It is peak concurrency against the single busiest endpoint — which is exactly the quantity #101 said the allocation actually caps, and therefore the right number to size against. The name is misleading and the metric is correct. The one that reports consumption is a different metric: nat/allocated_ports GA (project) Allocated ports GAUGE, INT64, {port} gce_instance Number of ports allocated to a VM by the NAT gateway.
Compare two metrics, not one. allocated_ports is what the VM has. port_usage is the peak it needs against its busiest single endpoint. Sizing is making the first exceed the second, with headroom.
Which is precisely what the tuning guide prescribes, once you know what the numbers mean: review your maximum port usage for the past 30 days, or for another period that you think is representative for your Cloud NAT gateway, then — if you are using static port allocation, configure the number of ports per VM so that the minimum is equal to your current peak port usage, and if you are using dynamic port allocation, configure the number of ports per VM so that the minimum is lower than the peak port usage and the maximum is higher than the peak port usage.
Note that static sizing is prescribed as equal to peak, with no margin. Given yesterday's finding that reductions break every established connection while increases are free, erring above that is the cheap direction to be wrong in.
The levers, and the floors underneath them
Two families of knob. Ports: MIN_PORTS must be a power of 2, and can be between 32 and 32768, while MAX_PORTS must be a power of 2, and can be between 64 and 65536. MAX_PORTS must be greater than MIN_PORTS. The default is 65536.
Timeouts are the other half, and they work by changing how fast ports come back: you can modify the default timeout values to decrease or increase the rate at which ports are reused. The idle timeouts are generous by default — TCP established connections hold a mapping for 1200 seconds (20 minutes) — so a fleet with many long-lived idle connections is holding ports it is not using.
But there are floors, and they mean aggressive tuning does less than the configured number suggests. Timeout processing has a variance of up to five seconds, meaning the actual expiration time might occur up to five seconds earlier or later than the configured timeout value. For TIME_WAIT specifically, to avoid dropped packets, set this timeout to at least 15 seconds. And regardless of what you set: Cloud NAT might require up to an additional 30 seconds before a Cloud NAT source IP address and source port tuple can be used to process a new connection.
Add those together and a configured TIME_WAIT below roughly three quarters of a minute buys very little real reuse, because the service reserves the right to hold the tuple for up to 30 seconds more anyway.
#101 noted that the TIME_WAIT default is either 30 or 120 seconds depending on when the gateway was created. The tuning page gives the cutoff: for gateways that are created after September 30, 2026, the default value is 30 seconds.
So two gateways with identical configuration can reuse ports at four times the rate of each other, decided by nothing in the configuration. Do not infer it — read it: run the gcloud compute routers nats describe command and locate the effectiveTcpTimeWaitTimeoutSec field in the output. An older gateway holding closed tuples for 120 seconds has materially less effective capacity than the formula suggests, and the formula has no term for it.
When Cloud NAT is the wrong answer
Two cases where the service is documented as unsuitable rather than merely tight. For anything depending on wide source port entropy: some servers, such as legacy DNS servers, require UDP port randomization across 64,000 ports for enhanced security. For these servers, assign an external IP address instead of using Cloud NAT. And for protocols that embed addresses in their payload: Cloud NAT doesn't provide application-level gateway (ALG) capabilities — so FTP in active mode and SIP will not be rewritten, and no amount of port headroom changes that.
Key Architecture Decisions
| Situation | Decision | Because |
|---|---|---|
| Intermittent failures, everything healthy | Read the drop reason label first | Exhaustion is a dropped packet, not an error. |
| Drops observed with no reason label | Get the label before acting | OUT_OF_RESOURCES and an EIM conflict look identical. |
| Sizing from the console | Compare allocated_ports against port_usage | One is what you have, the other is what you need. |
| Reading "Port usage" as ports consumed | It is peak connections to one endpoint | The metric's name does not describe its definition. |
| Static allocation sizing | Minimum equal to peak, then round up | Increases are free; reductions break every connection. |
| Dynamic allocation sizing | Minimum below peak, maximum above | That is the documented configuration. |
| Short of ports | Reduce demand, then raise the minimum, then add addresses | Raising the minimum shrinks VMs per address. |
| Needing a fixed allowlist of egress addresses | Manual assignment, and accept 300 as the ceiling | Auto-allocation reaches 2,500 but is unpredictable. |
| Splitting a workload across two gateways for capacity | It partitions, it does not aggregate | NAT IP addresses can't be shared between gateways. |
| Sizing a Private NAT subnet | Use 2^(32−prefix) − 4, and divide by twice the minimum | Four addresses are unusable and allocation is doubled. |
| A Private NAT subnet at the minimum size | Consider one bit wider | /29 to /28 is a factor of 3, not 2, at this size. |
| Packet drops during a traffic ramp on dynamic | Pace the ramp or raise the minimum | Drops happen while the allocation is growing. |
| Considering a larger tcp_syn_retries | Accept the latency it adds | The system keeps dropping until a retry succeeds. |
| Tuning TIME_WAIT very low | Do not go below about 45 seconds in effect | 15s floor, ±5s variance, plus up to 30s regardless. |
| Comparing two gateways' behaviour | Check effectiveTcpTimeWaitTimeoutSec on both | The default depends on the creation date. |
| Counting drops from logs during an incident | Treat the number as a lower bound | Logging is throttled and the omission is unknowable. |
| Workload needing wide UDP source entropy or ALG | Use an external IP address instead | Both are documented as outside Cloud NAT's scope. |
The one to look at today
Put nat/allocated_ports and nat/port_usage on one chart per gateway, grouped by instance, maximum aligner, over 30 days. Where the second approaches the first, you are near exhaustion for that VM's busiest destination regardless of how much total headroom the gateway shows. Then run gcloud compute routers nats describe and check effectiveTcpTimeWaitTimeoutSec — if it reads 120 on a gateway you have been sizing with the formula, your real capacity has been lower than the arithmetic all along.
Closing Thought
The thing that makes this failure expensive is not the arithmetic, which is a floor division anyone can do. It is that every instrument you would reach for is compromised in a different way at the moment you reach for it. The logs are being thinned by the same traffic that caused the problem, and by an unknown amount. The metrics describe a state three or four minutes old. The gateway-level alert is a boolean about any VM. And the one metric whose name promises the answer is measuring something else — correctly, usefully, but not what it says.
None of that is a defect. Each piece is documented, and the port_usage definition is arguably better than its name, because it measures the quantity that actually binds. But it does mean the sizing work has to be done in advance, from the formula and from thirty days of history, rather than discovered during an incident. By the time you are looking, the instruments have degraded in step with the problem.
#103 opens the load balancing block with the decision tree — which of Google Cloud's load balancers you are actually allowed to use, decided by traffic type, scope and where the clients are, before any feature comparison begins.
Comments