How to speed up TTFB (django+gunicorn+nginx)

Posted December 3, 2019 5.5k views

I’m trying to migrate an existing Django web app from AWS (using Apache) to DigitalOcean using nginx.

It’s not an identical comparison, but I’m getting massive differences in TTFB which I can’t account for. For a basic page, I’m getting these stats:

Discovered: 0.010 s
Request Start: 0.258 s
DNS Lookup: 26 ms
Initial Connection: 97 ms
SSL Negotiation: 118 ms
Time to First Byte: 7159 ms
Content Download: 2 ms
Bytes In (downloaded): 4.9 KB
Uncompressed Size: 31.1 KB
Bytes Out (uploaded): 0.5 KB

In comparison to my AWS site which is

Discovered: 0.008 s
Request Start: 0.124 s
DNS Lookup: 27 ms
Initial Connection: 31 ms
SSL Negotiation: 50 ms
Time to First Byte: 579 ms
Content Download: 3 ms
Bytes In (downloaded): 6.2 KB
Uncompressed Size: 34.2 KB
Bytes Out (uploaded): 0.5 KB

I’ve followed the tutorials for:

When that resulted in really slow TTFB I proceeded to

In an attempt to see some of the numbers budge, I moved a lot of asset loading to CDN. It ticked a lot of boxes in, but it didn’t alter the TTFB, which is by far the biggest bottleneck.

As you can see a 7s TTFB is absolutely massive! Most users keep mentioning times in the >1s range.

I’m totally stumped for ideas now, so short of nuking my droplet and starting from scratch, I’m not sure what else to try.

Is there something I should double check? I can post some configs if that helps.

edited by bobbyiliev
1 comment

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers
by Justin Ellingwood
Django is a powerful web framework that can help you get your Python application or website off the ground. Django includes a simplified development server for testing your code locally, but for anything even slightly production related, a more secure and powerful web server...
  • The linked community tutorial is the one I followed, and the video is following much the same steps, yes?

    What I’m confused about is at what level is this TTFB getting so affected? Is it at the gunicorn socket level? is there some kind of issue with SSL? My nginx error log has no warnings or errors and the system logs don’t show anything untoward.

    Is there a particular logging message I should be looking out for that could offer some insight as to what is taking so long? At 7 seconds (and that’s one of the simpler pages, the worst affected page is 15 seconds), there must be something fundamentally wrong somewhere.

My video is based on a compilation of the most important DO tutorials, including security and configuration. It is possible that my Nginx configuration is better prepared for a real case. Or it may, that a small syntax error is causing this slowness. I guess you’ve replaced HTML5 + PHP with HTML5 + Python. You should observe the browser’s command console (F12) and that there is no error. Have you contacted DO support?

@404namenotfound My guess was that TFFB was heavily affected by using an Amazon hosted database but having everything else hosted at DigitalOcean. I wasn’t prepared to migrate the database to DigitalOcean at the time so I had a mix of the app/sever running on DigitalOcean but the database was on AWS. Because of time constraints, I gave up and basically cloned my exact DigitalOcean ubuntu OS configuration to an AWS EC2 nano instance and everything came right.
Perhaps if I went all-in on DigitalOcean I could have avoided a high TTFB too.