Heroku app is slow? How to increase performance with Django and S3

My django app on Heroku runs starts up really slowly.

I run a simple django application that is hosted on Heroku and I use s3 to serve my static and media files.

My initial load ‘First View’ time would climb to almost 15seconds which is unacceptable. From then on it would speed up but still some tweaks were in order.

I ran my website through http://www.webpagetest.org and landed up scoring four F symbols for

  1. Cache static content [F]
  2. Keep-alive Enabled [F]
  3. Compress Transfer [F]
  4. First Byte Time [F]

Here is what I did to help speed up my site.

Speeding up Heroku

After going a little research I read that Heroku actually clears out it’s memory if your app is not very active. This is the main reason why the First View takes so long. The Heroku dynos have to start to spin from scratch and that takes time.

  1. Add on an extra dyno which will cost about $30 a month. (To be honest this still didn’t fix my problem entirely) (Helps with the Keep-alive Enabled & First Byte Time issues)
  2. Add a monitor/pinging service to ping your site regularly. This will stop the site from ‘sleeping’ during not very active times. (Helps with the Keep-alive Enabled & First Byte Time issues)

Speeding up s3 static

The biggest issues here being proper Expiry headers.


If your files are already up on S3 you’ll need to update their meta data. Best case scenario is that you set the headers as you upload the files though.

(Helps with the Cache static content & First Byte Time issues)

  1. Set your Cache-Control header.
    • Http Header: Cache-Control
    • Header Value: max-age=31536000, public
  2. Set your Expiry header
    • Http Header: Expires
    • Header Value: Sun, 17 Jan 2038 19:00:00 GMT

Speeding up Django slightly

I didn’t do too much work here. I did find out about the django.middleware.gzip which makes a huge difference on loading time.

(Helps with the Compress Transfer issue)

  1. add ‘django.middleware.gzip’ to your middleware settings and make sure it’s the first item in the list!

**I had some issues with the gzip middleware on my application where it was causing my requests to timeout randomly. I found this resource which seems to touch on the fact that the Heroku Cedar stack might not be capable of handling the gzip. Or maybe he means it needs to be setup correctly. Either way I had to remove this tweak, sadly. However a better method than this gzip middleware is probably to use django-compressor.


Running my site through the test again I now get

  1. Cache static content [A]
  2. Keep-alive Enabled [A]
  3. Compress Transfer [A]
  4. First Byte Time [D]

While the First Byte Time is still not ideal it’s much better than an F. The rest of the issues have had massive improvements with these tweaks.


One thought on “Heroku app is slow? How to increase performance with Django and S3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s