HTTP/2, HTTP/3, and CDN caching: a Python waterfall model
HTTP/2, HTTP/3, and CDN caching: a Python waterfall model
Search
Ask the AI

HTTP/2, HTTP/3, and CDN caching: a Python waterfall model

When a page loads slowly, check the network waterfall first. Is the time spent on DNS, connection setup, waiting for the first byte, or downloading resources? HTTP/2 multiplexing, HTTP/3, and CDN caching change different parts of that wait.

This article compares four combinations with the same model. With RTT fixed at 36 ms, the Python script produces 176 ms for HTTP/2 MISS, 77 ms for HTTP/2 HIT, 140 ms for HTTP/3 MISS, and 41 ms for HTTP/3 HIT. These are modeled times under the stated conditions. Packet loss, server processing, resource size, and browser rendering also affect a real page.

1. Which wait does each protocol change?

HTTP/2 multiplexes streams over one TCP connection. TCP delivers bytes in order. When some bytes are missing, later bytes wait for recovery before delivery to the application. Streams sharing that connection can therefore wait for the same retransmission: transport-level Head-of-Line (HoL) blocking.

HTTP/3 uses QUIC, which maintains byte ordering separately for each stream. Missing data in one stream does not prevent delivery of data already received on another. Streams still share connection congestion control, and HTTP dependencies can introduce other waits. The benefit depends on packet loss and request conditions. See RFC 9114, Section 2.

A CDN cache hit removes the origin fetch for that request. The diagram compares HIT and MISS paths over the same edge connection. Both paths still require communication between the client and the edge.

sequenceDiagram
    participant Client
    participant Edge as CDN Edge
    participant Origin
    Client->>Edge: Establish connection and send request
    alt Cache HIT
        Edge-->>Client: Return cached content
    else Cache MISS
        Edge->>Origin: Fetch content
        Origin-->>Edge: Return response
        Edge-->>Client: Return content and cache if eligible
    end

2. Compare protocol and cache changes separately

The model fixes client-to-edge RTT at 36 ms, a DNS miss at 42 ms, a DNS hit at 1 ms, and an edge cache read at 4 ms. The origin segment costs 36 + 26 = 62 ms. Connection setup costs two RTTs for HTTP/2 and one RTT for HTTP/3.

HTTP/2 MISS = DNS 42 + Connect 72 + Origin 62 = 176 ms
HTTP/2 HIT  = DNS  1 + Connect 72 + Edge    4 =  77 ms
HTTP/3 MISS = DNS 42 + Connect 36 + Origin 62 = 140 ms
HTTP/3 HIT  = DNS  1 + Connect 36 + Edge    4 =  41 ms

With the MISS condition held fixed, HTTP/3 saves 36 ms over HTTP/2. The difference is also 36 ms with HIT held fixed. Moving from HTTP/2 MISS to HTTP/3 HIT saves 135 ms, but changes DNS, handshake, and cache state together. That comparison needs all three changes to explain the result.

The HTTP/3 HIT case here includes a one-RTT handshake. Sending 0-RTT data requires prior session information, server acceptance, and handling of replay risks. Even accepted early data still requires network travel for the response. See RFC 9001, Section 4.6.

These totals add three script-defined budgets; they are not a complete wire timeline for cold-connection TTFB. In particular, HIT edge_ms includes only the 4 ms edge read, without a separate post-handshake request/first-byte network round trip. The 62 ms MISS segment is also an assigned response budget. To estimate real TTFB, distinguish client-to-edge and edge-to-origin RTTs, processing and overlapping flights, then compare the same boundaries.

Cache capacity and request distribution

Cache hit ratio depends on which objects receive requests, how many fit in the cache, and how TTLs and cache keys are configured. To model popularity with Zipf’s Law, let the object count be \( N \), the rank be \( k \), and the exponent be \( s \). The request probability for the object at rank \( k \) is:

$$ P_k = \frac{1/k^s}{\sum_{i=1}^{N} (1/i^s)} $$

Under this model, storing more of the popular objects covers more request probability. In practice, expiry, updates, and uncacheable responses also affect hit ratio. The latency script below assigns HIT or MISS to each row to compare request paths. It does not simulate eviction or estimate hit ratio.

3. Run the script and check the output

Download the network fundamentals lab and run this command from the extracted directory:

python src/http_cdn_waterfall.py

The script reads the scenario parameters and writes a CSV and a segmented bar chart. The default console output is:

HTTP/2 MISS: 176 ms; HTTP/3 HIT: 41 ms

All four rows are available in http-cdn-waterfall-results.csv. This independent calculation checks the difference between the two endpoints:

total_h2_miss = 42 + (36 * 2) + 36 + 26
total_h3_hit = 1 + 36 + 4
modeled_saving = total_h2_miss - total_h3_hit
print(f"Modeled saving: {modeled_saving} ms")  # 135 ms
The animation separates DNS, handshake, and edge or origin response time for the four modeled cases: 176, 77, 140, and 41 ms.

4. What to record during an investigation

Symptom Record first Comparison
Time to First Byte (TTFB) falls Protocol, cache status, Age, Cache-Control, and edge location. Hold protocol fixed and compare MISS with HIT for the same resource, then vary protocol separately.
An HTTP/3 connection is faster New or reused connection, session resumption, early-data acceptance, and handshake logs. Compare new and resumed connections with cache state held fixed.
A connection stalls after a network change Connection identifiers, path validation, backend routing, and reconnect logs. Check reachability on the new path and whether packets reach a backend holding the connection state.
TTFB is short but Largest Contentful Paint (LCP) is slow Critical images, blocking scripts, CSS, and main-thread tasks. Use the browser Performance panel to inspect download, parsing, and painting time.

A packet capture can show UDP packet timing, size, and connection identifiers. To inspect events within encrypted streams, combine the capture with qlog when the implementation provides it. Logging configuration depends on the QUIC library and build options; use the documentation for the installed version.

QUIC Connection IDs help identify a connection when endpoint addresses change. Migration also requires a reachable path, endpoint support, and correct backend routing. Connection identifiers can rotate during the connection. RFC 9000, Section 9 sets out migration and path-validation requirements.

5. Three cache configuration checks

  • Check cache keys.Vary identifies request headers that affect the response. Varying by User-Agent can increase the number of variants. Configure this according to actual content differences and the CDN’s support for that header. See RFC 9111, Section 4.1.
  • Check expiry behavior.A cache that supports stale-while-revalidate can return an expired response within the specified window while revalidating in the background. Choose the window according to freshness requirements. See RFC 5861, Section 3.
  • Check protocol availability and resource use.Measure HTTP/2 and HTTP/3 success rates, latency, and CPU use on the target networks. Record which connection path clients use when UDP is restricted.
Cache-Control: max-age=60, stale-while-revalidate=86400

This example allows a response with a fresh lifetime of 60 seconds to be served for up to 86400 seconds after expiry while revalidation occurs. Before applying it to account or private data, define shared-cache permissions and cache keys.

The retained curl -I --http3 command requires an HTTP/3-capable build, but --http3 permits fallback to another HTTP version. Also record the negotiated version, for example with -w "%{http_version}\n". A HEAD request can take a different cache path from GET, so compare the actual request method when diagnosing cache hits.

curl -I --http3 https://example.com/public-asset.css

Read the actual response headers. Cloudflare uses UPDATING when it serves expired content while updating it in the background, and STALE when it serves expired content because the origin is unavailable. A STALE response alone does not establish that asynchronous revalidation occurred. See the Cloudflare cache response definitions.

Continue reading

Leave a Reply

Scroll down