Load Balancing your web application with HAProxy & Varnish

Sudhindra Sajjal
Aug 24, 2017 · 3 min read

I was recently working on a personal project to build an end-to-end solution for hosting MediaWiki. Though I won’t be describing the entire architecture in one post, I wanted to share my approach on how I set up the front end to ensure a much robust, reliable and a much quicker content delivery system.

I used HAProxy (ver 1.7) which is a reverse-proxy load balancer and Varnish (ver 4.0.4) which is a reverse-proxy cache. Both these tools are open source and offer great outcomes when it comes to performance, scaling, and resilience. These two softwares go really well when used together thus making your web application more robust and agile. Both of these softwares are easy to configure and scale as per your requirements.

My architecture is really simple, I have two HAProxy servers running in master-slave mode receiving requests from clients and load balancing them to a set of servers in the backend in a round robin fashion. HAProxy performs really well when subjected to high traffic, so two were more than sufficient to accomplish the job. Then comes the caching layer which consists of two Varnish servers for caching static web content like images, gifs, css, js files, etc. This ensures quick delivery of web content and saves making calls to the web server asking for content every time we need static content to be delivered. Next comes the web server layer. I have three Nginx web servers which serve dynamic content or web content which is not present in Varnish yet.

When a client makes a request to one of the HAProxy servers, the HAProxy queries the Varnish for the web content, if we get a cache-hit in one of the Varnish boxes, we serve the content directly, else Varnish sends a cache-miss signal back to HAProxy after which the HAProxy server queries one of the web servers in a round robin fashion and fetches the content from there. The content then goes through Varnish where a copy of the results are cached and then delivered to the client.

The HAProxy config is as follows:

Here HAProxy acts as a Layer 7 router, the first line of defence against DDoS attacks (synflow, slowloris, etc) and also optimises the flow of the entire application.

The haproxy_stats dashboard looks something like this in the end.

Next, we configure the Varnish servers as follows:

This was one of the modules I built for a larger project i.e., end-to-end hosting and monitoring for MediaWiki. I’ll be sharing my database, monitoring and alerting infrastructure in my future posts. Hope you all found the post useful.

Cheers!!

(Sources: HAProxy blog, Varnish docs)

)

Sudhindra Sajjal

Written by

Site Reliability Engineer at Directi | CS Undergrad from BITS-Goa | Travel | Beaches | Dogs

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade