When you’re preparing for a software engineering interview, one common question you might encounter is “What happens when you enter a URL into a browser and press enter?” In this article, we’ll provide an in-depth answer to this question as explained by a Senior Software Engineer.
If you’re asked this question in an interview, the person interviewing you is likely probing the depth of your knowledge. Candidates with varying degrees of experience will answer this question differently, as they will go as deep as their knowledge takes them.
The browser goes through a series of steps to retrieve and display the requested content.
- The browser first attempts to resolve the domain with an
IP
address by looking up the domain in the browser’sDNS
cache. - If the first step doesn’t return an
IP
address, the browser will look up theDNS
record in your operating system’s cache. Many operating systems allow you to define theIP
address of aDNS
server that you would want to use, the browser will then use your cache or that definedIP
address to attempt to resolve the domain name. - If the second step fails and the domain still isn’t resolved, it will fall back on your router’s
DNS
entries. - If the third step fails as well, and your ISP can’t resolve the domain name, it will fall back on different
DNS
servers to try and find theIP
address. - Once the domain has been resolved to an
IP
address, the browser will initiate aTCP
connection with the server using aTCP/IP
handshake. The handshake will exchange a synchronize and acknowledge message to establish a connection. - Once the
TCP
connection is established, the browser sends anHTTP GET
request to the web server. - The
HTTP
Request will contain additional information, for example a user agent and accept Headers. - If the URL requires authentication, a token / key may be passed in the request.
- The server will then handle the request and send back a response.
- Responses usually contain what’s required for the browser to render the required resource.
- Depending on the load of the server you’re requesting, you may initially meet a load balancer – a small script that knows what servers are available to do the work, and how to route messages to these servers.
- The browser will then display the content for the requested resource by parsing the
HTML
or text and loadingCSS
andJavaScript
resources.
Related: Questions You Should Ask when Interviewing for a Software Engineering Position