Software engineer focused on Open source, infrastructure and testing.

Openshift: Survive to slashdot effect with Varnish

The problem.

A few days ago after adding my website to the Planet Python i got newbilly slashdotted. As i already posted about on OpenShift: First approach i am running this website on OpenShift and this is a quick guide so you do not make the same mistake.

The solution

If you can enable auto-scaling feature on OpenShift then a HAproxy will be automatically enabled on your gears. Using the default provided configuration by Red Hat you will be able to handle at max 256 concurrently openned connections on HAproxy. On the case of your Node.js gears if you consider that a single / request takes about ~200MB you will not be able to handle a big amount of traffic , also note that on thefree tier of OpenShift you will only have 3 small gears with about 1 GiB RAM on each one. So is time for cache requests and prevent them to execute Node.js. Here Varnish comes to play. The desired setup is something like:

Varnish is an awesome HTTP cache software, made by Poul-Henning Kamp it offers an embedded DSL language called VCL that allows you to modify the HTTP request (header, body), the TTL of the cached object, among other cool hacks.

The first step was to enable auto-scaling to a maximum of 2 ( remember: 3 available for free) using the following command:

The third and final step is to clone your application's repo and modify the default.vcl file to proxy your original HAproxy address. Mine configuration looks like (also available on this Github gist):

Please not that you have to override the req.http.host because HAproxy is used for virtual-host and not for direct hostnames. Also note that this setup is ready for ghost blog engine , so adapt it according to your application routes.