1. What Is It?
(Transmission Control Protocol) and (User Datagram Protocol) are the two dominant transport-layer protocols. They sit between IP (which routes packets across the internet) and application protocols like or (which define what the data means).
The core difference: guarantees delivery and ordering; does not. TCP establishes a connection, acknowledges every segment, retransmits lost data, and ensures bytes arrive in order. UDP sends datagrams with no handshake, no acknowledgment, and no retransmission — if a packet is lost, it's gone.
This isn't TCP-good, UDP-bad. It's a tradeoff between reliability and overhead. For most application-layer protocols (, databases, email), you want TCP's guarantees. For real-time workloads where a retransmit would arrive too late to be useful (video streaming, gaming, lookups), UDP's low overhead wins.
A multiplayer game server needs to broadcast real-time player position updates to all connected clients 60 times per second. If a position update packet is lost in transit, the next update will arrive within ~17ms anyway, making retransmission pointless. Which transport protocol is the better fit, and why?
2. How It Works
TCP Connection Lifecycle
Key mechanisms:
- Sequence numbers — every byte is numbered; the receiver can detect gaps and request retransmission.
- Flow control — the receiver advertises a window size telling the sender how much data it can buffer. Prevents a fast sender from overwhelming a slow receiver.
- Congestion control — detects packet loss (a proxy for network congestion) and slows its send rate. Algorithms like CUBIC and BBR govern this behavior.
- Nagle's algorithm — TCP buffers small writes and coalesces them into larger segments. Can add latency for interactive protocols; disable with
TCP_NODELAYwhen needed (used by most database drivers and /2 implementations).
UDP
has an 8-byte header: source port, destination port, length, checksum. That's it. No connection, no state, no acknowledgment. The application layer handles reliability if it needs it.
Where is the right choice:
- — single request/response fits in one datagram; TCP handshake overhead isn't worth it. ( does fall back to TCP for large responses.)
- QUIC / /3 — built on UDP with reliability and multiplexing implemented in userspace, avoiding TCP's .
- Real-time media (WebRTC, VoIP) — a retransmitted audio frame arriving 200ms late is useless; better to skip it and move on.
- DHCP, NTP — simple query/response protocols with low overhead requirements.
TCP opens with a three-way handshake
The client and server exchange SYN, SYN-ACK and ACK to establish a TCP connection.
The client sends SYN with sequence number one hundred to begin the TCP connection.
Text version
- The client sends SYN with sequence number one hundred to begin the TCP connection.
- The server's SYN-ACK says it expects number one hundred one next and starts its own count at two hundred.
- The client acknowledges the server with acknowledgment number two hundred one, completing the handshake.
A developer is building a VoIP application where users send real-time audio packets to each other. Occasionally, a packet is lost in transit. Which behavior is most appropriate for this use case?
3. What SDEs Actually Need to Know
The 3-way handshake costs a round trip. Before the first byte of data is sent over a new connection, a client and server exchange SYN/SYN-ACK/ACK. On a 100ms RTT link, this is 100ms of overhead before and even start. and HTTP/2 persistent connections exist to amortize this cost.
Half-open connections and timeouts. If a server process crashes without sending FIN, the client's stack may sit in ESTABLISHED state indefinitely waiting for data. Keepalives (TCP keepalive probes via SO_KEEPALIVE, or application-level pings such as HTTP/2 PING frames) detect these zombie connections. This is why you should always configure connection timeouts in database and HTTP clients.
Connection refused vs. timeout:
Connection refused(ECONNREFUSED) — the OS on the target machine is rejecting the connection, typically because nothing is listening on that port. Fast fail.Connection timed out(ETIMEDOUT) — no response at all. Either the machine is unreachable, a firewall is silently dropping packets, or the port is blocked. Slow fail (waits until the kernel's retry timer expires, often 75+ seconds by default).
TCP . HTTP/1.1 pipelining sends several requests on one connection without multiplexing (responses must return in order). A single lost TCP segment blocks all subsequent data on the connection until the retransmit arrives. HTTP/2 uses streams within a single connection — but those streams still share one TCP socket, so one lost packet still stalls all streams. HTTP/3 (QUIC over ) solves this by implementing independent reliability per stream.
A TCP gap blocks HTTP/2 delivery
A lost TCP segment holds back all later bytes behind it, including bytes from other HTTP/2 streams.
A lost TCP segment leaves a gap in the connection shared by HTTP/2 streams.
Text version
- A lost TCP segment leaves a gap in the connection shared by HTTP/2 streams.
- Stream B lost nothing, but TCP holds its data to keep the byte order.
- The retransmitted segment fills the gap that blocked delivery to the application.
- TCP delivers the bytes in order again, so the waiting HTTP/2 streams continue.
Your backend service connects to a remote database, but after a network partition, the database process crashes without sending a FIN packet. Which of the following best describes what happens to your service's TCP connection, and what should you do to prevent it from hanging indefinitely?
4. Tradeoffs & Decisions
When to use :
- /REST APIs, database connections, file transfers, email — anywhere data integrity and ordering are required.
- When your application doesn't want to implement its own reliability.
When to consider :
- Real-time media where latency beats reliability (WebRTC, game state updates).
- High-frequency, loss-tolerant telemetry or sensor data.
- Implementing a custom protocol where you want fine-grained control over reliability behavior (QUIC-style).
tuning decisions SDEs occasionally touch:
TCP_NODELAY— disable Nagle's algorithm when you need low-latency small messages (database drivers, Redis clients, gRPC).SO_KEEPALIVE— detect dead connections. Usually also set idle timeout at the application layer (database pool health check, idle timeout).- Backlog queue size — the
listen()backlog limits how many unaccepted connections the OS queues. Under heavy load, a full backlog makes Linux drop new SYNs, so clients see slow connects or timeouts rather thanConnection refused. Linux caps the backlog atnet.core.somaxconn(default 4096 since kernel 5.4, 128 before). Production servers often pass a backlog of 1024+.
Choose the guarantees your workload needs
TCP gives reliable, ordered delivery. UDP cuts transport overhead and lets the app choose its own reliability.
Choose TCP when the application needs reliable delivery with bytes kept in order.
Text version
- Choose TCP when the application needs reliable delivery with bytes kept in order.
- UDP sends datagrams with no handshake, so the first datagram does not wait a round trip for setup.
- With UDP, the application must provide any reliability it needs because UDP does not retransmit lost data.
- TCP suits databases and files, while UDP suits real-time media and game updates that can tolerate loss.
A backend team is running a high-traffic HTTP API server and starts seeing clients intermittently receive 'Connection refused' errors during traffic spikes, even though the server process is healthy and accepting connections normally at lower load. Which configuration change is most likely to fix this?
5. Interview Cheat Sheet
Key sentences:
- " guarantees reliable, ordered delivery using sequence numbers and acknowledgments; sends datagrams with no delivery guarantee and minimal overhead."
- "The 3-way handshake (SYN/SYN-ACK/ACK) costs one round trip before data can flow — this is why matters for latency-sensitive services."
- "/3 runs over QUIC on to eliminate TCP's : each stream gets independent reliability without a shared TCP connection bottleneck."
- "Connection refused means nothing is listening on that port (fast); connection timeout means packets are being dropped somewhere (slow)."
Common follow-ups:
Q: Why does losing one TCP packet stall all /2 streams? A: HTTP/2 multiplexes logical streams over a single TCP connection. TCP guarantees in-order delivery, so if a segment is lost, the OS won't deliver any later segments to the application until the missing one is retransmitted and received. All streams on that connection block. QUIC (HTTP/3) avoids this by implementing per-stream reliability in userspace over UDP.
Q: What is TCP slow start? A: When a new TCP connection opens or after detecting congestion, TCP starts sending at a small congestion window and exponentially increases it until it detects a loss. This means the first few seconds of a connection are rate-limited even if both sides have high bandwidth available. It's one reason why reusing established connections (, pooling) matters for performance.
Q: What's the difference between a FIN and a RST? A: FIN is a graceful close — "I have no more data to send" — and allows the other side to finish sending before closing. RST is an abrupt reset — "this connection is invalid, discard it immediately." You typically see RST when a process crashes, a port is unreachable, or a forcibly terminates a connection.