BRUCE_FEBRUCE_FE

EN/CH Mode

BRUCE_FE Interview Notes - Complete HTTP/1 vs HTTP/2 Comparison

In-depth analysis of key differences between HTTP/1.x and HTTP/2, including multiplexing, server push, header compression, binary protocol features, and practical performance improvements and frontend development best practices.

影片縮圖

Lazy to read articles? Then watch videos!

Key Differences Between HTTP/1.x and HTTP/2

HTTP/2 brings many significant improvements over HTTP/1.x, making web pages load faster and more efficiently:

FeatureHTTP/1.1HTTP/2
ConnectionsA website can only open 6-8 connections simultaneouslyOne connection can handle multiple requests simultaneously
Data FormatPlain text transmission, like writing lettersBinary format, more efficient, like direct computer communication
Header HandlingEach request must repeat sending the same informationCompress header information, reduce redundant transmission
Resource PriorityAll requests treated equallyCan set important resources to download first
Server PushCan only passively respond to requestsServer can actively push related resources (e.g., when requesting HTML, server can simultaneously push CSS and JS files without waiting for browser to parse HTML first)

Multiplexing: HTTP/2's Core Innovation

Multiplexing is HTTP/2's most important innovation, solving HTTP/1.x's head-of-line blocking problem:

HTTP/1.1 vs HTTP/2 Request Processing Comparison

HTTP/1.1:
Time →
Connection1: [---RequestA---][---RequestC---][---RequestE---]
Connection2: [---RequestB---][---RequestD---][---RequestF---]
      (Requires multiple TCP connections for parallel processing, requests serial within each connection)

HTTP/2:
Time →
Single connection: [--A--][--B--][--C--][--D--][--E--][--F--]
          (Multiple requests processed simultaneously within single connection, no need to wait for previous requests)

HTTP/2 multiplexing advantages, using Chrome browser as example:

  • 1. Open Chrome DevTools, you'll see HTTP/2 websites use only one connection to download multiple files simultaneously, no more queuing
  • 2. Web pages load faster because Chrome doesn't need to repeatedly establish multiple connections (connection count reduction clearly visible in Network panel)
  • 3. Even on 4G networks, pages display faster because resource downloads no longer block each other
  • 4. Developers no longer need to distribute resources across multiple subdomains (like static1.example.com, static2.example.com) to increase connection count
  • 5. When you open many tabs, Chrome uses fewer system resources because the number of maintained connections is greatly reduced

Header Compression Significantly Reduces Transmission Volume

Imagine sending 10 letters to the same friend every day, each time having to repeat the same sender and recipient addresses. HTTP/1.1 is like this, each request must repeat sending the same header information:

// HTTP/1.1 Each request like filling out complete form again
GET /homepage.html HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (...)
Accept: text/html,application/xhtml+xml,...
Cookie: userID=123; loginStatus=loggedIn
// Total transmission: ~500 bytes

GET /image.jpg HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (...)
Accept: text/html,application/xhtml+xml,...
Cookie: userID=123; loginStatus=loggedIn
// Repeat transmission: ~500 bytes

While HTTP/2 is like a smart mail carrier:

// HTTP/2 Only need to inform of changes
[First request]
Table index=1: example.com
Table index=2: Mozilla/5.0 (...)
Table index=3: userID=123; loginStatus=loggedIn
Request: /homepage.html
// Total transmission: ~500 bytes

[Subsequent requests]
Use table index 1,2,3
Request: /image.jpg
// Only need to transmit: ~20 bytes

Actual effect: Open Chrome DevTools, you'll find HTTP/2 website request headers are only about 20% of HTTP/1.1 size. A webpage with 50 requests might save over 20KB of data transmission, significantly speeding up page loading on mobile networks.

Server Push: Delivering Resources Early

HTTP/2 server push is like a restaurant waiter not only bringing your ordered main course, but also proactively bringing you drinks and dessert:

Traditional HTTP/1.1 vs HTTP/2 Server Push

HTTP/1.1 (Order Mode)

👨‍💻 Client: I want a homepage

🍽️ Waiter: OK, here's the homepage

👨‍💻 Client: (After seeing homepage) I also need stylesheet

🍽️ Waiter: OK, here's the stylesheet

👨‍💻 Client: I also need images and JS

🍽️ Waiter: OK, here are images and JS

⏱️ Requires multiple round trips, wastes time

HTTP/2 Server Push (Thoughtful Mode)

👨‍💻 Client: I want a homepage

🍽️ Waiter: Here's the homepage, I guess you also need stylesheet, common images and JS, brought them all for you!

👨‍💻 Client: Great! I was just needing these

⏱️ Get all needed resources in one round trip

Visual Comparison

HTTP/1.1

Request HTML

Receive HTML

Request CSS

Receive CSS

Request JS

Receive JS

Request Images

Receive Images

HTTP/2 Server Push

Request HTML

Receive HTML

+ Receive CSS (Push)

+ Receive JS (Push)

+ Receive Images (Push)

Page renders immediately

Server Push is suitable for these scenarios:

  • 1. Homepage essential CSS and JS (like restaurant knowing main course definitely needs utensils)
  • 2. Images that almost all users will see (like hotel knowing guests definitely need water)
  • 3. Next page user is likely to click immediately (like waiter predicting you might want dessert)

Although Server Push sounds great, there are some minor issues in actual use, such as possibly pushing resources the user already has, or pushing things the user doesn't need, like a waiter bringing you drinks you already have, or dessert you don't want to eat.

Binary Protocol Improves Parsing Efficiency

HTTP/1.x uses plain text format, like writing letters; while HTTP/2 adopts binary format, more like telegraph transmission:

HTTP/1.1 (Text Format)

GET /index.html HTTP/1.1

Host: example.com

User-Agent: Mozilla/5.0

Accept: text/html

...

↓ Human readable, but machine parsing slower

HTTP/2 (Binary Format)

01000111 01000101 01010100

00101111 01101001 01101110

01100100 01100101 01111000

00101111 01101000 01110100

...

↓ Machine efficient parsing, faster transmission

Imagine: HTTP/1.1 is like letters written in natural language, mail carriers need to read the entire letter to know where to deliver; while HTTP/2 is like packages marked with barcodes, scanners can process immediately with one scan.

Binary protocol advantages are like upgrading from handwritten letters to email:

  • 1. More efficient parsing - like scanning barcodes is much faster than reading addresses
  • 2. More compact - like compressed files take up less space than original files
  • 3. More reliable error detection - like tracking numbers on packages, knowing how big each part should be
  • 4. Easier to implement multiplexing - like multi-lane highways, different vehicles can advance simultaneously

🔥 Common Interview Questions

(1) What are the main differences between HTTP/1.1 and HTTP/2?

Answer: HTTP/2 has five major improvements over HTTP/1.1, understand with real-life examples:

1. Multiplexing

HTTP/1.1

One road only one car can pass
---------------------→
  🚗
---------------------→
       🚚
---------------------→
            🚌

HTTP/2

One road multiple lanes simultaneous traffic
---------------------→
  🚗    🚚    🚌
=====================→

Single connection can handle multiple requests simultaneously, no need to queue and wait

2. Header Compression

HTTP/1.1: Each request sends complete header information, like rewriting complete address each time you send a letter

HTTP/2: Use HPACK algorithm to compress header information, like after building contacts list, just say 'send to contact #1'

Significantly reduce redundant data transmission volume

3. Binary Protocol

HTTP/1.1: Text format, like manually read letters

HTTP/2: Binary format, like machine scanned barcodes

Faster parsing, more space efficient, less error prone

4. Server Push

HTTP/1.1

Client: I want HTML
Server: Here's HTML
Client: I also want CSS
Server: Here's CSS
Client: I also want JS
Server: Here's JS

HTTP/2

Client: I want HTML
Server: Here's HTML
        Also giving you CSS and JS
        (I know you'll need them)

Server can actively push related resources, reducing wait time

5. Stream Priority

Like hospital emergency triage: important resources (like CSS) can be processed first, secondary resources (like some images) can be processed later

Let critical resources load first, improve user experience

(2) Why do we need to use HTTP/2?

Answer: Five reasons modern websites need HTTP/2:

  1. 1. Modern website complexity explosion - Average web pages grew from about 100KB in 2010 to over 2MB now, resource count increased from dozens to hundreds. HTTP/1.1 designed in 1997 cannot efficiently handle this complexity.
  2. 2. Mobile device proliferation - Mobile networks have high latency and instability, HTTP/2's multiplexing and header compression can greatly improve mobile experience.
  3. 3. Development efficiency improvement - No longer need many optimizations for HTTP/1.1 (like domain sharding, CSS/JS merging, image merging, etc.), simplifying development workflow.
  4. 4. Page loading speed is key metric - Research shows that for every 1 second increase in page loading time, conversion rate drops by 7%. HTTP/2 can improve page loading speed by 15-50% on average.
  5. 5. No need to change application code - Upgrading to HTTP/2 usually only requires server-side configuration, client code doesn't need major changes, making it a very cost-effective performance optimization method.