Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Networking 2: TCP, UDP and Ports

    Interview Questions

    Real interview questions on Networking 2: TCP, UDP and Ports, answered the way you would say them out loud.

    Extra: Interview prep6 min read

    Interview questions

    TCP vs UDP — when would you use each?

    How I'd answer: "TCP gives a reliable, ordered byte stream: it sets up a connection with a handshake, numbers every byte, resends anything lost, and does flow and congestion control. The cost is extra latency — a round trip to connect, and head-of-line blocking when a packet is lost. UDP just adds ports and a checksum on top of IP: no connection, no ordering, no retransmission, so it's fast and lightweight.

    I'd use TCP whenever the data has to be complete and exact — web APIs, database connections, file transfers. I'd use UDP when latency matters more than completeness, like voice and video calls or games where a late packet is useless, or for tiny request-reply exchanges like DNS. And QUIC, which HTTP/3 uses, is a middle ground: it builds reliability on top of UDP but with independent streams, so it avoids TCP's head-of-line blocking."

    Combines: Ch 3, 4, 6, 9.

    Walk me through the TCP three-way handshake. Why three steps and not two?

    How I'd answer: "The client sends SYN with its initial sequence number. The server replies SYN-ACK, acknowledging the client's number and sending its own. The client sends ACK acknowledging the server's number. Then both are ESTABLISHED.

    It needs three because each side must both send its starting sequence number and learn that the other side received it. After two packets, the server has sent its number but doesn't know the client got it. The third packet confirms that. It also proves both directions of the path work. The practical consequence is that every new connection costs one round trip before data — which is why we reuse connections."

    Combines: Ch 5, 12.

    Our service calls a partner API and sometimes gets 'connection reset by peer'. What could cause that?

    How I'd answer: "A reset means the other side — or something in between — sent a TCP RST, killing the connection. Common causes: the partner's server closed or crashed while we were using the connection; we're reusing a pooled keep-alive connection that the server, a load balancer or a NAT gateway already timed out as idle — so our next request on it gets reset; or the server is overloaded and dropping connections.

    The idle-timeout case is the most common with connection pools. The fixes are to make our pool's idle timeout shorter than the server's and the NAT's, enable TCP keep-alive, and retry safe requests once on a reset. I'd confirm by checking whether resets happen mostly on the first request after an idle period."

    Combines: Ch 8, 10, 12.

    Why do we need a database connection pool? What goes wrong if it's too small or too large?

    How I'd answer: "Opening a database connection is expensive — a TCP handshake, often TLS, authentication, and the database setting up per-connection state; for Postgres that's a whole server process. Doing that for every request would add milliseconds and overload the database. A pool keeps a fixed set of open connections that request handlers borrow and return.

    If it's too small, requests queue waiting for a free connection and latency climbs even though the database is idle. If it's too large — especially across many app instances, say 20 pods with 50 connections each is 1,000 — the database spends its memory and CPU on connections and slows down or refuses new ones. So I'd size it from the database's connection limit divided by the number of app instances, and watch the pool's wait time."

    Combines: Ch 2, 5, 12. (Backend detail: FastAPI with Databases (coming soon).)

    How can many devices in a home share one public IP address? Why can't someone on the internet connect directly to one of them?

    How I'd answer: "The home router does NAT — technically port address translation. When a device opens a connection, the router rewrites the source private IP and port to its public IP and a unique port, and records the mapping in a table. Replies come back to that public port, and the router uses the table to forward them to the right device.

    An unsolicited incoming connection has no table entry, so the router doesn't know which device it's for and drops it. To allow it you'd need port forwarding — a static rule — or a tunnel. The same idea appears in the cloud as a NAT gateway: private servers can call out to the internet, but nothing can call in."

    Combines: Ch 1, 10, plus private addresses from Networking 1: Addresses and Routing Ch 6.