The following is my recent interview experience with a reputed network software company. I was asked questions about interconnecting TCP level and web requests and that confused me a lot. I really would like to know expert opinions on the answers. It is not just about the interview but also about a fundamental understanding of how networking work (or how application layer and transport layer cross-talk, if at all they do).
Interviewer: Tell me the process that happens behind the scenes when I open a browser and type
google.com
in it.Me: The first thing that happens is a socket is created which is identified by
{SRC-IP, SRC-PORT, DEST-IP, DEST-PORT, PROTOCOL}
. TheSRC-PORT
number is a random number given by the browser. Usually the TCP/IP connection protocol (three-way handshake is established). Now both the client (my browser) and the server (Google) are ready to handle requests. (TCP connection is established).Interviewer: Wait, when does the name resolution happen?
Me: Yep, I am sorry. It should have happened before even the socket is created. DNS name resolve happens first to get the IP address of Google to reach at.
Interviewer: Is a socket created for DNS name resolution?
Me: hmm, I actually do not know. But all I know DNS name resolution is connectionless. That is, it's not TCP but UDP. Only a single request-response cycle happens. (So there is a new socket created for DNS name resolution).
Interviewer:
google.com
is open for other requests from other clients. So is establishing your connection with Google blocking other users?Me: I am not sure how Google handles this. But in a typical socket communication, it is blocking to a minimal extent.
Interviewer: How do you think this can be handled?
Me: I guess the process forks a new thread and creates a socket to handle my request. From now on, my socket endpoint of communication with Google is this child socket.
Interviewer: If that is the case, is this child socket’s port number different than the parent one?
Me: The parent socket is listening at 80 for new requests from clients. The child must be listening at a different port number.
Interviewer: How is your TCP connection maintained since your destination port number has changed. (That is the src-port number sent on Google's packet) ?
Me: The dest-port that I see as a client is always 80. When a response is sent back, it also comes from port 80. I guess the OS/the parent process sets the source port back to 80 before it sends back the post.
Interviewer: How long is your socket connection established with Google?
Me: If I don’t make any requests for a period of time, the main thread closes its child socket and any subsequent requests from me will be like I am a new client.
Interviewer: No, Google will not keep a dedicated child socket for you. It handles your request and discards/recycles the sockets right away.
Interviewer: Although Google may have many servers to serve requests, each server can have only one parent socket opened at port 80. The number of clients to access Google's webpage must be larger than the number of servers they have. How is this usually handled?
Me: I am not sure how this is handled. I see the only way it could work is spawn a thread for each request it receives.
Interviewer: Do you think the way Google handles this is different from any bank website?
Me: At the TCP-IP socket level, it should be similar. At the request level, slightly different because a session is maintained to keep state between requests for banks' websites.
If someone can give an explanation of each of the points, it will be very helpful for many beginners in networking.
This question doesn't actually appear in the interview, but it's in your title so I'll answer it. Google doesn't open any sockets at all. Your browser does that. Google accepts connections, in the form of new sockets, but I wouldn't describe that as 'opening' them.
No. The connection is identified by the tuple. The socket is an endpoint to the connection.
No. By the operating system.
Any rationally implemented browser would delegate the entire thing to the operating system's Sockets library, whose internal functioning depends on the OS. It might look at an in-memory cache, a file, a database, an LDAP server, several things, before going out to a DNS server, which it can do via either TCP or UDP. It's not a great question.
Wrong again. It has very little to do with Google specifically. A TCP server has a separate socket per accepted connection, and any rationally constructed TCP server handles them completely independently, either via multithreading, muliplexed/non-blocking I/O, or asynchronous I/O. They don't block each other.
Threads are created, not 'forked'. Forking a process creates another process, not another thread. The socket isn't 'created' so much as accepted, and this would normally precede thread creation. It isn't a 'child' socket.
Wrong again. The accepted socket uses the same port number as the listening socket, and the accepted socket isn't 'listening' at all, it is receiving and sending.
This question was designed to explore your previous wrong answer. Your continuation of your wrong answer is still wrong.
Wrong again. You don't know anything about threads at Google, let alone which thread closes the socket. Either end can close the connection at any time. Most probably the server end will beat you to it, but it isn't set in stone, and neither is which if any thread will do it.
Here the interviewer is wrong. He doesn't seem to have heard of HTTP keep-alive, or the fact that it is the default in HTTP 1.1.
Here you haven't answered the question at all. He is fishing for an answer about load-balancers or round-robin DNS or something in front of all those servers. However his sentence "the number of clients to access google webpage must be exceeding larger than the number of servers they have" has already been answered by the existence of what you are both incorrectly calling 'child sockets'. Again, not a great question, unless you've reported it inaccurately.
You almost got this one right. HTTP sessions to Google exist, as well as to bank websites. It isn't much of a question. He should be asking for facts, not your opinion.
Overall, (a) you failed the interview completely, and (b) you indulged in far too much guesswork. You should have simply stated 'I don't know' and let the interview proceed to things that you do know about.
For point #6, here is how I understand it: if both ends of an end to end connection were the same as that of another socket, there would indeed be no way to distinguish both socket, but if a single end is not the same as that of the other socket, then it's easy to distinguish both. So there is not need to turn destination port 80 (the default) forth and back, since the source ports differ.