In the latest post to the Dutch Web Alliance blog Bas de Nooijer looks at a handy use of ESI in Varnish as a part of the output for a REST API. ESI (Edge Side Includes) let you handle the caching and reuse of fragments of the output from your application and define different caching rules/techniques for each.
For a project I’m currently working on we are implementing a REST API. Performance for this API is critical, so amongst some other solutions Varnish is used. This was done with minimal effort, as the API already had the correct caching headers. There was a noticeable improvement, however I had the feeling this could be improved much more. I realised I can basically compare our REST API to any website, it uses HTTP in the same way. The main diffence is the content being JSON instead of HTML. Just like most websites, our API composes content in many different ways, for instance ‘standalone’ resources, collections of resources or embedded resources. In a website this would be a clear use case for ESI, so why not use it in our API?
He starts off by asking the question "why use ESI in an API" and compares the ESI method to the more traditional caching rules and handling. He includes two diagrams showing the flow of each handling type along with a brief description of how it would extract and combine the data. With the background out of the way, he gets into the code. He shows how to enable JSON caching in Varnish and the JSON tag information you'll need to include to tell Varnish to do its job.