What happens when we click on any website ,for instance amazon.com
When one clicks on a website like amazon.com, a series of complex steps happen behind the scenes to fetch and display the webpage. Here's a detailed breakdown of the activities:
1. Browser-Level Actions
User Input: You type
amazon.com
in your browser and hit enter.URL Parsing: The browser parses the URL to extract components like protocol (
http://
orhttps://
), domain name (amazon.com
), and path (e.g.,/home
).
2. DNS Resolution
The browser needs to convert the domain name (amazon.com
) into an IP address.
Browser Cache: The browser checks its cache for a cached IP address of
amazon.com
.Operating System Cache: If not in the browser cache, the OS cache is checked.
Local DNS Resolver: If not found, the query goes to the local DNS resolver (usually provided by your ISP).
Recursive DNS Lookup:
The resolver queries root DNS servers to find the top-level domain (TLD) server (e.g.,
.com
).The TLD server directs it to the authoritative DNS server for
amazon.com
.The authoritative server provides the IP address.
3. TCP/IP Connection
Once the IP address is obtained:
TCP Handshake: The browser establishes a TCP connection with the web server at the resolved IP using the three-way handshake:
SYN: The client sends a synchronize packet.
SYN-ACK: The server acknowledges.
ACK: The client acknowledges, establishing the connection.
TLS Handshake (if HTTPS): For secure connections, the browser performs a TLS handshake to establish an encrypted connection.
4. HTTP/HTTPS Request
The browser sends an HTTP or HTTPS request to the server for the desired resource:
Method:
GET
(to fetch the webpage).Headers: Includes details like the browser type, cookies, etc.
The request may pass through intermediate entities like:
Load Balancers: Distribute traffic among multiple servers.
CDNs (Content Delivery Networks): Serve static resources (like images, JavaScript, and CSS) from geographically closer servers.
5. Backend Server Processing
On the server side:
Routing:
The load balancer routes the request to the appropriate web server.
The web server (e.g., Nginx, Apache) routes it to the backend application.
Application Logic:
The backend application processes the request.
Queries are sent to databases for dynamic content.
Business logic is applied to construct the response.
Microservices:
For complex systems like Amazon, the request might invoke multiple microservices (e.g., product catalog, pricing, recommendations).
6. Database and Cache
Cache Layer:
Before querying the database, the server checks cache systems like Redis or Memcached for frequently requested data.
Database Query:
If the cache doesn't have the data, a query is made to databases (e.g., MySQL, PostgreSQL, or NoSQL databases like DynamoDB).
The results are aggregated and returned to the backend application.
7. Server Response
The server constructs the HTTP response:
Status Code: e.g.,
200 OK
,404 Not Found
.Headers: Metadata about the response.
Body: The HTML, CSS, JavaScript, or JSON data for the webpage.
8. Browser Rendering
The browser receives the response and:
Parses HTML: Builds the DOM (Document Object Model).
Fetches Resources:
Loads additional CSS, JavaScript, and images referenced in the HTML.
These may result in additional HTTP requests.
Executes JavaScript: Dynamically updates the DOM or interacts with APIs.
Renders the Page: The final page is displayed to the user.
9. User Interaction and Subsequent Requests
Any interaction with the webpage (e.g., clicking a product) triggers additional HTTP requests, repeating some or all of the above steps.
Summary of Key Components
DNS Resolution: Converts
amazon.com
to an IP address.TCP/TLS Handshake: Establishes a connection with the server.
HTTP Request: Sends the request to the server.
Backend Processing: Handles the request using business logic, databases, and caches.
HTTP Response: Sends the response back to the client.
Browser Rendering: Displays the webpage to the user.
This intricate chain of events ensures that clicking amazon.com
provides a fast, seamless, and secure experience.
Same process happens for all the websites