Django Performance Recipes

Jon Atkinson
Jon AtkinsonTechnical Director at FARM Digital Ltd.
Django
Performance
Recipes
September 2015
Jon Atkinson
Technical Director
FARM Digital
Me
• I am not a very good programmer.
• I’m quite a good problem solver.
• I have made a lot of mistakes.
• I prefer valuable solutions.
My Context
• “Fast-moving agency environment”.
• We often participate in launches; demand is
spiky.
• Complex software.
Shared Context
• Traffic is unpredictable.
• We probably host in the cloud.
• The Django ecosystem.
Performance Matters
Speed is all about perception, but:
0-100ms Instant!
100-300ms Small delay.
300-1000ms Something is happening.
1000ms+ Likely task switch.
10000ms+ Abandoned task.
Rule of thumb
50% of users will abandon a task after
3 seconds.
Environment
A web server, talking to:
A managed python process, talking to:
A cache, and a database.
Request Cycle
Web Server
ORM
Middleware
Views + Templates
Web Server
Tools
!
ORM
• Optimising your database is a separate
presentation entirely.
• There are no ‘slow’ databases any more.
• In a read-heavy environment, caching Querysets
is a huge advantage.
ORM
django-cachalot
Caches your Django ORM queries and
automatically invalidates them.
ORM
$ pip install django-cachalot
INSTALLED_APPS += (‘cachalot’)
settings.CACHALOT_ENABLED = True
ORM
from django.conf import settings
from django.test.utils import override_settings
with override_settings(CACHALOT_ENABLED=False):
# SQL queries are not cached in this block
@override_settings(CACHALOT_CACHE=‘second_cache')
def your_function():
# What’s in this function uses another cache
# Globally disables SQL caching until you set it back to True
settings.CACHALOT_ENABLED = False
ORM
• A few other data access tips:
• Is your database doing DNS lookups?
• Do you have a connection timeout set? The
default is 0, and setup/teardown costs time.
settings.DATABASES[‘…’][‘CONN_MAX_AGE’] = 600
Middleware
Middleware is dumb.
Middleware
Middleware
• Think hard. Milliseconds add up.
• Middleware (and context processors!) often
becomes a dumping ground for common
features.
Middleware
Middleware is helpful.
Middleware
django.middleware.http.ConditionalGetMiddleware
Optimises GET requests from modern browsers
django.middleware.http.GZipMiddleware
Compresses responses. But be aware of BREACH!
Middleware
$ pip install django-cprofile-middleware
“Once you've installed it, log in as a user who has
staff privileges and add ?prof to any URL to see the
profiler's stats.”
eg. http://localhost:8000/foo/?prof.
Middleware
7986 function calls (7850 primitive calls) in 1.725 CPU seconds
Ordered by: internal time, call count
List reduced from 392 to 20 due to restriction <20>
ncalls tottime percall cumtime percall filename:lineno(function)
2 1.570 0.785 1.570 0.785 /…/django/db/backends/__init__.py:36(_commit)
15 0.043 0.003 0.043 0.003 /…/linecache.py:68(updatecache)
1 0.020 0.020 0.027 0.027 /…/django/contrib/auth/models.py:1(<module>)
12 0.014 0.001 0.030 0.002 /…/django/utils/importlib.py:18(import_module)
1013 0.010 0.000 0.010 0.000 /…/posixpath.py:56(join)
Views & Templates
• View performance problems are usually obvious:
• Avoid nested loops (especially when generating
QuerySets!)
• Cache where you can.
• Always return at the earliest possible moment.
Views & Templates
• Templates are more interesting.
• It’s easy to duplicate ORM calls already made in
the view.
• It’s easy to traverse relationships in the template
language.
• Templates are loaded from disk by default.
Views & Templates
class Person(models.Model):
def friends(self):
# Hit the database here for something complex…
return friends
# View:
if person.friends():
# Do something here…
# Template:
{% for friend in person.friends %}
Views & Templates
from django.utils.functional import
cached_property
@cached_property
def friends(self):
# Hit the database here for something complex…
return friends
Views & Templates
• Caching template fragments is very powerful.
• Sometimes you need to do something expensive in a
template, but:
{% load cache %}
{% cache 500 sidebar %}
… do something expensive here …
{% endcache %}
Views & Templates
• Where do your templates actually live?
• Cloud disk performance can be erratic.
Views & Templates
# Default setting.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
Views & Templates
TEMPLATE_LOADERS = (
('django.template.loaders.cached.Loader', (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)),
)
nginx
This is my “one weird tip”…
… with a trade-off.
uwsgi_cache_path /tmp/nginx levels=1:2 keys_zone=my_zone:10m;
server {
listen 80;
server_name example.com;
…
uwsgi_cache_use_stale error timeout invalid_header http_500;
uwsgi_cache_valid 10m;
location / {
include uwsgi_params;
uwsgi_pass unix:///tmp/example.sock;
uwsgi_cache cache;
uwsgi_cache_key $scheme:$host$request_uri:$request_method;
uwsgi_cache_bypass $http_pragma $http_authorization;
uwsgi_no_cache $http_pragma $http_authorization;
}
nginx
Tools
• The tool ecosystem is richer now than ever
before.
$ pip install django-debug-toolbar
$ pip install dogslow
• Measure twice, cut once.
• Remember, Schrödinger’s web app.
Tools
Tools
import newrelic.agent
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
# This needs to be called after we bootstrapped the application
# otherwise the settings wouldn't be configured
from django.conf import settings # noqa
if hasattr(settings, 'NEWRELIC_CONFIG'):
newrelic.agent.initialize(settings.NEWRELIC_CONFIG 
getattr(settings, 'NEWRELIC_ENVIRONMENT', None))
application = newrelic.agent.WSGIApplicationWrapper(application)
Meta 1
Performance can be personal.
Meta 2
Meta 2
Meta 3
Performance is of huge value.
10x programmers probably don’t exist.
Keep your eyes up.
Concentrate on where the value lies.
Thank You
Questions?
Resources & Credits: http://blog.wearefarm.com
1 of 41

Recommended

Hybrid Web Applications by
Hybrid Web ApplicationsHybrid Web Applications
Hybrid Web ApplicationsJames Da Costa
6K views81 slides
Django a whirlwind tour by
Django   a whirlwind tourDjango   a whirlwind tour
Django a whirlwind tourBrad Montgomery
1.6K views61 slides
Introduction To Django (Strange Loop 2011) by
Introduction To Django (Strange Loop 2011)Introduction To Django (Strange Loop 2011)
Introduction To Django (Strange Loop 2011)Jacob Kaplan-Moss
14.4K views139 slides
The Django Web Application Framework 2 by
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2fishwarter
7.7K views38 slides
The Best (and Worst) of Django by
The Best (and Worst) of DjangoThe Best (and Worst) of Django
The Best (and Worst) of DjangoJacob Kaplan-Moss
35.7K views53 slides
Django Architecture Introduction by
Django Architecture IntroductionDjango Architecture Introduction
Django Architecture IntroductionHaiqi Chen
1.8K views24 slides

More Related Content

What's hot

Building a Dynamic Website Using Django by
Building a Dynamic Website Using DjangoBuilding a Dynamic Website Using Django
Building a Dynamic Website Using DjangoNathan Eror
6.8K views49 slides
Getting started with Django 1.8 by
Getting started with Django 1.8Getting started with Django 1.8
Getting started with Django 1.8rajkumar2011
699 views18 slides
Php go vrooom! by
Php go vrooom!Php go vrooom!
Php go vrooom!Elizabeth Smith
2.8K views46 slides
jQuery from the very beginning by
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginningAnis Ahmad
11.8K views73 slides
Ruby on Rails by
Ruby on RailsRuby on Rails
Ruby on RailsAizat Faiz
3.6K views69 slides
Writing Pluggable Software by
Writing Pluggable SoftwareWriting Pluggable Software
Writing Pluggable SoftwareTatsuhiko Miyagawa
3.9K views112 slides

What's hot(20)

Building a Dynamic Website Using Django by Nathan Eror
Building a Dynamic Website Using DjangoBuilding a Dynamic Website Using Django
Building a Dynamic Website Using Django
Nathan Eror6.8K views
Getting started with Django 1.8 by rajkumar2011
Getting started with Django 1.8Getting started with Django 1.8
Getting started with Django 1.8
rajkumar2011699 views
jQuery from the very beginning by Anis Ahmad
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
Anis Ahmad11.8K views
Ruby on Rails by Aizat Faiz
Ruby on RailsRuby on Rails
Ruby on Rails
Aizat Faiz3.6K views
PECL Picks - Extensions to make your life better by ZendCon
PECL Picks - Extensions to make your life betterPECL Picks - Extensions to make your life better
PECL Picks - Extensions to make your life better
ZendCon4.1K views
Django multi-tier by smirolo
Django multi-tierDjango multi-tier
Django multi-tier
smirolo4.1K views
Realize mais com HTML 5 e CSS 3 - 16 EDTED - RJ by Leonardo Balter
Realize mais com HTML 5 e CSS 3 - 16 EDTED - RJRealize mais com HTML 5 e CSS 3 - 16 EDTED - RJ
Realize mais com HTML 5 e CSS 3 - 16 EDTED - RJ
Leonardo Balter1.1K views
HTML5: friend or foe (to Flash)? by Remy Sharp
HTML5: friend or foe (to Flash)?HTML5: friend or foe (to Flash)?
HTML5: friend or foe (to Flash)?
Remy Sharp53.4K views
Django Rest Framework and React and Redux, Oh My! by Eric Palakovich Carr
Django Rest Framework and React and Redux, Oh My!Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!
High Performance Ajax Applications by Julien Lecomte
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
Julien Lecomte56.7K views
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010 by singingfish
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
singingfish497 views
Web application development with Django framework by flapiello
Web application development with Django frameworkWeb application development with Django framework
Web application development with Django framework
flapiello1.9K views
Learning django step 1 by 永昇 陳
Learning django step 1Learning django step 1
Learning django step 1
永昇 陳3.5K views

Similar to Django Performance Recipes

Where Django Caching Bust at the Seams by
Where Django Caching Bust at the SeamsWhere Django Caching Bust at the Seams
Where Django Caching Bust at the SeamsConcentric Sky
16.4K views64 slides
Drupal Performance : DrupalCamp North by
Drupal Performance : DrupalCamp NorthDrupal Performance : DrupalCamp North
Drupal Performance : DrupalCamp NorthPhilip Norton
1.7K views79 slides
Expecto Performa! The Magic and Reality of Performance Tuning by
Expecto Performa! The Magic and Reality of Performance TuningExpecto Performa! The Magic and Reality of Performance Tuning
Expecto Performa! The Magic and Reality of Performance TuningAtlassian
11.3K views114 slides
2019 StartIT - Boosting your performance with Blackfire by
2019 StartIT - Boosting your performance with Blackfire2019 StartIT - Boosting your performance with Blackfire
2019 StartIT - Boosting your performance with BlackfireMarko Mitranić
26 views79 slides
Dr Elephant: LinkedIn's Self-Service System for Detecting and Treating Hadoop... by
Dr Elephant: LinkedIn's Self-Service System for Detecting and Treating Hadoop...Dr Elephant: LinkedIn's Self-Service System for Detecting and Treating Hadoop...
Dr Elephant: LinkedIn's Self-Service System for Detecting and Treating Hadoop...DataWorks Summit
5.2K views28 slides
High Performance Django 1 by
High Performance Django 1High Performance Django 1
High Performance Django 1DjangoCon2008
1.1K views22 slides

Similar to Django Performance Recipes(20)

Where Django Caching Bust at the Seams by Concentric Sky
Where Django Caching Bust at the SeamsWhere Django Caching Bust at the Seams
Where Django Caching Bust at the Seams
Concentric Sky16.4K views
Drupal Performance : DrupalCamp North by Philip Norton
Drupal Performance : DrupalCamp NorthDrupal Performance : DrupalCamp North
Drupal Performance : DrupalCamp North
Philip Norton1.7K views
Expecto Performa! The Magic and Reality of Performance Tuning by Atlassian
Expecto Performa! The Magic and Reality of Performance TuningExpecto Performa! The Magic and Reality of Performance Tuning
Expecto Performa! The Magic and Reality of Performance Tuning
Atlassian11.3K views
2019 StartIT - Boosting your performance with Blackfire by Marko Mitranić
2019 StartIT - Boosting your performance with Blackfire2019 StartIT - Boosting your performance with Blackfire
2019 StartIT - Boosting your performance with Blackfire
Marko Mitranić26 views
Dr Elephant: LinkedIn's Self-Service System for Detecting and Treating Hadoop... by DataWorks Summit
Dr Elephant: LinkedIn's Self-Service System for Detecting and Treating Hadoop...Dr Elephant: LinkedIn's Self-Service System for Detecting and Treating Hadoop...
Dr Elephant: LinkedIn's Self-Service System for Detecting and Treating Hadoop...
DataWorks Summit5.2K views
High Performance Django 1 by DjangoCon2008
High Performance Django 1High Performance Django 1
High Performance Django 1
DjangoCon20081.1K views
High Performance Django by DjangoCon2008
High Performance DjangoHigh Performance Django
High Performance Django
DjangoCon20081.3K views
Performance & Scalability Improvements in Perforce by Perforce
Performance & Scalability Improvements in PerforcePerformance & Scalability Improvements in Perforce
Performance & Scalability Improvements in Perforce
Perforce3.2K views
Capacity Management from Flickr by xlight
Capacity Management from FlickrCapacity Management from Flickr
Capacity Management from Flickr
xlight976 views
Cloud Best Practices by Eric Bottard
Cloud Best PracticesCloud Best Practices
Cloud Best Practices
Eric Bottard1.3K views
Cloud Computing with .Net by Wesley Faler
Cloud Computing with .NetCloud Computing with .Net
Cloud Computing with .Net
Wesley Faler256 views
Gearman - Northeast PHP 2012 by Mike Willbanks
Gearman - Northeast PHP 2012Gearman - Northeast PHP 2012
Gearman - Northeast PHP 2012
Mike Willbanks2.5K views
Profiling PHP with Xdebug / Webgrind by Sam Keen
Profiling PHP with Xdebug / WebgrindProfiling PHP with Xdebug / Webgrind
Profiling PHP with Xdebug / Webgrind
Sam Keen43.8K views
Gearman: A Job Server made for Scale by Mike Willbanks
Gearman: A Job Server made for ScaleGearman: A Job Server made for Scale
Gearman: A Job Server made for Scale
Mike Willbanks9.1K views
(BDT402) Performance Profiling in Production: Analyzing Web Requests at Scale... by Amazon Web Services
(BDT402) Performance Profiling in Production: Analyzing Web Requests at Scale...(BDT402) Performance Profiling in Production: Analyzing Web Requests at Scale...
(BDT402) Performance Profiling in Production: Analyzing Web Requests at Scale...
Amazon Web Services1.7K views
Real-Time Query for Data Guard by Uwe Hesse
Real-Time Query for Data Guard Real-Time Query for Data Guard
Real-Time Query for Data Guard
Uwe Hesse3.5K views
PyGrunn 2017 - Django Performance Unchained - slides by Artur Barseghyan
PyGrunn 2017 - Django Performance Unchained - slidesPyGrunn 2017 - Django Performance Unchained - slides
PyGrunn 2017 - Django Performance Unchained - slides
Artur Barseghyan741 views

Recently uploaded

The Forbidden VPN Secrets.pdf by
The Forbidden VPN Secrets.pdfThe Forbidden VPN Secrets.pdf
The Forbidden VPN Secrets.pdfMariam Shaba
20 views72 slides
Piloting & Scaling Successfully With Microsoft Viva by
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft VivaRichard Harbridge
13 views160 slides
HTTP headers that make your website go faster - devs.gent November 2023 by
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023Thijs Feryn
26 views151 slides
Design Driven Network Assurance by
Design Driven Network AssuranceDesign Driven Network Assurance
Design Driven Network AssuranceNetwork Automation Forum
19 views42 slides
Special_edition_innovator_2023.pdf by
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdfWillDavies22
18 views6 slides
PRODUCT LISTING.pptx by
PRODUCT LISTING.pptxPRODUCT LISTING.pptx
PRODUCT LISTING.pptxangelicacueva6
18 views1 slide

Recently uploaded(20)

The Forbidden VPN Secrets.pdf by Mariam Shaba
The Forbidden VPN Secrets.pdfThe Forbidden VPN Secrets.pdf
The Forbidden VPN Secrets.pdf
Mariam Shaba20 views
Piloting & Scaling Successfully With Microsoft Viva by Richard Harbridge
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft Viva
HTTP headers that make your website go faster - devs.gent November 2023 by Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn26 views
Special_edition_innovator_2023.pdf by WillDavies22
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdf
WillDavies2218 views
Future of AR - Facebook Presentation by ssuserb54b561
Future of AR - Facebook PresentationFuture of AR - Facebook Presentation
Future of AR - Facebook Presentation
ssuserb54b56122 views
"Node.js Development in 2024: trends and tools", Nikita Galkin by Fwdays
"Node.js Development in 2024: trends and tools", Nikita Galkin "Node.js Development in 2024: trends and tools", Nikita Galkin
"Node.js Development in 2024: trends and tools", Nikita Galkin
Fwdays17 views
"Surviving highload with Node.js", Andrii Shumada by Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays33 views
2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe by Simone Puorto
2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe
2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe
Simone Puorto13 views
SAP Automation Using Bar Code and FIORI.pdf by Virendra Rai, PMP
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdf
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker48 views
"Running students' code in isolation. The hard way", Yurii Holiuk by Fwdays
"Running students' code in isolation. The hard way", Yurii Holiuk "Running students' code in isolation. The hard way", Yurii Holiuk
"Running students' code in isolation. The hard way", Yurii Holiuk
Fwdays24 views
Business Analyst Series 2023 - Week 3 Session 5 by DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10345 views

Django Performance Recipes

  • 2. Me • I am not a very good programmer. • I’m quite a good problem solver. • I have made a lot of mistakes. • I prefer valuable solutions.
  • 3. My Context • “Fast-moving agency environment”. • We often participate in launches; demand is spiky. • Complex software.
  • 4. Shared Context • Traffic is unpredictable. • We probably host in the cloud. • The Django ecosystem.
  • 5. Performance Matters Speed is all about perception, but: 0-100ms Instant! 100-300ms Small delay. 300-1000ms Something is happening. 1000ms+ Likely task switch. 10000ms+ Abandoned task.
  • 6. Rule of thumb 50% of users will abandon a task after 3 seconds.
  • 7. Environment A web server, talking to: A managed python process, talking to: A cache, and a database.
  • 8. Request Cycle Web Server ORM Middleware Views + Templates Web Server Tools
  • 9. !
  • 10. ORM • Optimising your database is a separate presentation entirely. • There are no ‘slow’ databases any more. • In a read-heavy environment, caching Querysets is a huge advantage.
  • 11. ORM django-cachalot Caches your Django ORM queries and automatically invalidates them.
  • 12. ORM $ pip install django-cachalot INSTALLED_APPS += (‘cachalot’) settings.CACHALOT_ENABLED = True
  • 13. ORM from django.conf import settings from django.test.utils import override_settings with override_settings(CACHALOT_ENABLED=False): # SQL queries are not cached in this block @override_settings(CACHALOT_CACHE=‘second_cache') def your_function(): # What’s in this function uses another cache # Globally disables SQL caching until you set it back to True settings.CACHALOT_ENABLED = False
  • 14. ORM • A few other data access tips: • Is your database doing DNS lookups? • Do you have a connection timeout set? The default is 0, and setup/teardown costs time. settings.DATABASES[‘…’][‘CONN_MAX_AGE’] = 600
  • 17. Middleware • Think hard. Milliseconds add up. • Middleware (and context processors!) often becomes a dumping ground for common features.
  • 19. Middleware django.middleware.http.ConditionalGetMiddleware Optimises GET requests from modern browsers django.middleware.http.GZipMiddleware Compresses responses. But be aware of BREACH!
  • 20. Middleware $ pip install django-cprofile-middleware “Once you've installed it, log in as a user who has staff privileges and add ?prof to any URL to see the profiler's stats.” eg. http://localhost:8000/foo/?prof.
  • 21. Middleware 7986 function calls (7850 primitive calls) in 1.725 CPU seconds Ordered by: internal time, call count List reduced from 392 to 20 due to restriction <20> ncalls tottime percall cumtime percall filename:lineno(function) 2 1.570 0.785 1.570 0.785 /…/django/db/backends/__init__.py:36(_commit) 15 0.043 0.003 0.043 0.003 /…/linecache.py:68(updatecache) 1 0.020 0.020 0.027 0.027 /…/django/contrib/auth/models.py:1(<module>) 12 0.014 0.001 0.030 0.002 /…/django/utils/importlib.py:18(import_module) 1013 0.010 0.000 0.010 0.000 /…/posixpath.py:56(join)
  • 22. Views & Templates • View performance problems are usually obvious: • Avoid nested loops (especially when generating QuerySets!) • Cache where you can. • Always return at the earliest possible moment.
  • 23. Views & Templates • Templates are more interesting. • It’s easy to duplicate ORM calls already made in the view. • It’s easy to traverse relationships in the template language. • Templates are loaded from disk by default.
  • 24. Views & Templates class Person(models.Model): def friends(self): # Hit the database here for something complex… return friends # View: if person.friends(): # Do something here… # Template: {% for friend in person.friends %}
  • 25. Views & Templates from django.utils.functional import cached_property @cached_property def friends(self): # Hit the database here for something complex… return friends
  • 26. Views & Templates • Caching template fragments is very powerful. • Sometimes you need to do something expensive in a template, but: {% load cache %} {% cache 500 sidebar %} … do something expensive here … {% endcache %}
  • 27. Views & Templates • Where do your templates actually live? • Cloud disk performance can be erratic.
  • 28. Views & Templates # Default setting. TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', )
  • 29. Views & Templates TEMPLATE_LOADERS = ( ('django.template.loaders.cached.Loader', ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', )), )
  • 30. nginx This is my “one weird tip”… … with a trade-off.
  • 31. uwsgi_cache_path /tmp/nginx levels=1:2 keys_zone=my_zone:10m; server { listen 80; server_name example.com; … uwsgi_cache_use_stale error timeout invalid_header http_500; uwsgi_cache_valid 10m; location / { include uwsgi_params; uwsgi_pass unix:///tmp/example.sock; uwsgi_cache cache; uwsgi_cache_key $scheme:$host$request_uri:$request_method; uwsgi_cache_bypass $http_pragma $http_authorization; uwsgi_no_cache $http_pragma $http_authorization; }
  • 32. nginx
  • 33. Tools • The tool ecosystem is richer now than ever before. $ pip install django-debug-toolbar $ pip install dogslow • Measure twice, cut once. • Remember, Schrödinger’s web app.
  • 34. Tools
  • 35. Tools
  • 36. import newrelic.agent from django.core.wsgi import get_wsgi_application application = get_wsgi_application() # This needs to be called after we bootstrapped the application # otherwise the settings wouldn't be configured from django.conf import settings # noqa if hasattr(settings, 'NEWRELIC_CONFIG'): newrelic.agent.initialize(settings.NEWRELIC_CONFIG getattr(settings, 'NEWRELIC_ENVIRONMENT', None)) application = newrelic.agent.WSGIApplicationWrapper(application)
  • 37. Meta 1 Performance can be personal.
  • 40. Meta 3 Performance is of huge value. 10x programmers probably don’t exist. Keep your eyes up. Concentrate on where the value lies.
  • 41. Thank You Questions? Resources & Credits: http://blog.wearefarm.com