Networking
TCP Fast Open on Linux: How It Works and When to Enable It
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيEvery new TCP connection starts with a three-way handshake: the client sends SYN, the server answers SYN-ACK, and only after the client's ACK can application data flow. On a link with a 100 ms round trip, that is 100 ms of waiting before the first byte of a request even leaves the client. TCP Fast Open (TFO), defined in RFC 7413, lets a returning client put data inside the SYN itself, so the server can start processing the request one round trip earlier. This guide explains how TFO works, how to turn it on for a Linux VPS, and where it does and does not help.
How TCP Fast Open works
TFO relies on a cookie that the server issues and the client stores.
- First connection. The client sends a SYN with an empty TFO option, asking for a cookie. The server returns a cookie in the SYN-ACK. This connection completes a normal handshake, so there is no gain yet.
- Later connections. The client sends a SYN that carries both the cookie and the first chunk of application data, for example an HTTP request. If the cookie is valid, the server passes that data to the application immediately, before the handshake finishes.
- Fallback. If the cookie is missing or invalid, the server ignores the data in the SYN and the client resends it after a normal handshake. Nothing breaks; you simply lose the speedup.
The cookie is derived from the client IP address and a secret key on the server, which stops attackers from spoofing source addresses to flood the server with data-bearing SYNs.
Enabling TFO in the kernel
Linux controls TFO with a bitmask in net.ipv4.tcp_fastopen (it applies to IPv6 as well):
| Value | Meaning |
|---|---|
| 0 | Disabled |
| 1 | Client side enabled (the usual default) |
| 2 | Server side enabled |
| 3 | Client and server enabled |
Check the current value and enable both sides:
sysctl net.ipv4.tcp_fastopen
sudo sysctl -w net.ipv4.tcp_fastopen=3To keep the setting after a reboot, create a drop-in file:
echo "net.ipv4.tcp_fastopen = 3" | sudo tee /etc/sysctl.d/60-tcp-fastopen.conf
sudo sysctl --systemThe kernel flag alone is not enough on the server side. Each listening application must also request TFO on its socket, using the TCP_FASTOPEN socket option with a queue length for pending TFO requests.
Enabling TFO in nginx
nginx exposes this through the fastopen parameter of the listen directive. The number is the maximum queue of connections that have not yet completed the handshake:
server {
listen 443 ssl fastopen=256;
listen [::]:443 ssl fastopen=256;
server_name example.com;
# ...
}Test the configuration and reload:
sudo nginx -t && sudo systemctl reload nginxNote that only one listen line per address and port may carry socket options like fastopen, so if several server blocks share port 443, put it on one of them, typically the default server.
Testing and monitoring
curl can act as a TFO client:
curl --tcp-fastopen -so /dev/null -w "%{time_connect} %{time_starttransfer}\n" https://example.com/Run it a few times; the first request fetches the cookie and later ones can use it. On the server, kernel counters show whether TFO is really happening:
nstat -az | grep -i fastopenLook at TcpExtTCPFastOpenPassive (accepted TFO connections) and TcpExtTCPFastOpenCookieReqd (cookie requests). If passive stays at zero while clients claim to use TFO, something on the path is interfering. You can also capture traffic with tcpdump -ni eth0 'tcp[tcpflags] & tcp-syn != 0' and check for SYN packets with a payload.
Limits and risks
TFO is useful, but it is not free of trade-offs:
- Replay of SYN data. A SYN can be duplicated in the network, so the data inside it may reach the application twice. Only idempotent requests are safe, such as an HTTP GET. Applications that perform state changes on the first bytes of a connection must account for this.
- Middleboxes. Some firewalls and NAT devices drop SYN packets that carry data or unknown options. Linux includes blackhole detection (
net.ipv4.tcp_fastopen_blackhole_timeout_sec) that disables active TFO after repeated failures. - Client support is limited. Major browsers do not use TFO for normal web browsing, so for public websites the real gain is often small. TFO shines more for your own clients: internal services, API clients, proxies, or tools you control between your servers.
- TLS has its own shortcut. TLS 1.3 session resumption with 0-RTT data addresses a similar delay at the encryption layer and carries the same replay concern. TFO and TLS 0-RTT can be combined, but each needs careful handling.
- Key rotation. The server secret lives in
net.ipv4.tcp_fastopen_key. Behind a load balancer, servers with different keys reject each other's cookies, which only costs speed, not correctness.
A quick checklist
- Set
net.ipv4.tcp_fastopen = 3in/etc/sysctl.d/. - Add
fastopen=Nto onelistenline per address and port in nginx, or setTCP_FASTOPENin your own server code. - Make sure the first request on a connection is safe to replay.
- Confirm with
nstatthat passive TFO counters increase. - Pair it with other latency work, such as a modern congestion control described in our guide to TCP BBR.
If you want a clean environment to experiment with kernel network settings, a Linux VPS from our pricing page gives you full root access and a browser console in case a change locks you out.
FAQ
Does TCP Fast Open make every connection faster?
No. It only saves a round trip for clients that already hold a valid cookie from an earlier connection, and only when both sides and the network path support it. The first connection to a server always performs a normal handshake.
Is it safe to enable TCP Fast Open on a production web server?
Enabling it with nginx is generally low risk because clients that do not support TFO fall back silently. The main concern is replayed SYN data, so make sure your application does not perform non-idempotent actions based solely on data that arrived in the SYN.
Why do my nstat counters stay at zero after enabling it?
Usually because the clients are not using TFO: most browsers do not, and curl needs the --tcp-fastopen flag. Also check that the server socket actually requested TFO, for example that the nginx listen line includes fastopen, and that no firewall on the path strips the option.