How to use Cache Headers to speed up your website with Django and CloudFlare

Since I started working at bigger startups, like MinbilDinbil, I had to face the problem of providing pages to a larger amount of users every day. For this reason, it is really important to use a CDN service like CloudFlare, in order to provide a good user experience on our website: we don’t want our clients wait too much to load the homepage! How do we do that? The author of GNU grep once said:

The key to making programs fast is to make them
 do practically nothing

Mike Haertel, Sat Aug 21 2010. Source

So I decided to integrate our django servers with CloudFlare, improving the cache. According to their documentation, to enable the cache, the backend needs to provide specific headers in the HTTP response, like Cache-Control, Expires and others: by using these headers both the browsers and the CloudFlare servers will cache the file for a specific amount of time.

cf-logo-v-rgb

For this purpose, I created an open source customizable middleware to provide cache headers in a smart way. It is called django-smartcc (standing for “smart cache control”). Once installed, it will start considering not authenticated requests as public and disabling the cache when the user is logged in. You can read here the instructions to install it too:

pip install -U django-smartcc

Continue reading How to use Cache Headers to speed up your website with Django and CloudFlare