Written by

Here is our sec­ond arti­cle ded­i­cat­ed to our new reverse-proxy engine and its awe­some fea­tures! After the Web Application Firewall, we now have a look to the HTTP cache built into our infrastructure.

punch it star trek GIF @Giphy

What is an HTTP cache?

A good blog post is a post with a chart

We have test­ed our WordPress blog per­for­mances, using our new HTTP cache built in our proxy. Here is the result, which let us bet that may like this new feature:

There’s a con­sid­er­able improve­ment of the num­ber of requests han­dled by the proxy when we enable the Cache. When we only serve 15 req/s with­out it, it increas­es to 2604 req/s. A 173 fac­tor, for the same frame. The response time is also improved, and fall to 0.38ms instead of 63.65ms approx­i­mate­ly. It’s inter­est­ing for a fea­ture effort­less to use!

We made this bench­mark using ApacheBench, by request­ing the blog home­page. We run each shoot1) four times, with and with­out Cache enabled, before com­pil­ing the results. Our blog stands on a ded­i­cat­ed serv­er, but we expect a sim­i­lar rate for shared host­ing instances. You can make the test your­self by con­nect­ing to your account using SSH and run­ning the ab com­mand using the same options on your website.

How does it work?

A Cache is a tem­po­rary stor­age which can serve the cached results when request­ed. An HTTP Cache is a cache which can store web pages and assets. It is pri­mar­i­ly used to decrease the charge of an upstream serv­er when it must serve an often request­ed page with­out any mod­i­fi­ca­tion between two requests.

When a client requests a page to a web serv­er, this one gen­er­ates an HTML response and send it to the client over the net­work. Before the response goes out­side of the infra­struc­ture, the HTTP Cache han­dles it and stores it in its mem­o­ry before to let it go.

Caching a resource (schema)
Caching a resource when a new request hap­pens (icons from The Noun Project)

When our proxy encoun­ters the same request, it asks the Cache for an avail­able ver­sion. If the page is avail­able in the Cache mem­o­ry, this one is served instead of ask­ing the upstream server.

Serving a cached resource (schema)
Serving pre­vi­ous­ly cached resource (icons from The Noun Project)

Use it at alwaysdata

If you want to use the HTTP Cache, you can enable it for any site indi­vid­u­al­ly in the Sites → Edit → Cache sec­tion. Tick the Enable the cache option.

Screenshot of the interface to enable cache for a web site

You must set the TTL for the pages served by this web­site. The TTL defines how many times the Cache retains the page before expir­ing it. You should choose it well. If we rec­om­mend a high TTL for a page that is not often mod­i­fied, you must reduce it for high­ly dynam­ic con­tent like a news web­site. If you set a too long dura­tion, your vis­i­tors may see an expired page instead.

For instance, we need that any vis­i­tor sees a new home­page. When we pub­lish a new arti­cle, the pre­vi­ous ver­sion of the home­page is then out­dat­ed. We then pre­fer to use a TTL between 5 and 10 sec­onds. This way, we ensure that we can use the high per­for­mances offered by the Cache with a rel­a­tive­ly low risk of serv­ing an old page.

This fea­ture is cur­rent­ly in beta test and may evolves dur­ing the next weeks.

This fea­ture need your appli­ca­tion or web­site to be able to autho­rize the Cache to han­dle the requests. If resources aren’t explic­it­ly marked as _cacheable_ by your app, there is a risk that our HTTP Cache can’t store them.

What’s behind the scene?

For tech­ni­cal peo­ple, here’s how we pro­ceed to enable the cache. We chose to write with Python2) a mod­ule that fol­lows the RFC 7234. A local Redis instance stores the cached resources. It allows us to man­age the mem­o­ry ded­i­cat­ed to the stor­age effortlessly.

We also chose to imple­ment the HTTP PURGE verb. This method allows you to remove a cached ver­sion of a resource by call­ing it on its URL. Yan can then force to refresh the Cache easily.

After the per­for­mance, we made a sig­nif­i­cant effort on log­ging! In our next and last blog post, we intro­duce you to the new log­ging sys­tem that allows you to store cus­tom for­mat­ted logs to allow you to debug your upstream appli­ca­tions effortlessly.


1 ab -c 10 -t 60 http://blog.alwaysdata.com/
2 cause we ♥ Python at alwaysdata