title: How many sockets do Google open for every request it receives?
body: <p>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).</p>
  
  <ol>
  <li><p><em>Interviewer</em>: Tell me the process that happens behind the scenes when
  I open a browser and type <code>google.com</code> in it.</p>
  
  <p><em>Me</em>: The first thing that happens is a socket is created which is 
  identified by <code>{SRC-IP, SRC-PORT, DEST-IP, DEST-PORT, PROTOCOL}</code>. The
  <code>SRC-PORT</code> 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).</p></li>
  <li><p><em>Interviewer</em>: Wait, when does the name resolution happen? </p>
  
  <p><em>Me</em>: 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. </p></li>
  <li><p><em>Interviewer</em>: Is a socket created for DNS name resolution? </p>
  
  <p><em>Me</em>: 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).</p></li>
  <li><p><em>Interviewer</em>: <code>google.com</code> is open for other requests from other
  clients. So is establishing your connection with Google blocking
  other users?</p>
  
  <p><em>Me</em>: I am not sure how Google handles this. But in a typical socket
  communication, it is blocking to a minimal extent.</p></li>
  <li><p><em>Interviewer</em>: How do you think this can be handled? </p>
  
  <p><em>Me</em>: 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.</p></li>
  <li><p><em>Interviewer</em>: If that is the case, is this child socket’s port number
  different than the parent one?</p>
  
  <p><em>Me</em>: The parent socket is listening at 80 for new requests from
  clients. The child must be listening at a different port number.</p></li>
  <li><p><em>Interviewer</em>: How is your TCP connection maintained since your destination port number has changed. (That is the src-port number sent on Google's packet) ?</p>
  
  <p><em>Me:</em>  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.</p></li>
  <li><p><em>Interviewer</em>: How long is your socket connection established with
  Google? </p>
  
  <p><em>Me</em>: 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.</p>
  
  <p><em>Interviewer</em>: No, Google will not keep a dedicated child socket for
  you. It handles your request and discards/recycles the sockets right
  away.</p></li>
  <li><p><em>Interviewer</em>: 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?</p>
  
  <p><em>Me</em>: 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.</p></li>
  <li><p><em>Interviewer</em>: Do you think the way Google handles this is different from
  any bank website? </p>
  
  <p><em>Me</em>: 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.</p></li>
  </ol>
  
  <p>If someone can give an explanation of each of the points, it will be very helpful for many beginners in networking.</p>