Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Learn › Networking › HTTP

HTTP/1.1, 2, 3

What differs between the versions when you fetch 30 files

Why it exists — As a single page grew to dozens of files, HTTP/1.1, which receives responses one after another, forced workarounds such as combining images into one or splitting content across several domains. HTTP/2 gives each request a numbered stream so that it no longer waits for the response before it, and HTTP/3 moved to QUIC over UDP so that streams are also handled separately at the transport level.

Where it's used today — The major browsers, CDNs, and nginx support HTTP/2, and HTTP/3 is also used by large sites and CDNs. Spring Boot turns on HTTP/2 with server.http2.enabled, but when nginx or a load balancer sits in front, it is common for that component to handle HTTP/2 and HTTP/3 with the browser and for the servers behind it to receive HTTP/1.1. gRPC, which servers use to call each other, also runs over HTTP/2.

A single web page is made of dozens of files (scripts, styles, images). HTTP/1.1 receives only one file at a time on a connection, so requests have to wait in line, while HTTP/2 mixes several requests on one connection. HTTP/3 changes even the transport layer below it.

Partly checked against real execution · The number of connections, the queuing, and the round trips of connection setup were run and checked with nginx 1.27 and Chromium, and HTTP/3 was really run. Packet loss was confirmed only with 6 files, and the times are assumed values

Concepts in this lab

HTTP/1.1, HTTP/2, and HTTP/3
HTTP/1.1 handles requests one at a time, in order, on a connection. HTTP/2 sends and receives several requests and responses at the same time over a single connection (multiplexing) and compresses headers. HTTP/3 uses the UDP-based QUIC protocol instead of TCP. This makes connection setup faster and reduces the problem where one lost packet also delays other requests.
TCP and UDP
The two main ways of transporting data on the internet. TCP establishes a connection before communication, guarantees that data arrives complete and in order, and sends data again if there is a problem. UDP sends data without a connection and does not guarantee arrival or order, but in return it is lightweight and fast. HTTP/1.1, HTTP/2, and database connections use TCP, and real-time video and voice and DNS mainly use UDP.
Three-way handshake
Why it exists — If data is sent right away without confirming the connection, it is sent without knowing whether the other side is ready to receive, or where to start counting the numbers attached to each piece. The three-way handshake lets both sides confirm each other's starting numbers in three exchanges, which sets the basis for counting missing pieces and order afterward.
Where it's used today — Developers do not call it directly, but the connection timeout is the limit on how long to wait for this process to finish. If a firewall silently drops the SYN, there is no reply and the wait lasts until this timeout, and if no program is waiting on the other side's port, Connection refused appears right away.
The three-step process used to establish a TCP connection. The client sends a connection request (SYN), the server sends its acceptance together with its own connection request (SYN+ACK), and the client acknowledges it (ACK). After both sides have confirmed that they can send to and receive from each other, data transfer starts.

Try these

Running with the default settings

  1. In HTTP/1.1, a connection has to receive the whole response to one request before it can send the next request. To go faster, the browser opens up to 6 connections to the same server. Each connection needs its own TCP and TLS handshakes.
  2. Batch #1: 6 connections receive one file each. The remaining 24 wait until a connection is free.
  3. Batch #2: 6 connections receive one file each. The remaining 18 wait until a connection is free.
  4. Batch #3: 6 connections receive one file each. The remaining 12 wait until a connection is free.
  5. Batch #4: 6 connections receive one file each. The remaining 6 wait until a connection is free.
  6. Batch #5: 6 connections receive one file each. No files are waiting now.
  7. All the files have been received. About 530ms in total.

Result of this run

Code

# application.yml
server.http2.enabled: false        # browsers use HTTP/2 only over TLS (https)

# nginx
server {
    listen 443 ssl;
    ...
}

<!-- Files requested by one page: 30 -->

Assumptions of this model

This is a model built from assumed times. It assumes that it takes 100ms to receive one file, 200ms to retransmit a lost packet, and, to set up the connection, 30ms for HTTP/1.1 and HTTP/2 (the 6 connections of HTTP/1.1 are opened at the same time) and 15ms for HTTP/3. The real numbers depend on the network and the file sizes. The ratio of the connection setup times (2 to 2 to 1), the number of connections, and the queuing were checked by really running them. nginx 1.27.5 (http2 on, listen quic) and Chromium 151 (run without a window) were put on a Docker network, a delay was added so that one round trip takes 500ms (tc netem), and 6, 30, and 60 files of 1000 bytes were requested at the same time. Setting up the connection took 2 round trips for HTTP/1.1 and HTTP/2 (TCP and TLS 1.3) and 1 for HTTP/3 (QUIC). HTTP/1.1 received the files 6 at a time on 6 connections (30 files in five batches, and 60 files in ten batches in one run and eleven in another), and HTTP/2 (6, 30, and 60 files) and HTTP/3 (6 and 30 files) sent every request on 1 connection without waiting. However, with HTTP/2 the responses for 30 and 60 files arrived split over two or three round trips because of the TCP congestion window, so they did not finish in one go as in this model. Packet loss was produced by dropping one large packet from the server on the browser side (iptables). With 6 files, it was confirmed twice each that with HTTP/2 all 6 files finished one round trip late and that with HTTP/3 only 1 file finished late. In the loss combinations with 30 and 60 files, the number of late files differed from run to run (5 and 9 with 30 files on HTTP/1.1), and the runs that fetched 60 files over HTTP/3 and some of the other HTTP/3 runs failed, so these combinations could not be confirmed by running. The parts that were not confirmed follow the descriptions in the standards (RFC 9112, 9113, 9114, and RFC 9000 for QUIC). The 6 HTTP/1.1 connections to the same server is the value seen in Chromium, and it is known as the default of the major browsers.

Previous lab: HTTP messages and status codes · Next lab: Cookies

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these