Why APISIX Gateway 504s Clustered Around 15 Seconds: A SYN Timeout Investigation

Our production API gateway (API7 Enterprise, a commercial distribution of Apache APISIX) started returning intermittent 504s. The failed requests clustered around 15 seconds.

The plugin timeout was configured at 600 seconds, so where did the 15 seconds come from? The kernel exhausted its SYN retransmission budget without seeing a SYN-ACK. The tcp_syn_retries=3 setting explained the timing; why the SYN-ACK never arrived remained an open question.

Following the timeout through the code

First step: verify that the 600-second timeout actually reaches the bottom of the stack. Tracing the call chain from the plugin configuration, through the Lua layer, through the FFI boundary, into the C module:

1
2
3
4
5
6
7
8
9
-- ai-transport/http.lua:145
httpc:set_timeout(timeout)

-- resty/ngx_http_ffi_client.lua
client.set_timeouts(self, 600000, 600000, 600000)

-- ngx_http_ffi_client_request.c:2238
op->connect_timeout = 600000
ngx_add_timer(c->write, op->connect_timeout)

The 600s value is passed down correctly, and the C module really does arm a 600s timer. So the timeout is not coming from the application. Something else owns the 15-second deadline.

Suspecting the kernel

15 seconds is a suspicious number. It smells like a kernel TCP timer rather than an application default. Checking the kernel parameters from the gateway pod:

1
2
sysctl net.ipv4.tcp_syn_retries
# output: net.ipv4.tcp_syn_retries = 3

With the exponential backoff observed in this environment, the total SYN timeout can be estimated as:

1
timeout = ((2 << boundary) - 1) * rto_base

With tcp_syn_retries=3 and a 1-second base RTO:

1
timeout = ((2 << 3) - 1) * 1 = 15 seconds

That matches the timing seen in monitoring. The retransmission timeline:

1
2
3
4
1st SYN:        sent t=0s,  times out t=1s   (cumulative 1s)
1st retransmit: sent t=1s, times out t=3s (cumulative 3s)
2nd retransmit: sent t=3s, times out t=7s (cumulative 7s)
3rd retransmit: sent t=7s, times out t=15s (cumulative 15s) <- kernel gives up

There is no fifth attempt. tcp_syn_retries=3 means at most 3 retransmissions, 4 attempts total, and then the kernel declares the connection dead.

Eliminating DNS

Could this be DNS resolution failing slowly? Looking at APISIX's DNS resolution code for the error message formats:

1
2
3
4
5
-- resolver.lua:83
log.error("failed to parse domain: ", host, ", error: ", err)

-- resty/ngx_http_ffi_client.lua
return nil, host .. " could not be resolved (" .. (err or "no address") .. ")"

Searching the gateway logs for these strings did turn up DNS errors:

1
2
3
2026/09/07 08:59:15 [warn] base.lua:255: failed to send request to LLM server:
connect: failed to parse domain: failed to query the DNS server:
dns server error: 2 server failure

But when we pulled the request_ids of those DNS errors from our log store and compared the two populations, a clear difference emerged:

DNS errors SYN timeouts
count 21 750
avg duration 2.25s 15.38s
status code 500 504

The DNS failures returned 500s and averaged 2.25 seconds. They were a separate group from the 504s clustered around 15 seconds.

Packet capture

Theory is nice; packets are better. Capturing traffic on the gateway node and filtering for handshakes:

1
2
3
4
5
6
# find SYN packets
tcpdump -r capture.pcap 'tcp[tcpflags] & (tcp-syn) != 0' -nn

# find SYN retransmissions (multiple SYNs from the same port)
tcpdump -r capture.pcap 'tcp[tcpflags] & (tcp-syn) != 0' -nn | \
awk '{print $3, $5}' | sort | uniq -c | sort -rn

Here is one captured retransmission sequence. The elapsed times are measured from the initial SYN:

1
2
3
4
5
192.0.2.55.18494 -> 198.51.100.249.443:
initial SYN: 15:27:20.096
retransmit 1: 15:27:21.117 (about 1.0s elapsed)
retransmit 2: 15:27:23.133 (about 3.0s elapsed)
retransmit 3: 15:27:27.229 (about 7.1s elapsed)

Successive sends were about 1, 2, and 4 seconds apart, consistent with the exponential backoff in this environment. With no response, the sequence predicts a further wait of about 8 seconds after the final send before the connection attempt ends.

Three layers of evidence

layer evidence conclusion
application these 504 requests averaged 15.38s requests failed after about 15s
code source trace: the 600s deadline reaches the C module the configured plugin deadline had not expired
network PCAP: successive SYNs about 1s, 2s, and 4s apart consistent with the observed SYN retransmission sequence

Together, the evidence shows where the timeout happened and explains the roughly 15-second wait. The capture did not establish why the gateway got no SYN-ACK.

The fix we did not ship

We decided against bumping tcp_syn_retries to 5. Under the same backoff schedule, and with no response, it would stretch the wait from about 15s to about 63s.

We asked the network team to investigate connectivity. Application-level retries or fallback, within the request's time budget, could also limit the impact of this kind of failure.

Takeaways

  • A cluster of timeouts around the same duration is a clue. Compare the timing against the application's configured deadline and the retransmission schedule observed in the capture.
  • Cross-check across layers. Request logs, source-level timeout tracing, and the packet capture together identified the timeout as a TCP connection failure. Finding why the connection could not be established required further network investigation.
  • Similar-looking errors can be unrelated. The DNS failures and the SYN timeouts appeared in the same gateway, in the same period, but had completely different duration profiles. Always compare distributions, not anecdotes.
  • Check what a retry setting changes. With no response, a higher retry count only extends the wait.

The gateway in this incident is API7 Enterprise, a commercial distribution of Apache APISIX. The fix discussed above was deliberately not applied; connectivity was escalated to the network team instead.

This article is the English edition of a post originally published in Chinese.