Programming Guy
Programming Guy

Reputation: 7441

Is this video being cached or not?

I'm wanting to ensure that videos I'm serving are being cached by the browser.

I've knocked up this test page : https://itype.online/videoCacheTest

I'm a bit puzzled about why I'm not seeing a 304 status in the Network tab for the video. Instead I see status 206.

Is my file being retrieved from the browser cache or is it coming from the server every time?

Thanks, Peter

Upvotes: 1

Views: 2139

Answers (1)

Mick
Mick

Reputation: 25471

The link you shared appears broken (there are some cloud availability issues at this time so maybe that is the problem) but its possible to test with other videos and similar behaviour will be seen.

Its worth looking at the most common ways of streaming a video:

  • Simple HTTP streaming of a single video file
  • streaming via a dedicated streaming protocol like HLS or MPEG DASH

For simple HTTP streaming, the video file is typically downloaded using byte range requests. In theory, so long as the cache can handle range requests then the cache should be ok to cache the responses - from the HTTP caching spec (RFC 2616 Fielding, et al):

A response received with a status code of 200, 203, 206, 300, 301 or 410 MAY be stored by a cache and used in reply to a subsequent request, subject to the expiration mechanism, unless a cache-control directive prohibits caching. However, a cache that does not support the Range and Content-Range headers MUST NOT cache 206 (Partial Content) responses.

In practice, it seems that many browsers still do not cache the 206 responses as you have seen. It is made more difficult to observe the behaviour sometimes as some browsers, to speed up download, may add a first request for the full video initially, which is then cancelled and replaced by range requests when the video length is known.

HLS and DASH introduce a further complexity - with ABR you create multiple bit rate versions of the video on the server, each broken into chunks. The player decides which resolution to download the next chunk of the video from based on a number of factors, typically including network conditions and device display size and capabilities.

So when HLS or DASH is being used, replaying a video will not necessarily download the same chunks every time as network conditions etc may have changed.

You can usually see on the browser inspector if a particular item is downloaded on the network or loaded from memory/cache - for example with Safari web inspector:

enter image description here

And for Chrome:

enter image description here

You could also use Wireshark is you want to be 100% sure - this will show the network traffic to and from your device: https://www.unified-streaming.com/academy

Upvotes: 1

Related Questions