Modern HTTP Implementation Experiments
Modern HTTP Implementation Experiments
Try It Yourself
Experiment 1: HTTP/2 Multiplexing Benchmark
Compare HTTP/1.1 sequential requests versus HTTP/2 parallel streams:
# Install httpx: pip install httpx
import httpx
import time
# HTTP/1.1 sequential
start = time.time()
with httpx.Client() as client:
for i in range(50):
client.get("https://httpbin.org/delay/0.1")
http1_time = time.time() - start
# HTTP/2 parallel
start = time.time()
with httpx.Client(http2=True) as client:
import asyncio
async def fetch_all():
async with httpx.AsyncClient(http2=True) as async_client:
tasks = [async_client.get("https://httpbin.org/delay/0.1") for _ in range(50)]
await asyncio.gather(*tasks)
asyncio.run(fetch_all())
http2_time = time.time() - start
print(f"HTTP/1.1: {http1_time:2f}s")
print(f"HTTP/2: {http2_time:2f}s")
print(f"Speedup: {http1_time/http2_time:1f}x")What to Observe:
Read these points as one connected sequence: compare measured elapsed time for the same 50 requests under sequential and parallel HTTP/1.1 and multiplexed HTTP/2; then account for connection reuse and loss behavior.
- Compare the chapter’s sequential HTTP/1.1 and concurrent HTTP/2 assumptions; a 10–50× result is a lab scenario, not a general expectation
- Single TCP connection vs 6-8 parallel connections in HTTP/1.1
- Lower latency variance with HTTP/2 multiplexing
Experiment 2: HPACK Header Compression
Measure header compression savings:
import httpx
# Capture traffic with Wireshark filtering "http2"
client = httpx.Client(http2=True)
# First request - headers sent in full
r1 = client.get("https://httpbin.org/headers",
headers={"User-Agent": "IoTDevice/1.0",
"Authorization": "Bearer token123..."})
# Subsequent requests - headers compressed via HPACK
for i in range(10):
r = client.get("https://httpbin.org/headers",
headers={"User-Agent": "IoTDevice/1.0",
"Authorization": "Bearer token123..."})What to Observe:
Read these points as one connected sequence: start with an illustrative first request: full headers (~350 bytes); then Assumed subsequent requests: compressed to ~15-30 bytes (90%+ reduction); and finish with Total bandwidth savings over 100 requests.
- First request: full headers (~350 bytes)
- In this header-size example, subsequent requests are assumed to compress to ~15-30 bytes (90%+ less than the assumed ~350-byte first request).
- Total bandwidth savings over 100 requests
Experiment 3: HTTP/3 vs HTTP/2 Latency
Compare connection establishment latency:
# HTTP/2 over TCP
time curl -I --http2 https://cloudflare-quic.com
# HTTP/3 over QUIC (requires curl 7.66+)
time curl -I --http3 https://cloudflare-quic.comWhat to Observe:
Read these points as one connected sequence: start with HTTP/2: 2-3 RTT connection setup (TCP + TLS); then HTTP/3: 0-1 RTT with QUIC; and finish with Connection resumption: 0-RTT for HTTP/3.
- HTTP/2: 2-3 RTT connection setup (TCP + TLS)
- HTTP/3: 0-1 RTT with QUIC
- Connection resumption: 0-RTT for HTTP/3
Challenge: Gateway Data Aggregation
Build an IoT gateway that collects from 20 simulated sensors and uploads to a cloud API:
import httpx
import asyncio
import time
async def simulate_sensor(sensor_id):
"""Simulate sensor reading"""
await asyncio.sleep(0.1)
return {"sensor_id": sensor_id, "value": 23.5}
async def gateway_http1():
"""HTTP/1.1 approach - sequential uploads"""
readings = await asyncio.gather(*[simulate_sensor(i) for i in range(20)])
with httpx.Client() as client:
for reading in readings:
client.post("https://httpbin.org/post", json=reading)
async def gateway_http2():
"""HTTP/2 approach - parallel multiplexed uploads"""
readings = await asyncio.gather(*[simulate_sensor(i) for i in range(20)])
async with httpx.AsyncClient(http2=True) as client:
tasks = [client.post("https://httpbin.org/post", json=r) for r in readings]
await asyncio.gather(*tasks)
# Measure and compareExpected Results:
Under the stated sequential-versus-concurrent lab assumptions, read these points as one connected sequence: start with HTTP/1.1: ~2-3 seconds (sequential uploads); then HTTP/2: ~200-300 ms (parallel over single connection); and finish with 10x throughput improvement for gateway scenario.
- HTTP/1.1: ~2-3 seconds (sequential uploads)
- HTTP/2: ~200-300 ms (parallel over single connection)
- About 10× less elapsed time under the stated sequential-versus-concurrent lab assumptions
Lab Exercise Ideas:
Read these points as one connected sequence: start with HTTP/2 Multiplexing Demo: Use curl --http2 with timing to compare 10 sequential vs. parallel requests; then Header Compression Measurement: Capture HTTP/1.1 vs HTTP/2 traffic with Wireshark to measure header size reduction; and finish with 0-RTT Latency Test: Configure a QUIC server and measure first-request latency for new vs. resumed connections.
- HTTP/2 Multiplexing Demo: Use
curl --http2with timing to compare 10 sequential vs. parallel requests - Header Compression Measurement: Capture HTTP/1.1 vs HTTP/2 traffic with Wireshark to measure header size reduction
- 0-RTT Latency Test: Configure a QUIC server and measure first-request latency for new vs. resumed connections
Discussion Questions:
Read these points as one connected sequence: start with Why might an enterprise prefer HTTP/2 over HTTP/3 even when UDP is available?; then How does QUIC’s connection migration feature change the design of mobile IoT applications?; and finish with What are the security implications of 0-RTT early data?.
- Why might an enterprise prefer HTTP/2 over HTTP/3 even when UDP is available?
- How does QUIC’s connection migration feature change the design of mobile IoT applications?
- What are the security implications of 0-RTT early data?
Common Student Misconceptions:
Read these points as one connected sequence: start with “HTTP/3 is always faster” - Not true; on stable networks with low loss, HTTP/2 performs similarly; then “HTTP/2 needs multiple connections” - The whole point is single connection with multiplexing; and finish with “Modern HTTP works on any device” - Memory constraints often make it impractical for constrained sensors.
- “HTTP/3 is always faster” - Not true; on stable networks with low loss, HTTP/2 performs similarly
- “HTTP/2 needs multiple connections” - The whole point is single connection with multiplexing
- “Modern HTTP works on any device” - Memory constraints often make it impractical for constrained sensors
Test the claim before standardizing on HTTP/3: start an upload on Wi-Fi, roam to cellular mid-stream, and verify that the same logical request survives under one QUIC Connection ID without restarting application work.
QUIC may rotate Connection IDs during a connection; continuity of the connection and logical request is the acceptance criterion, not an unchanged Connection ID value.
Checkpoint: implementation contract
You now know:
- A production HTTP/2 gateway needs a reusable client session, bounded connection pools, and long-lived keep-alive rather than one request per connection.
- Server settings such as
http2_max_concurrent_streams 128,http2_recv_buffer_size 256k, andkeepalive_timeout 3600smake the gateway pattern explicit. - Fleet tracking is the case where HTTP/3 features such as connection migration, 0-RTT resumption, and independent streams are easiest to justify.
Checkpoint: deployment tradeoffs
You now know:
- HTTP/2 wins when tooling, firewall traversal and a smaller (typically ~32 KB) footprint matter more than handoff.
- HTTP/3 wins when 0-RTT resumption, independent streams and migration justify its larger footprint.
- A staged deployment starts with HTTP/2, then upgrades the mobile or lossy slice after measurements prove the complexity is worthwhile.