Rest – URLs with variable query strings and cache

cachinghttprest

I am developing a media server, the basic functionality is to serve an image, which is done the following way

/media/:id

where :id is the id of the image.

You may want to ask for a specific size, with some rules I will provide.

Eg:

/media/:id/50x50
/media/:id?witdh=50&height=50

Question: Which option do you think is better? I believe the second one to be more RESTful and beautiful, but we will be using a CDN and varnish for cache, so if we get

/media/:id?width=50&height=50

or

/media/:id?height=50&width=50

we will have 2 hits when we should have 1

Any advice?

Best Answer

According to URI standard the path should contain the hierarchical components and the query should contain the non-hierarchical components of the URI. But it can be subjective what is hierarchical and what non-hierarchical.

By developing a REST client these URLs mean nothing, because they follow hyperlinks and check link relations or other additional meta-data. (aka uniform interface / HATEOAS constraint)

If you cannot cache one of them, then you should choose the other one. Note: you always have to send cache headers. (aka cache constraint)