Client — Server Flow in a Developer Perspective
2 min readNov 22, 2024
When you connect to a web server for the first time and make subsequent calls, the request flow involves several steps. Here’s a breakdown of the process:
First-Time Connection to a Web Server
- URL Entry: You enter a URL (e.g.,
www.example.com
) in your web browser. - DNS Resolution: The browser checks its cache to see if it has the IP address for the domain. If not found, it sends a request to a DNS (Domain Name System) server to resolve the domain name into an IP address. The DNS server responds with the corresponding IP address.
- Establishing a Connection: With the IP address, the browser establishes a connection to the web server using the Transmission Control Protocol (TCP). This usually involves a three-way handshake (SYN, SYN-ACK, ACK).
- Sending the HTTP Request: The browser sends an HTTP request (e.g., GET request) to the web server to retrieve the desired resource (like a web page).
- Server Response: The web server processes the request and sends back an HTTP response, which includes the requested resource and a status code (like 200 for success).
- Rendering the Page: The browser receives the response and renders the web page for you to view.
Subsequent Calls to the Same Web Server
- Cached DNS Entry: For subsequent requests, the browser may use the cached IP address from the first request, so it may not need to query the DNS server again.
- Reusing the TCP Connection: If the server supports persistent connections (HTTP/1.1 default behavior), the browser may reuse the existing TCP connection for faster communication.
- Sending Additional HTTP Requests: The browser may send additional HTTP requests for other resources (like images, stylesheets, scripts) as needed. These requests may still use the cached IP address.
- Receiving Responses: The server responds to each request, and the browser continues to render the page or update content as necessary.
Does Every Request Pass Through DNS?
Not Every Request: No, not every request passes through DNS. Once the IP address is resolved for the first request, the browser caches that IP address for a certain period (defined by the DNS Time-to-Live, or TTL). Subsequent requests to the same domain can use the cached IP address without needing to query DNS again, unless the cache expires or is cleared.
- DNS TTL: Defined in the DNS zone file for each record, indicating how long clients can cache the record.
- TCP Connection: Lifetime varies based on factors like idle timeout, keep-alive settings, application layer protocols, and server configurations, without a fixed duration.