DEVOPS, SRE AND CLOUD · PART 2 · NETWORKING FOR DEVOPS · HTTP · 37 OF 187

HTTP/1.1 vs HTTP/2 vs HTTP/3 for DevOps

HTTP as you operate it: a raw HTTP/1.1 request typed by hand, methods, status codes and headers, keep-alive measured (a new HTTPS connection per request cost 1.33 to 1.82 ms here, a reused one 0.26 to 0.35 ms), HTTP/2 multiplexing negotiated by ALPN, HTTP/3 over QUIC, curl -w timings that split DNS, connect, TLS and first byte, and what each version means for proxies.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Text requests
  • status codes
  • keep-alive: 1.8 → 0.3 ms
  • HTTP/2 streams via ALPN
  • HTTP/3 on QUIC
  • curl -w

Notes

By hand

HTTP one point one is plain text, so we typed it with netcat. The request line: method GET, path /api /health, version. Then headers: Host, which lets one server host many sites, and Connection close. A blank line ends the headers. ShopRung answered with a status line, 200 OK, headers for the content type and length, a blank line and the JSON body, fifty bytes: status UP. Every HTTP library is building and parsing exactly this.

printf ... | nc 127.0.0.1 8080
GET /api/health HTTP/1.1
Host: shoprung.test
Connection: close

HTTP/1.1 200 OK
Content-Type: application/
  json; charset=utf-8
Content-Length: 50
{"status":"UP","app":
  "ShopRung",...}

Status codes

Status codes group by first digit. Two hundreds mean success. Three hundreds redirect: 301 permanent, three oh four means use your cached copy. Four hundreds blame the client: four oh one is not authenticated, four oh three is not allowed, four two nine is rate limited. Five hundreds blame the server. For operators, five oh two, five oh three and five oh four are the important ones: a proxy could not reach, found no healthy, or timed out waiting for the app behind it.

Know these
  • 2xx ok: 200, 201, 204
  • 3xx go elsewhere: 301, 302, 304 not modified
  • 4xx client problem: 400, 401 who are you, 403 no, 404, 429 slow down 5xx server problem: 500, 502 bad gateway, 503 unavailable, 504 timeout

Keep-alive

HTTP one point one made connections persistent by default: keep alive. We measured fifty requests opening a new connection each time versus reusing one. Over plain HTTP, the new connection version cost about point three milliseconds a request, reused about point one to point two. Over HTTPS through nginx, a new connection cost 1.3 to one point eight milliseconds, reused under point four. That is on localhost; across a real network each handshake adds full round trips.

50 requests, ms per request
               new   reused
HTTP  :8080   0.28-0.30
                    0.12-0.22
HTTPS :8443   1.33-1.82
                    0.26-0.35

localhost, 3 runs each
real networks add a round
  trip per handshake

HTTP/2

HTTP one point one has a limit: one request at a time per connection, so browsers opened about six connections per site. HTTP two keeps the same methods, headers and status codes, but sends them as binary frames, and multiplexes many requests as independent streams on one connection. Headers are compressed. gRPC, common between microservices, is built on HTTP two. But it still runs on TCP, so one lost packet holds up every stream behind it.

What HTTP/2 changed
  • HTTP/1.1: one request at a time per connection → browsers opened 6
  • HTTP/2: binary frames, many streams at once on ONE connection
  • header compression (HPACK)
  • gRPC is built on HTTP/2
  • still TCP underneath

Negotiation

How do client and server agree on HTTP two? During the TLS handshake, using an extension called ALPN. Our curl offered h two and HTTP one point one, and nginx accepted h two. On nginx one point twenty four that took one word in the listen line; newer versions use a separate http two on directive, which our older nginx rejected as an unknown directive, a real config migration gotcha.

curl -v --http2 (ours)
ALPN: curl offers h2,
  http/1.1
SSL connection using TLSv1.3
ALPN: server accepted h2
using HTTP/2

nginx: listen 8443 ssl http2
  (nginx 1.25+: http2 on;)

HTTP/3

HTTP three replaces TCP with QUIC, which runs over UDP port 443. QUIC includes TLS 1.3, so a secure connection is set up in one round trip, or zero when resuming. Loss on one stream does not block the others. Connections survive a change of IP address, like walking from Wi-Fi to mobile data. For operators: firewalls and load balancers must allow UDP 443. Our nginx build had no HTTP three module at all, so check your build.

What HTTP/3 changed
  • QUIC transport on UDP 443
  • TLS 1.3 built in: setup in one round trip (or zero on resume)
  • lost packet blocks only its own stream
  • survives IP change (Wi-Fi to mobile)
  • needs UDP 443 open

Time a request

When a request is slow, curl minus w splits it into phases. Our HTTPS request: name lookup in microseconds, TCP connect at seventy eight microseconds, TLS done at two and a half milliseconds, first byte at three point four, total three point four. The values are cumulative. Slow DNS points at the resolver, slow connect at the network, slow TLS at handshakes or certificates, and slow time to first byte at the application.

curl -w over HTTPS (ours)
dns      0.000009 s
connect  0.000078 s
tls      0.002463 s
ttfb     0.003386 s
total    0.003416 s

each value is cumulative
slow dns/connect/tls/ttfb
  each points somewhere else

Common mistakes

Mistakes. No keep alive between a proxy and the application, so every request pays a new handshake. Treating all five hundreds alike, when five oh two, five oh three and five oh four each point somewhere different. Retrying POST requests that are not safe to repeat. Blocking UDP 443 and then wondering why HTTP three never activates. And health checks that omit the Host header and hit the wrong virtual host.

Avoid these
  • no keep-alive between proxy and app
  • treating every 5xx the same
  • retrying non-idempotent POSTs blindly
  • blocking UDP 443, then wondering about HTTP/3
  • missing Host header in health checks

Say it like this

In the interview: HTTP one point one is text with persistent connections but one request at a time per connection. HTTP two is binary and multiplexes many streams over one TCP connection, negotiated with ALPN, but TCP loss still blocks all streams. HTTP three runs over QUIC on UDP with TLS 1.3 built in, so setup is faster and loss only affects one stream.

In the interview
  • "1.1 is text with keep-alive but one request at a time per connection. 2 is binary and multiplexes streams on one TCP
  • connection, chosen by ALPN. 3
  • runs on QUIC over UDP with TLS
  • built in, so loss only blocks
  • its own stream."

Interview questions

Q1What is the difference between HTTP/1.1, HTTP/2 and HTTP/3?

1.1: text, one request at a time per connection. 2: binary frames multiplexed on one TCP connection. 3: the same semantics over QUIC (UDP) with built-in TLS 1.3.

Q2What do 502, 503 and 504 mean?

502: the proxy got an invalid or no response from upstream. 503: service unavailable (no healthy backends, overload). 504: the proxy timed out waiting for upstream.

Q3What is HTTP keep-alive?

Reusing one TCP (and TLS) connection for many requests, avoiding a new handshake, slow start and TIME-WAIT socket for each request.

Q4What is ALPN?

Application-Layer Protocol Negotiation: a TLS extension where client and server agree on h2 or http/1.1 during the handshake.

Q5What is head-of-line blocking?

One delayed item holding up others behind it: HTTP/1.1 requests on a connection, or all HTTP/2 streams when a TCP packet is lost; QUIC removes the TCP part.

Q6How do you find which phase of a request is slow?

curl -w with time_namelookup, time_connect, time_appconnect, time_starttransfer and time_total separates DNS, TCP, TLS, server and transfer time.

Q7Which HTTP methods are idempotent?

GET, HEAD, PUT, DELETE and OPTIONS are idempotent; POST and PATCH are not, so they should not be retried blindly by proxies or clients.