Sub topic 12
Range and conditional requests: partial content and revalidation
This chapter assumes you know HTTP methods, headers, and status codes, and that you have read the CDN caching chapter. It covers two features that let clients and caches avoid transferring bytes they don't need.
Range requests: asking for part of a file
A range request asks the server for a byte slice of a resource. Video players use it to seek. Download managers use it to resume an interrupted download. The server advertises support with Accept-Ranges: bytes.
The client sends a Range header:
GET /media/9b1c4e/clip.mp4 HTTP/1.1
Host: cdn.example.com
Range: bytes=1000000-1999999
HTTP/1.1 206 Partial Content
Content-Type: video/mp4
Content-Length: 1000000
Content-Range: bytes 1000000-1999999/48211034
Accept-Ranges: bytesNote the differences from a normal response:
- The status is
206 Partial Content, not200. Content-Rangegives the slice and the total size.Content-Lengthis the size of the slice, not of the whole file.
If the range lies outside the file, the server answers 416 Range Not Satisfiable. If the server ignores Range, it returns 200 with the full body. Clients must handle both outcomes.
S3 and most CDNs support ranges without extra configuration. If you serve files through your own application code, you must implement the header yourself, or seeking in video will not work.
Conditional requests: revalidation
A conditional request asks "send the body only if it changed." The client stores the ETag (a validator identifying one version of the content) or the Last-Modified date from an earlier response. It sends them back later:
GET /avatars/u42.jpg HTTP/1.1
Host: cdn.example.com
If-None-Match: "9b1c4e"
HTTP/1.1 304 Not Modified
ETag: "9b1c4e"
Cache-Control: public, max-age=300A 304 Not Modified response has no body. The cache keeps its stored copy and resets its freshness timer. This costs one round trip but no file transfer.
Revalidation matters when a URL is not content-addressed. Content-hashed URLs with immutable never need it, because the content at that URL never changes. For mutable URLs such as /avatars/u42.jpg, a short max-age plus an ETag keeps the cost of staleness low.
Combining the two with If-Range
Resuming a download is safe only if the file has not changed. Otherwise the client would stitch together bytes from two versions. If-Range solves this. The client sends the saved ETag with its Range header:
GET /media/report.pdf HTTP/1.1
Range: bytes=5000000-
If-Range: "v7"If the validator still matches, the server returns 206 with the remainder. If it doesn't, the server returns 200 with the whole new file.
Common errors
- Caching only full responses. Some CDNs fetch the whole object from the origin even for a ranged request, then slice it. Check whether your CDN does this for large video files.
- Missing
Content-Rangeor wrong length. Players may stall or refuse to seek. - Weak validators across servers. If two origin servers generate different ETags for the same file, revalidation fails and caches refetch needlessly. Derive ETags from content, not from file timestamps or inode numbers.
- Blocking
Rangein CORS. Cross-origin media requests need the response headersContent-RangeandAccept-Rangesexposed throughAccess-Control-Expose-Headersif your JavaScript reads them.
Exercise
A client sent Range: bytes=0-999 with If-Range: "v1". The file was replaced and its ETag is now "v2". What status does the server return, and what does the body contain?
Show answer
It returns 200 OK with the entire new file. The If-Range validator no longer matches, so the server ignores the Range header instead of sending a slice that might mix old and new content.
Exercise
Why does a content-hashed URL served with Cache-Control: public, max-age=31536000, immutable rarely produce 304 responses?
Show answer
Browsers treat the file as fresh for a year and never send a conditional request. Because the content at that URL never changes, there is nothing to revalidate.