What happens when you open a URL
This is one of the most common opening questions in systems interviews, and it is also a troubleshooting map: every step is a place where a request can fail, and each has its own command to check it.
The four steps
Opening a web address takes four steps, in order: find the server's address (DNS), open a connection to it (TCP), make the connection private and prove who the server is (TLS), then send the request and read the answer (HTTP). In between sit load balancers, which spread requests over many servers and stop sending traffic to ones that fail health checks.
DNS: name to address
Your machine asks a resolver, listed in /etc/resolv.conf, for the address behind a name. The resolver either has the answer cached or asks the authoritative servers for that domain. Every answer carries a time to live (TTL), which is how long it may be cached; a low TTL makes changes take effect quickly and a high one reduces load. DNS normally uses UDP and switches to TCP when an answer is too large to fit.
$ dig +short app.example.com # what address do we get $ dig @10.0.0.2 app.example.com # ask one specific resolver $ dig +trace app.example.com # follow the chain from the root
TCP: a connection
TCP opens a connection with a three-step handshake: the client sends SYN, the server replies SYN-ACK, the client replies ACK. The two common failures look different, and telling them apart is worth a lot in an interview. Connection refused comes back immediately and means the machine answered but nothing is listening on that port. A timeout means nothing answered at all, which points at a firewall, a wrong route, or a machine that is down.
$ nc -vz app.example.com 443 # can we open the port at all $ ss -tlnp | grep 443 # on the server: is anything listening $ ss -s # socket counts by state
TLS: a private connection
During the TLS handshake the client names the site it wants (SNI), the server presents a certificate, and the client checks three things: the certificate is for that name, it has not expired, and it chains to an authority the client trusts. A failure in any of the three stops the connection before a single HTTP byte is sent, which is why a site can be reachable by TCP and still not load.
$ openssl s_client -connect app.example.com:443 -servername app.example.com </dev/null \ | openssl x509 -noout -dates -subject # who is this certificate for, and when does it expire
HTTP: the request
The response starts with a status code. The first digit tells you whose problem it is: 2xx is success, 3xx is a redirect, 4xx means the request was wrong, and 5xx means the server side failed. Three 5xx codes are worth knowing precisely, because they point to different places.
- 502 Bad Gateway: the proxy reached the backend and got an invalid answer or a dropped connection. Look for a crashing or restarting backend.
- 503 Service Unavailable: there was no healthy backend to send to, or the service refused the work. Look at health checks and overload.
- 504 Gateway Timeout: the backend accepted the request and did not answer in time. Look for a slow dependency.
Using it to debug
One command shows how long each step took. If the name lookup is slow, it is DNS. If connect is slow, it is the network path. If the gap before the first byte is large, the server or something behind it is slow.
$ curl -s -o /dev/null -w 'dns %{time_namelookup} tcp %{time_connect} tls %{time_appconnect} first-byte %{time_starttransfer} total %{time_total}\n' https://app.example.com/