HTTP Pattern: Alias

Published on August 21, 2017

A resource designed to provide a logical identifier but without being responsible for incurring the costs of transferring the representation.

Problem

Sometimes when working with large media files it is desirable to identify a the media resource within the same URI space as other resources related to the media resource, however for cost or performance reasons it is prefered to fetch the representation from an alternate location.

Solution

Through the use of the 307 Temporary Redirect status code and a 'Location' header with the actual location of the resource it is possible to resolve this issue.

GET /podcast/5/episode/22/media HTTP/1.1
Host: example.org
=>
HTTP/1.1 307 Temporary Redirect
Location: http://storage.mycdn.net/file/583245698

Discussion

This approach requires an extra network roundtrip in order to identify the true location of the resource. However, the assumption is that the target resource has a large representation and therefore the additional overhead should be minimal.

The location that is returned does not necessarily need to be the same for every request. The server could use a geo-location algorithm on the client IP address and provide a location URL that it believes will be physically close to the client.

It is even possible to provide different quality levels of media file based on particular details of the client. This is similar to what can be done with content negotiation however, with the additional capability to have the representation bytes come from a different resource, potential on a server on the other side of the world.

Being able to retrieve the bytes from a different location can reduce overhead when the alias resource is accessed via multiple intermediaries like load balancers, caching proxies and other pieces of management middleware.

If the redirect target of an Alias resource is not likely to change then it may be worth doing a 308 Permanent Redirect. This could allow the client to store the redirect URL locally and prevent the second round trip on future requests. Alternatively, the redirect response could simply contain a 'cache-control' header to indicate how long that alternate URL could remain valid for and allow a local HTTP cache to do all the work.