July 6, 2024

Hyper Text Transfer Protocol

HTTP (Hypertext Transfer Protocol) is a protocol used for transferring data over the internet. It's the foundation of data communication for the World Wide Web. HTTP is a client-server protocol, where the client (usually a web browser) initiates a request, and the server responds with the requested data.

There are multiple versions of HTTP and each version brings improvements to the protocol. The most common versions are HTTP/1.1, HTTP/2, and HTTP/3.

If you are wondering, how do we know which version of HTTP is being used ? You can find this out by looking at the requests in the network tab of your browser's developer tools.

By default you might not have protocol column in the network tab. To enable it, right click on the network tab and select "Protocol".

Here's what you see in the protocol column:

The H3 in protocol column means HTTP/3. Go to google.com and you can see this for yourself.

This blog uses HTTP/2.

If you go to shippingchina.com, you will see HTTP/1.1.

How does browser decide which version of HTTP to use ?

  • Browsers decide which HTTP version to use through a process called "HTTP version negotiation".
  • When the browser makes the first request to the server, it starts by using the most recent version of HTTP it supports.
  • If the server doesnt support the version, it downgrades to the next supported version, one by one.

Comparison of HTTP/1, HTTP/2, and HTTP/3

Here's a quick comparison of HTTP/1, HTTP/2, and HTTP/3:

FeatureHTTP/1.1HTTP/2HTTP/3
Release Year199720152022 (Finalized)
Transport ProtocolTCPTCPQUIC (Based on UDP)
Connection ManagementOne connection per request (or keep-alive for multiple sequential requests)Single multiplexed connection for multiple requestsMultiple independent streams over a single connection
MultiplexingNo - Head-of-line blocking issuesYes - Multiple requests in parallel over single connectionYes - Independent streams with no head-of-line blocking
Header CompressionNo - Headers sent in plain textYes - HPACK compressionYes - QPACK compression
Server PushNoYes - Server can push resources before client requestsYes - Enhanced with better control
LatencyHigher latency due to TCP handshakesImproved over HTTP/1.1 but still affected by TCPSignificantly reduced with 0-RTT connection establishment
Connection MigrationNo - New connection needed when network changesNo - New connection needed when network changesYes - Can survive IP changes (mobile to WiFi)
Binary ProtocolNo - Text-basedYes - More efficient parsingYes - Binary framing
Use Cases
  • Simple websites
  • Legacy systems
  • When compatibility is essential
  • Limited resource environments
  • Modern websites with many resources
  • API services
  • When TCP is required but performance matters
  • Media-rich applications
  • Mobile applications
  • High-latency networks
  • Real-time applications
  • Connections that need to survive network changes
Limitations
  • Head-of-line blocking
  • Higher bandwidth usage
  • Multiple connections increase overhead
  • Slow start after idle connections
  • Still suffers from TCP head-of-line blocking
  • Complex implementation
  • TCP handshake overhead
  • Requires HTTPS in most browsers
  • Newer, less widespread support
  • More CPU intensive
  • UDP may be blocked in some corporate networks
  • More complex debugging
Browser SupportUniversalMost modern browsers (>95%)Growing (Chrome, Firefox, Safari, Edge)
Real World Examples
  • Basic blogs
  • Legacy web applications
  • Simple API endpoints
  • Example: Many government websites
  • Google services
  • Facebook
  • Twitter
  • Most major CDNs
  • Google services
  • Facebook
  • Cloudflare-hosted sites
  • Fastly CDN customers
When to Use
  • Simple websites with few resources
  • When maximum compatibility is needed
  • Legacy system support
  • Very constrained environments
  • Modern web applications
  • Sites with many assets (images, scripts)
  • When performance matters but HTTP/3 isn't viable
  • Most general web development
  • Performance-critical applications
  • Mobile-first development
  • Real-time communication
  • Applications on unreliable networks

Summary

If your distributed system demands low latency, efficient bandwidth utilization, handles many concurrent requests, or requires strong security, migrating to HTTP/2 or HTTP/3 offers substantial advantages over HTTP/1.1. The specific choice between HTTP/2 and HTTP/3 often depends on the maturity of library support and the specific network conditions you anticipate. HTTP/3's use of QUIC over UDP can be particularly beneficial in lossy network environments, but might face challenges with firewall configurations in some scenarios.