§2.2.5–2.2.6Web Caching · HTTP/2

Application layer Kurose & Ross pp. 108–115 · ~22 min read

  • web cache
  • origin server
  • hit rate
  • conditional get
  • head-of-line blocking
  • http/2 frame
  • framing sub-layer
  • server push
  • message prioritization

Where you are

  • Application layer you are here
  • Transport layer
  • Network layer
  • Link layer
  • Physical layer

A cache answers requests without letting them leave the building, and HTTP/2 stops one large object from making every small object behind it wait.

Words you will meet

  • Web cache — a nearby machine holding copies of objects; also called a proxy server.
  • Origin server — the machine that really owns an object.
  • Hit rate — the share of requests a cache answers from its own copies.
  • Conditional GET — a request meaning “send this only if it changed since this date”.
  • Head-of-line blocking — one large item at the front of a queue makes everything behind it wait.
  • HTTP/2 frame — a small piece of an HTTP/2 message. Not the link-layer frame of §1.5.
  • Framing sub-layer — the part of HTTP/2 that cuts messages into HTTP/2 frames and puts them back together.
  • Server push — the server sends objects it knows you will need, before you ask.

Why this matters

Section 2.2.2 showed that almost all the time in a page load is spent waiting. These two sections are the two answers to that.

Caching shortens the distance. It is the same idea that section 2.6.3 scales up into content distribution networks, which carry most of the video on the Internet.

HTTP/2 changes the order things are sent in. It is also where you meet head-of-line blocking, a problem that returns in §3.7 and again inside routers in §4.2.

A warning about one word

HTTP/2 uses the word frame for its small pieces. Section 1.5 already gave that word to the link-layer packet — the thing an Ethernet adapter puts on a wire.

They are completely different things at completely different layers. To keep them apart, this site always writes HTTP/2 frame and never the bare word on this page. It reads a little heavily, and that is the price of not confusing you in chapter 6.

Web caching

A web cache — also called a proxy server — is a network entity that satisfies HTTP (HyperText Transfer Protocol) requests on behalf of an origin web server . It has its own disk storage and keeps copies of recently requested objects there.

A browser can be configured so that all of its HTTP requests go to the cache first.

Figure 2.11 — a cache miss, in four steps
message 4 of 4
BrowserTimeWeb cacheproxy serverTimeOrigin serverwww.someschool.eduTimechecks its own disk — missstores a copy locallyGET /campus.gifGET /campus.gif200 OK + campus.gif200 OK + campus.gif

Click any arrow to see what that message says and why it is sent.

Watch which side of the cache each arrow touches. The same box is a server on the left and a client on the right.

Read this diagram as text
  1. Browser sends GET /campus.gif to Web cache. Step 1. The browser has been configured so that all its HTTP requests go to the cache first. It opens a TCP connection to the cache and asks for the object exactly as it would ask the origin server. Nothing about the request is special.
  2. Web cache sends GET /campus.gif to Origin server. Steps 2 and 3. The cache checked its own disk and did not have a copy. So it opens its own TCP connection to the origin server and sends its own request. Here the cache is a client.
  3. Origin server sends 200 OK + campus.gif to Web cache. The origin server answers. It has no idea it is talking to a cache rather than a browser, and does not need to.
  4. Web cache sends 200 OK + campus.gif to Browser. Step 4. The cache stores a copy on its own disk and sends a copy to the browser, over the connection those two already have. The next request for this object will not leave the building.

Lifelines, left to right: Browser (host), Web cache (middlebox), Origin server (server).

In plain words

Note what a cache is: both a server and a client at the same time.

When it receives requests from and sends responses to a browser, it is a server. When it sends requests to and receives responses from an origin server, it is a client. Section 2.1.2 said the label depends on who spoke first in a session, and here is a machine that proves it.

Caches are typically purchased and installed by an ISP (Internet Service Provider) . A university might install one on its campus network and point every campus browser at it. A large residential ISP might install several and preconfigure the browsers it ships.

Why caches were deployed

Two reasons, and the second is the one people forget.

  1. A cache can substantially reduce the response time for a request. That is especially true when the bottleneck link between client and origin server is much slower than the one between client and cache. If the cache sits on the same fast local network and holds the object, it delivers it almost at once.
  2. A cache can substantially reduce traffic on an institution’s access link. By reducing traffic, the institution does not have to upgrade its link as quickly, which reduces cost.

Caches also reduce web traffic in the Internet as a whole, which improves performance for everybody.

The example that makes the case

Figures 2.12 and 2.13 — the institution, with and without a cache
2 hops: PC → Switch → Institutional cache
Public InternetInstitutional network15 Mbps access link100 Mbps LANOrigin serverOrigin serverOrigin serverR-internetR-institutionSwitchPCPCPCInstitutional cache

Drag any device to rearrange the picture. Hover a link to see its rate, delay and length.

Play “cache miss” and “cache hit” and compare how far each one travels. The 15 Mbps access link is the whole problem.

Read this diagram as text
  • Origin server wired link R-internet (1 Gbps)
  • Origin server wired link R-internet (1 Gbps)
  • Origin server wired link R-internet (1 Gbps)
  • R-internet wired link R-institution (15 Mbps · 1 ms)
  • R-institution wired link Switch (100 Mbps)
  • Switch wired link PC (100 Mbps)
  • Switch wired link PC (100 Mbps)
  • Switch wired link PC (100 Mbps)
  • Switch wired link Institutional cache (100 Mbps)

The institutional network is a high-speed LAN (Local Area Network) . A router in it and a router in the Internet are joined by a 15 Mbps link. The origin servers are scattered around the globe. Suppose:

  • the average object size is 1 Mbit;
  • the average request rate from the institution’s browsers is 15 requests per second;
  • HTTP request messages are so small they create no traffic worth counting;
  • the time from the Internet-side router forwarding a request to receiving the response is 2 seconds on average. Call this the Internet delay.

The total response time is the LAN delay plus the access delay plus the Internet delay. Now use traffic intensity from section 1.4.2:

The two traffic intensities

On the 100 Mbps LAN:

15 requests/s×1 Mbit/request100 Mbps=0.15\frac{15\ \text{requests/s} \times 1\ \text{Mbit/request}}{100\ \text{Mbps}} = 0.15

On the 15 Mbps access link:

15 requests/s×1 Mbit/request15 Mbps=1\frac{15\ \text{requests/s} \times 1\ \text{Mbit/request}}{15\ \text{Mbps}} = 1

A traffic intensity of 0.15 on a LAN costs at most tens of milliseconds, so the LAN delay can be neglected. But as traffic intensity approaches 1, the delay on a link becomes very large and grows without bound.

The average response time will be on the order of minutes, if not more. That is unacceptable. Clearly something must be done.

Two ways to fix it

Option 1: buy a bigger link. Raise the access rate from 15 Mbps to 100 Mbps. Traffic intensity falls to 0.15 and the delay between the two routers becomes negligible. The total response time becomes roughly 2 seconds — the Internet delay, which no link upgrade can touch. It also means paying for a 100 Mbps link, which is a costly proposition.

Option 2: install a cache. Hit rates typically range from 0.2 to 0.7 in practice. Suppose 0.4 here. Then 40 % of requests are satisfied almost immediately — say within 10 milliseconds — by the cache on the same fast LAN. The other 60 % still go to the origin servers, so the access link now carries 60 % of the traffic and its intensity falls from 1.0 to 0.6. Below about 0.8, the delay is small. So:

0.4×(0.01 s)+0.6×(2.01 s)=1.21 s0.4 \times (0.01\ \text{s}) + 0.6 \times (2.01\ \text{s}) = 1.21\ \text{s}

What is the institution’s access link actually doing?

What each symbol means

  • arequest rate from the institution’s browsers (requests/s)
  • Laverage object size (bits)
  • Rlink rate of the access link (bits/s)
  • hhit rate — the fraction the cache answers itself ()

Read aloud: Only the requests the cache cannot answer cross the access link, so raising the hit rate lowers the traffic intensity in exact proportion.

Traffic intensity on the access link0.60

only the misses cross the access link: 60 % of requests
arrival rate on the link = 0.60 x 15 = 9.00 requests/s
intensity = 9.00 x 1,000,000 bits / 15.0 Mbps
          = 0.60
below 0.8 the queuing delay is small, tens of milliseconds

Average response time1.21 s

a hit is served from the cache over the LAN: 10.0 ms
a miss also crosses the access link and the Internet: 10.0 ms + 0.000 ns + 2.00 s = 2.01 s
average = 0.40 x 10.0 ms + 0.60 x 2.01 s
        = 4.00 ms + 1.21 s
        = 1.21 s

Change any number above and the arithmetic re-runs, carrying the units through.

The defaults are the book’s own example. Set the hit rate to 0 to see the institution before the cache is installed, then drag it up.

The cache beats the upgrade — and it is not close

1.21 s against roughly 2 s, and the institution does not have to buy a faster link at all. It buys a cache, and many caches run public-domain software on inexpensive machines.

The reason is worth stating plainly. Upgrading the link removes the queuing delay. The cache removes the queuing delay and removes the 2-second Internet delay entirely — for 40 % of requests. You cannot make a request to Frankfurt faster than the speed of light allows. You can avoid making it.

Through CDN (Content Distribution Network) s, web caches are increasingly important in the Internet. A CDN company installs many geographically distributed caches, which localises much of the traffic. There are shared CDNs such as Akamai and Limelight, and dedicated ones such as Google’s and Netflix’s. Section 2.6.3 covers them properly.

The conditional GET

Caching introduces a new problem: the copy in the cache may be stale. The object at the origin server may have been modified since the copy was taken.

HTTP’s answer is the conditional GET . A request is a conditional GET if it uses the GET method and includes an If-Modified-Since: header line.

The conditional GET — checking without fetching
message 4 of 4
Proxy cacheTimewww.exotiquecuisine.comTimeone week laterGET /fruit/kiwi.gif200 OK + kiwi.gifGET + If-modified-since304 Not Modified (empty body)

Click any arrow to see what that message says and why it is sent.

Four messages, a week apart. Click the last one: the reply carries no object at all, and that is the point.

Read this diagram as text
  1. Proxy cache sends GET /fruit/kiwi.gif to www.exotiquecuisine.com. An ordinary request, made on behalf of a browser.
  2. www.exotiquecuisine.com sends 200 OK + kiwi.gif to Proxy cache. The cache forwards the object to the browser and stores it locally. Importantly, it also stores the Last-Modified date that came with it. That date is what makes the next exchange possible.
  3. Proxy cache sends GET + If-modified-since to www.exotiquecuisine.com. One week later another browser asks for the same object, and the cache still has it. But it may be stale, so the cache performs an up-to-date check. The value of If-modified-since is exactly the Last-Modified value the server sent a week ago.
  4. www.exotiquecuisine.com sends 304 Not Modified (empty body) to Proxy cache. The object has not changed, so the server deliberately leaves it out. Including it would only waste capacity and increase the response time the user feels, particularly for a large object. The cache now forwards its own copy to the browser.

Lifelines, left to right: Proxy cache (middlebox), www.exotiquecuisine.com (server).

The value of If-modified-since: is exactly the value of the Last-Modified: line the server sent a week earlier. That is why section 2.2.3 called that header critical for caching.

In plain words

304 Not Modified is a response whose usefulness comes from what it does not carry. The server still replies, but leaves the object out. Sending it would only waste capacity and increase the response time the user feels — especially for a large object.

The status line tells the cache it may go ahead and forward its own copy.

HTTP/2

HTTP/2 was standardised in 2015. It was the first new version of HTTP since HTTP/1.1, which dates from 1997. It has taken off: over 40 % of the top 10 million websites supported it in 2020, and most browsers do too.

Its primary goals are to reduce perceived delay by multiplexing requests and responses over a single TCP (Transmission Control Protocol) connection. It also provides request prioritization and server push, and compresses HTTP header fields efficiently.

What HTTP/2 did not change

HTTP/2 does not change HTTP methods, status codes, URLs or header fields.

Everything in section 2.2.3 still holds exactly. What changed is how the data is formatted and transported between client and server. It is a change to the packaging, not to the message.

The problem: head-of-line blocking

Recall that HTTP/1.1 uses persistent connections, so a whole page can travel over one TCP connection. That has two benefits: the server keeps fewer sockets open, and each page gets a fair share of the link.

But browser developers quickly discovered a problem.

Everyday picture

One checkout lane at a supermarket. The person at the front has a full trolley. Behind them are eight people each holding one item. Everybody waits for the trolley.

Nothing is wrong with the lane, and nobody is being unfair. The order is simply bad for everyone except the person at the front.

Where the picture breaks: a shop can open a second lane, and a browser opening parallel connections is doing exactly that. But every new lane needs its own staff, which here means TCP buffers and variables at both ends. And as the box below explains, a browser that opens lanes also takes a bigger share of the shop.

Consider a page with an HTML (HyperText Markup Language) base file, a large video clip near the top, and many small objects below it. Suppose the path has a low-to-medium speed bottleneck link, such as a slow wireless link. Over a single connection the video clip takes a long time to cross that link, and the small objects are delayed behind it. That is head-of-line blocking .

HTTP/1.1 browsers work around it by opening multiple parallel TCP connections, so objects arrive in parallel and small ones render sooner.

The second motive, which is less innocent

TCP congestion control gives each connection sharing a bottleneck link roughly an equal share. So if n connections cross that link, each gets about 1/n of it.

By opening several parallel connections for one page, a browser can “cheat” and grab a larger portion of the link. Many HTTP/1.1 browsers open up to six parallel connections — not only to work around head-of-line blocking, but to obtain more capacity.

Section 2.1.4 said congestion control is a service for the general welfare of the Internet rather than for the two processes. Here is what happens when the two processes find a way around it.

So one primary goal of HTTP/2 is to reduce or remove parallel connections. That reduces sockets at servers and lets congestion control work as intended. But with only one connection, HTTP/2 needs a careful mechanism to avoid head-of-line blocking.

The solution: framing

The framing sub-layer breaks each message into small HTTP/2 frames and interleaves the request and response messages on the same TCP connection.

One connection, nine messages, two orderings
What has to be sentstep 1 of 3
The server has 9 responses to send down one TCP connectionvideo clip — 1000 HTTP/2 framesobj 1obj 2obj 3obj 4obj 5obj 6obj 7obj 88 small objects × 2 frames = 16Total: 1016 HTTP/2 frames either way. The question is what arrives first.

The browser asked for a page holding one large video clip and eight small objects, so the server has nine responses to send. Suppose every HTTP/2 frame is the same size: the video is 1000 of them, and each small object is 2. That is 1016 frames in total, whichever order they go in.

The book’s example: a video clip of 1000 HTTP/2 frames, and eight small objects of two each. Same bits, same link — only the order changes.

Read all steps as text
  1. What has to be sentThe browser asked for a page holding one large video clip and eight small objects, so the server has nine responses to send. Suppose every HTTP/2 frame is the same size: the video is 1000 of them, and each small object is 2. That is 1016 frames in total, whichever order they go in.
  2. Without interleaving: the video goes firstSend one message at a time and the video occupies the connection for 1000 HTTP/2 frames. The small objects sit behind it, complete and ready, waiting. The last of them is delivered at frame 1016. This is head-of-line blocking: one large item at the front of a queue makes everything behind it wait.
  3. With interleaving: take turnsThe framing sub-layer breaks each message into HTTP/2 frames and interleaves them. After one frame of the video clip, the first frames of each of the small objects are sent. Then after the second frame of the video clip, the last frames of each of the small objects are sent. All eight small objects are complete after 18 HTTP/2 frames.
Small objects delivered, against HTTP/2 frames sent
0.0002.04.06.08.00.0002204406608801100HTTP/2 frames sentsmall objects completeinterleaved: all 8 done at 18sequential: all 8 done at 1016

At 1100 (move the pointer over the plot to read it anywhere):

  • One message at a time:8.0
  • Interleaved (HTTP/2):8.0

Two orderings of the same 1016 frames. Drag the video length: the interleaved curve does not move at all, because it never waits for the video.

The book’s numbers

A page of one video clip and 8 smaller objects. The server receives 9 concurrent requests and must send 9 competing responses. Suppose all HTTP/2 frames are the same length, the video clip is 1000 of them, and each small object is 2.

  • Interleaved: all the smaller objects are sent after a total of 18 HTTP/2 frames.
  • Not interleaved: they would be sent only after 1016.

The framing mechanism can therefore significantly decrease the delay the user perceives.

When the server wants to send a response, the framing sub-layer breaks it up. The header field of the response becomes one HTTP/2 frame, and the body becomes one or more further ones. Those are interleaved with the pieces of other responses and sent over the single persistent TCP connection. At the client they are reassembled into the original messages, then processed by the browser as usual. A client’s requests are broken up and interleaved the same way.

The framing sub-layer also binary encodes what it sends. Binary protocols are more efficient to parse, give slightly smaller pieces, and are less error-prone.

In plain words

The book calls this “the single most important enhancement of HTTP/2”: the ability to break an HTTP message into independent pieces, interleave them, and reassemble them at the other end.

Notice that nothing was made faster. Exactly the same bits cross exactly the same link. Only the order changed — and that order was worth 998 HTTP/2 frames of waiting.

Prioritization and server push

Message prioritization lets developers customise the relative priority of requests. When a client sends concurrent requests it can assign each message a weight between 1 and 256, where higher means higher priority, and the server sends the highest-priority pieces first. A client can also state that one message depends on another, by giving the ID of the message it depends on.

Server push is the ability of a server to send several responses for a single client request. The HTML base page states which objects are needed to render the page. So the server need not wait for the browser to ask. It reads that page, works out what is needed, and sends those objects before any request for them arrives. That removes the delay of waiting for the requests.

HTTP/3

QUIC (Quick UDP Internet Connections) , covered in chapter 3, is a new transport-like protocol implemented in the application layer over bare UDP (User Datagram Protocol) . It has several features HTTP wants: message multiplexing, per-stream flow control, and connection setup with very little delay.

HTTP/3 is designed to run over QUIC. As of 2020 it was described in Internet drafts and not yet fully standardised. Many HTTP/2 features — message interleaving among them — are subsumed by QUIC, which allows a simpler, streamlined design for HTTP/3.

Check yourself

Check yourself

0 of 7 answered
  1. 1.A web cache receives a request for an object it does not hold. What roles does it play in the exchange that follows?

  2. 2.predictIn the institution of Figure 2.12, requests arrive at 15 per second, objects average 1 Mbit, and the access link is 15 Mbps. Set the hit rate to 0 in the calculator. What is the traffic intensity on the access link, and what does that mean?

  3. 3.The institution could upgrade its access link from 15 Mbps to 100 Mbps, or install a cache with a hit rate of 0.4. Which gives the lower average response time?

    Both remove the queuing problem. Compare what is left.

  4. 4.A conditional GET gets the reply `304 Not Modified` with an empty entity body. Why is a response carrying nothing useful?

  5. 5.A page holds one large video and eight small objects, sent over one connection. Why do the small objects arrive so much sooner when HTTP/2 interleaves the frames?

  6. 6.HTTP/1.1 browsers typically open up to six parallel TCP connections to one server. The section gives two reasons. What are they?

  7. 7.What did HTTP/2 change about methods, status codes, URLs and header fields?

What to remember

  • A web cache holds copies of objects near the users. It is a server to the browser and a client to the origin server, at the same time.
  • In the book’s example the access link runs at traffic intensity 1.00 without a cache, which means unbounded delay. A 0.4 hit rate drops it to 0.60 and the average response time to 1.21 s — better than upgrading the link, which would leave 2 s.
  • Head-of-line blocking is one large object delaying the small ones behind it. HTTP/1.1 browsers open up to six parallel connections to escape it. HTTP/2 instead breaks messages into HTTP/2 frames and interleaves them: 18 of them instead of 1016, over the same link.