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.

Rogier Mulhuijzen
Senior Professional Services Engineer
Published

1 min read

Want to continue the conversation?
Schedule time with an expert
Share this post
Rogier Mulhuijzen
Senior Professional Services Engineer

Rogier “Doc” Mulhuijzen is a senior professional services engineer and Varnish wizard at Fastly, where performance tuning and troubleshooting have formed the foundation of his 18-year career. When he’s not helping customers, he sits on the Varnish Governance Board, where he helps give direction and solve issues for the Varnish open source project. In his spare time, he likes to conquer all terrains by riding motorcycles, snowboarding, and sailing.

Ready to get started?

Get in touch or create an account.