Frontend System Design · Lesson 2 of 86 · 3 min
TCP and TLS Handshakes: Connection Setup Costs
This is about the round-trip latency you pay before your app's first byte of data can even be sent.
How a TCP connection is established
Before your browser can send an HTTP request, it needs a reliable connection to the server. TCP, the Transmission Control Protocol, provides this reliability. It uses a three-step process called the 3-way handshake.
- SYN: The client sends a "synchronize" packet to the server to initiate a connection.
- SYN-ACK: The server acknowledges the client's packet and sends its own synchronize packet back.
- ACK: The client acknowledges the server's packet. The connection is now open.
This exchange ensures both client and server are ready to communicate. The key cost is that it takes one full round-trip time (RTT) before any application data (like your GET request) can be sent.
In an interview, framing this as a "one round-trip tax" for reliability shows you understand the performance implications. It's the first component of Time To First Byte (TTFB).
How a secure connection is established
After the TCP connection is open, you need to secure it for HTTPS. This is done with a TLS (Transport Layer Security) handshake. Its job is to authenticate the server, agree on encryption algorithms, and generate a shared secret key.
A full TLS 1.2 handshake is a chatty process. The client says hello and lists the encryption methods (cipher suites) it supports. The server picks one, sends back its public certificate to prove its identity, and then they exchange key information to create a shared secret.
This negotiation adds two more round trips on top of the initial TCP handshake. Your first request to a new https:// domain now costs 1 RTT (TCP) + 2 RTTs (TLS 1.2) before the HTTP request is even sent.
The trap: Forgetting that TLS adds its own latency. Candidates often describe the TCP handshake cost but miss the much larger cost of the initial TLS negotiation. This total setup time can easily be hundreds of milliseconds on a mobile network.
How TLS 1.3 speeds things up
TLS 1.3, now widely adopted, is a major improvement. It streamlines the handshake process, reducing the cost from two round trips down to one. The client sends its parameters and immediately guesses which key agreement protocol the server will use, saving a full RTT.
Now, the total cost for a new connection is 1 RTT (TCP) + 1 RTT (TLS 1.3). This is a significant reduction in initial connection latency.
TLS 1.3 also supports a "zero round trip" (0-RTT) mode for resuming previous sessions. If you've connected to a server recently, the client can send encrypted application data in its very first message. This is powerful but has security trade-offs, as these 0-RTT requests are vulnerable to replay attacks.
How this appears in your code and tools
You don't write code to perform these handshakes. The browser or Node.js runtime handles them automatically whenever you initiate a secure network request.
When you call fetch, the time spent on these handshakes happens before your request for /items is ever sent over the wire. This is the "Initial connection" or "SSL" phase you see in your browser's network timing tab.
async function getItems() {
try {
// Before this HTTP GET request is sent, the browser performs:
// 1. TCP 3-way handshake (1 RTT)
// 2. TLS handshake (1-2 RTTs)
const response = await fetch('https://api.example.com/items');
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
console.log('Data received:', data);
return data;
} catch (error) {
console.error('Failed to fetch data:', error);
}
}
getItems();
The latency is most noticeable on the very first request to a domain. Subsequent requests can often reuse the existing warm connection, avoiding the handshake penalty entirely.
Try it
Open your browser's DevTools to the Network tab. Check "Disable cache" and load a website you haven't visited recently, like https://www.fastly.com. Click the first HTML document request and view the "Timing" tab. See the time spent on "Initial connection" and "TLS setup." That's the cost, right there.
Next: We'll look at how browsers manage these connections to avoid paying this setup cost over and over in the "Browser Network Stack and Connection Pooling" lesson.
Lock it in — apply this lesson under interview conditions.