Introduction to HTTP Connections
Hypertext Transfer Protocol (HTTP) is the foundation of communication on the World Wide Web. It follows the client-server model where a client (such as a browser) requests data from a web server. The mode of connection used in HTTP significantly impacts the efficiency and performance of data transmission. There are two primary types of HTTP connections: Non-Persistent and Persistent.
HTTP Non-Persistent Connection
A non-persistent connection is the traditional mode of HTTP communication where a separate TCP connection is established for each HTTP request-response pair. Once the server sends the response, the connection is closed.
Characteristics of Non-Persistent Connection:
- Multiple TCP Connections: Each request-response cycle requires a new connection.
- Increased Latency: Establishing a new connection for every request leads to higher overhead.
- Short-Lived Connection: After serving the response, the connection is terminated.
- Less Efficient for Multiple Requests: Each object on a webpage (HTML, CSS, images, scripts) requires a separate connection.
Advantages of Non-Persistent Connection:
- Simple to implement.
- Reduces server resource usage by closing connections quickly.
Disadvantages of Non-Persistent Connection:
- Higher latency due to multiple TCP handshakes.
- Increased network congestion as each request requires a separate connection.
HTTP Persistent Connection
A persistent connection (also known as a keep-alive connection) allows multiple requests and responses to be sent over a single TCP connection, reducing the overhead of establishing new connections.
Characteristics of Persistent Connection:
- Single TCP Connection: The connection remains open for multiple request-response transactions.
- Reduced Latency: Avoids repeated connection setup, improving efficiency.
- Keep-Alive Mechanism: The connection remains active until a timeout or until explicitly closed.
- Better Performance for Multiple Requests: Ideal for loading complex web pages with multiple objects.
Advantages of Persistent Connection:
- Reduces the overhead of establishing multiple connections.
- Enhances web performance and speeds up content delivery.
- Lowers network congestion due to fewer TCP connections.
Disadvantages of Persistent Connection:
- Increased resource usage on the server due to maintaining open connections.
- Requires additional management to handle timeouts and connection limits.
Comparison Between Non-Persistent and Persistent Connections
Feature | Non-Persistent Connection | Persistent Connection |
---|---|---|
TCP Connection | New connection per request | Single connection for multiple requests |
Latency | Higher due to multiple handshakes | Lower due to a single handshake |
Network Overhead | High due to frequent connection setup | Lower due to reuse of connection |
Efficiency | Less efficient for multiple requests | More efficient for multiple requests |
Server Load | Lower (connections close quickly) | Higher (connections stay open longer) |
Conclusion
Non-persistent and persistent connections are two fundamental approaches in HTTP communication. Non-persistent connections, while simpler, result in increased latency due to frequent connection establishment. In contrast, persistent connections enhance efficiency by keeping a single connection open for multiple requests. Modern web applications prefer persistent connections to optimize performance and reduce network congestion.
Numerical Problems
1. Latency Calculation for Non-Persistent Connection
A webpage consists of 10 objects, and each object requires a separate HTTP request. If the Round Trip Time (RTT) is 100 ms and each request-response cycle requires 2 RTTs, what is the total time required to load the page using a non-persistent connection?
Solution: Total time = Number of objects × Time per request-response cycle = 10 × (2 × 100 ms) = 2000 ms or 2 seconds.
2. Efficiency of Persistent Connection
If the same webpage with 10 objects is loaded using a persistent connection and the initial connection setup takes 2 RTTs while each subsequent request takes 1 RTT, what is the total load time?
Solution: Total time = Initial setup time + (Number of objects × Time per object) = (2 × 100 ms) + (10 × 100 ms) = 200 ms + 1000 ms = 1200 ms or 1.2 seconds.
Example
Scenario: A client wants to download a webpage with index.html
and three images: image1.jpg
, image2.png
, and image3.gif
.
Assumptions:
- RTT (Round Trip Time): The time it takes for a small packet to travel from the client to the server and back is 20 ms. We’ll assume this is constant for all connections.
- Server Processing Time: The time the server spends processing a request (finding the file, etc.) is negligible (0 ms).
- File Sizes & Transmission Times:
index.html
: 5 KB (5 * 1024 bytes). Transmission time = 40 ms (calculated based on an assumed bandwidth – this is just an example, you’d use a realistic bandwidth for a real-world scenario. We are just using this to show a relative difference).image1.jpg
: 10 KB. Transmission time = 80 ms.image2.png
: 15 KB. Transmission time = 120 ms.image3.gif
: 20 KB. Transmission time = 160 ms.
1. Non-Persistent HTTP:
In a non-persistent connection, a new TCP connection is established for each request (HTML file and each image).
index.html
:- Establish TCP connection: 1 RTT (20 ms)
- Send request: Negligible (we are ignoring server processing time).
- Receive
index.html
: 40 ms - Close TCP connection: 1 RTT (20 ms)
- Total for
index.html
: 20 + 40 + 20 = 80 ms
image1.jpg
:- Establish TCP connection: 1 RTT (20 ms)
- Receive
image1.jpg
: 80 ms - Close TCP connection: 1 RTT (20 ms)
- Total for
image1.jpg
: 20 + 80 + 20 = 120 ms
image2.png
:- Establish TCP connection: 1 RTT (20 ms)
- Receive
image2.png
: 120 ms - Close TCP connection: 1 RTT (20 ms)
- Total for
image2.png
: 20 + 120 + 20 = 160 ms
image3.gif
:- Establish TCP connection: 1 RTT (20 ms)
- Receive
image3.gif
: 160 ms - Close TCP connection: 1 RTT (20 ms)
- Total for
image3.gif
: 20 + 160 + 20 = 200 ms
- Grand Total (Non-Persistent): 80 + 120 + 160 + 200 = 560 ms
2. Persistent HTTP (without pipelining):
With persistent connections, the TCP connection remains open after the server sends the response for the first request. However, the client waits for the response to the previous request before sending the next.
index.html
:- Establish TCP connection: 1 RTT (20 ms)
- Receive
index.html
: 40 ms - Total for
index.html
: 20 + 40 = 60 ms
image1.jpg
:- Send request (over existing connection): Negligible
- Receive
image1.jpg
: 80 ms - Total for
image1.jpg
: 80 ms
image2.png
:- Send request: Negligible
- Receive
image2.png
: 120 ms - Total for
image2.png
: 120 ms
image3.gif
:- Send request: Negligible
- Receive
image3.gif
: 160 ms - Close TCP connection: 1 RTT (20ms) (Connection can be closed after all transfers)
- Total for
image3.gif
: 160 + 20 = 180 ms
- Grand Total (Persistent without pipelining): 60 + 80 + 120 + 180 = 440 ms
3. Persistent HTTP (with pipelining):
With pipelining, the client can send multiple requests over the same persistent connection without waiting for the response to each individual request.
index.html
:- Establish TCP connection: 1 RTT (20 ms)
- Receive
index.html
: 40 ms - Total for
index.html
: 20 + 40 = 60 ms
image1.jpg
:- Receive
image1.jpg
: 80 ms - Total for
image1.jpg
: 80 ms
- Receive
image2.png
:- Receive
image2.png
: 120 ms - Total for
image2.png
: 120 ms
- Receive
image3.gif
:- Receive
image3.gif
: 160 ms - Close TCP connection: 1 RTT (20ms)
- Total for
image3.gif
: 160 + 20 = 180 ms
- Receive
- Grand Total (Persistent with pipelining): 60 + 80 + 120 + 180 = 440 ms (In this specific example, pipelining doesn’t offer much improvement because the transmission times are much larger than the RTT. Pipelining’s advantage is more apparent when RTTs are significant compared to transmission times)
Key Observations:
- Non-persistent HTTP has a significant overhead due to establishing and closing a TCP connection for every single file.
- Persistent HTTP reduces this overhead considerably.
- Pipelining can further improve performance, especially when RTTs are a larger proportion of the total time.