My Caching Tales Part 1

Title: My caching tales - part 1 Date: 2012-03-25 17:47 Author: eamonnfaherty Category: development Tags: caching, cdn, django, geo:lat=51.54171669999999, geo:lon=-0.08008210000002691, geotagged, London, nginx Slug: my-caching-tales-part-1

I am building a bespoke social network using Django. I am approaching the first release and I thought it would be a good time to look at the static asset performance. I installed yslow and tested my site. I got an F! Arghhh! Luckily, YSlow gives you a lot of guidance on how to improve your performance. I decided to write up what I did after @gmsharky asked me to explain a little more about what I was doing. BTW, after these steps I got to a grade C!

CDN usage

I moved all my static assets and media to dedicated subdomains so that the session and csrf cookies would not be transmitted whilst the assets were being requested. This was an easy win thanks to Djangos STATIC and MEDIA setup.

GZIP compression

I turned on gzip compression within nginx:

http { ... gzip on; gzip_http_version 1.1; gzip_vary on; gzip_comp_level 6; gzip_proxied any; gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js; gzip_buffers 16 8k; gzip_disable "MSIE [1-6].(?!.*SV1)"; ... }

I set far future expiration dates on my static content and disabled cookies for the urls:

server { listen 80; ... location ~* .(?:ico|gif|css|js|jpe?g|png)$ { access_log off; expires max; add_header Pragma public; add_header Cache-Control "public"; break; } ... }

JS and CSS minify

I used jezdez’s great django_compressor library to minify my js and css. This basically read my JS and CSS declarations and imported them into a single file for each. I had to tweak this due to possible name conflicts but it worked well in the end.

Next steps

The next thing for me to do is to use more sprite sheets instead of seperate images but there are not too many images anyway so I am going to stop for now.

Next post

In order to improve performance more I introduced varnish-cache to my stack. I had to do a lot of reading to come up with a working setup and will post that in another blog post coming soon - I need to finish my testing first!

Published: March 25 2012

  • category:
  • tags:
blog comments powered by Disqus