DEV Community

upordown.online
upordown.online

Posted on Edited on

How to fix 'nf_conntrack: table full, dropping packet': the Linux connection-tracking limit that makes a site look randomly down

Uptime monitoring from outside your own infrastructure that alerts you the minute your site goes down — upordown.online

A site that is "down for some people, fine for others, and fine if you retry" is not always a bad ISP or a dead backend. On a busy Linux host — especially a Kubernetes node, a Docker host, or anything doing NAT — it is often the kernel's connection-tracking table filling up. New packets that would create a conntrack entry get dropped. Existing connections may still work. Your homepage check can stay green through the worst of it.

The kernel is unusually honest about this. It writes a line to the log, and two sysctl values tell you how close you are. This is how to read them, fix the cause, and only then decide whether to raise the limit.

Confirm it is conntrack, not a flaky origin

On the host that terminates or NATs the traffic (the node, the gateway, the box running iptables or nftables, not necessarily the app container):

# current entries vs the cap
sysctl net.netfilter.nf_conntrack_count net.netfilter.nf_conntrack_max

# the smoking gun, if it is still in the ring buffer
sudo dmesg -T | grep -i conntrack | tail
# or, if you have journald
sudo journalctl -k -S "1 hour ago" | grep -i conntrack
Enter fullscreen mode Exit fullscreen mode

The line you want looks like:

nf_conntrack: table full, dropping packet
Enter fullscreen mode Exit fullscreen mode

If nf_conntrack_count is sitting at or very near nf_conntrack_max, you have the cause even if the log line has already rotated. If the count is a small fraction of the max, this is not your outage — keep looking (file descriptors, upstream timeouts, a full disk, a dead worker).

On many hosts the older name net.ipv4.netfilter.ip_conntrack_* is a symlink or is simply absent. Prefer the nf_conntrack_* keys. If sysctl says the key is unknown, the module is not loaded and you are on the wrong machine.

A quick live count of what is in the table helps too:

# if the conntrack tool is installed (conntrack-tools)
sudo conntrack -C
sudo conntrack -S
# a rough split by state, no extra packages
sudo awk '{print $4}' /proc/net/nf_conntrack | sort | uniq -c | sort -nr | head
Enter fullscreen mode Exit fullscreen mode

A pile of TIME_WAIT or ESTABLISHED entries is the usual picture. A pile of SYN_SENT or SYN_RECV is a different problem (a scan, a flood, or a backend that is not completing handshakes).

Why a web server hits this

Conntrack is not only for "a firewall appliance". Docker, Kubernetes (kube-proxy in iptables or nftables mode), libvirt, UFW, firewalld, and any iptables -t nat rule will cause the kernel to track connections. Each tracked flow uses an entry until it times out.

The default nf_conntrack_max is often 65,536, or a value derived from RAM at boot. That sounds large until you have a node handling thousands of short HTTP connections a second plus NAT, nf_conntrack_tcp_timeout_established left at its default of 5 days, TIME_WAIT at 120 seconds with clients that open a new connection for every request, or a mesh that NATs every outbound call.

The site then fails in a way that looks random: packets that need a new entry are dropped, retries often land on a reused flow, and a one-off curl from your laptop succeeds.

What to change, in order

1. Free space in the table, right now, if you are in the incident. Restarting a noisy workload, draining a node, or briefly lowering timeouts will drop count faster than a reboot. A reboot also clears the table, and also drops every connection. Prefer not to.

2. Shorten timeouts that are keeping dead flows around. This is the fix that usually matters more than raising the cap:

# how long an idle established TCP flow is remembered (default is often 432000s = 5 days)
sysctl net.netfilter.nf_conntrack_tcp_timeout_established
# 1 hour is plenty for most HTTP/TCP services; tune to your longest real idle need
sudo sysctl -w net.netfilter.nf_conntrack_tcp_timeout_established=3600

# TIME_WAIT entries (default 120)
sudo sysctl -w net.netfilter.nf_conntrack_tcp_timeout_time_wait=60
Enter fullscreen mode Exit fullscreen mode

Make those persist in /etc/sysctl.d/99-conntrack.conf. Do not cut timeout_established to a few minutes if you have legitimate idle connections (a database pool, a WebSocket, a long poll) that still need a reply after silence. Look at your real idle times first.

3. Raise nf_conntrack_max only after you know why you hit it. Each entry costs memory (on the order of a few hundred bytes). Doubling the cap on a 64k default is usually fine; multiplying it by 20 because a retry loop is leaking flows is how you hide a bug and then run out of RAM.

# example: 256k entries, and buckets roughly max/4
sudo sysctl -w net.netfilter.nf_conntrack_max=262144
sudo sysctl -w net.netfilter.nf_conntrack_buckets=65536
Enter fullscreen mode Exit fullscreen mode

If you change max at runtime, also set buckets so lookups stay cheap. Put both in sysctl.d; some Kubernetes node images or kube-proxy flags will overwrite them at start.

4. Find the source of the flows. Which namespace, which pod, which NAT rule, which client is creating them? conntrack -L is verbose but filterable. In Kubernetes, a Deployment with a missing timeout, a health check that opens a new connection every second across thousands of pods, or a service that used to be a ClusterIP and is now a NAT-heavy path will show up as one workload owning most of the table.

5. Reduce needless tracking where you can. Not every packet has to be tracked. A NOTRACK/notrack rule can exempt intra-node health checks; do this carefully, because it is easy to break NAT or a stateful allow rule.

A conntrack drop rarely looks like a clean 502 in every region at once; it looks like a flaky site that your next retry happens to catch. upordown.online checks the public URL from outside, on an interval, so a burst of dropped packets becomes a run of failures with timestamps, not a single "works for me" refresh.

What to watch so it does not surprise you again

Alert when nf_conntrack_count / nf_conntrack_max is over 70 or 80 percent, not when it is already 100 and packets are on the floor. Export both values from node exporters (node_nf_conntrack_entries and node_nf_conntrack_entries_limit on many setups). Treat the ratio as the early signal.

And put the public path on a monitor that is not running on the same node. A checker that shares the table will fail and recover in the same pattern as your users, or, worse, will be one of the flows filling it.

If you changed timeouts or the cap today, write down why. The next person who sees a "random" outage should find that note before they double the limit again.

Related reading

Have you hit a table-full drop in production? Was it timeouts, a leaked client, or a Kubernetes node that had simply grown past the default cap?

Top comments (1)

Collapse
 
c-wind profile image
c-wind •

The check I wish I'd had earlier is the one you lead with: if nf_conntrack_count is already sitting on nf_conntrack_max, that is the outage even when the "table full, dropping packet" line has rotated out of dmesg. I've watched people chase app 502s for a long time on a node that was simply pegged.

The order of the timeout pass also matches what I've seen. Before raising the cap, the awk split on /proc/net/nf_conntrack tells you which knob actually matters. A pile of TIME_WAIT is usually a client or a health check opening a new TCP connection per request — shortening nf_conntrack_tcp_timeout_time_wait helps, but so does making those callers reuse connections. A pile of ESTABLISHED with the default 5-day timeout_established is a different leak, and cutting that to an hour is reasonable on a front-end node as long as you have looked at real idle times first, the way you warn.

One operational detail worth repeating from your list: if you bump nf_conntrack_max at runtime and forget nf_conntrack_buckets, the change looks like it worked until lookup cost shows up later. Some node images and kube-proxy flags will also overwrite both on start, so a sysctl.d file is not enough unless you check the live values after the node has finished booting.