Using Boltsort to Make API Caching More Efficient
Sorted
If you're caching (parts of) an API that is consumed by various developers, you might find that they all have different ideas about the order in which the arguments are placed in the query string. The following URLs should all be considered the exact same by your origin:
http://www.example.com/api/v1?action=search&sort=byname&query=foobar
http://www.example.com/api/v1?action=search&query=foobar&sort=byname
http://www.example.com/api/v1?query=foobar&action=search&sort=byname
http://www.example.com/api/v1?query=foobar&sort=byname&action=search
http://www.example.com/api/v1?sort=byname&query=foobar&action=search
http://www.example.com/api/v1?sort=byname&action=search&query=foobar
And that's with just 3 parameters!
There's a Fastly module available called "boltsort" which allows you to easily sort the parameters in the query string, so that you will always end up with the same URL, no matter what order the consumer of your API placed them in.
Using it couldn't be simpler:
# at the top of your VCL
import boltsort;
...
sub vcl_recv {
set req.url = boltsort.sort(req.url);
...
}
Of course not all variations will be used, but the amount of requests to your origin could decrease significantly.