Django in the Real World

Jacob Kaplan-Moss

OSCON 2009
http://jacobian.org/TN
Jacob Kaplan-Moss
http://jacobian.org / jacob@jacobian.org / @jacobian

              Lead Developer, Django

            ...
Shameless plug:



    http://revsys.com/




                         3
Hat tip:
James Bennett (http://b-list.org)




                                    4
So you’ve written a
  Django site…


                      5
… now what?



              6
http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites
                                                      ...
•   API Metering




                   http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites
              ...
•   API Metering
•   Backups & Snapshots




                   http://randomfoo.net/2009/01/28/infrastructure-for-modern-...
•   API Metering
•   Backups & Snapshots
•   Counters




                   http://randomfoo.net/2009/01/28/infrastructur...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools




                   http://ran...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering
•   Backups & Snapshots
•   Counters
•   Cloud/Cluster Management Tools
     •   Instrumentation/Monitori...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots
...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
•   API Metering                                            •   Distributed Log storage, analysis
•   Backups & Snapshots ...
The bare minimum:




                    8
The bare minimum:
• Test.




                    8
The bare minimum:
• Test.
• Structure for deployment.




                              8
The bare minimum:
• Test.
• Structure for deployment.
• Use deployment tools.




                              8
The bare minimum:
• Test.
• Structure for deployment.
• Use deployment tools.
• Design a production environment.




     ...
The bare minimum:
• Test.
• Structure for deployment.
• Use deployment tools.
• Design a production environment.
• Monitor...
The bare minimum:
• Test.
• Structure for deployment.
• Use deployment tools.
• Design a production environment.
• Monitor...
Testing



          9
“   Tests are the
Programmer’s stone,
transmuting fear into



                      ”
     boredom.

              — Kent...
Hardcore TDD



               11
“
    I don’t do test driven
development. I do stupidity
driven testing… I wait until
I do something stupid, and




     ...
Whatever happens, don’t let
your test suite break thinking,
“I’ll go back and fix this later.”



                         ...
Unit testing




Functional/behavior
testing




Browser testing
                      14
Unit testing          unittest




Functional/behavior
testing




Browser testing
                                 14
Unit testing          unittest



                      doctest
Functional/behavior
testing




Browser testing
          ...
Unit testing                  unittest



                              doctest
Functional/behavior
testing
              ...
Unit testing                  unittest



                              doctest
Functional/behavior
testing
              ...
You need them all.



                     15
Testing Django




                 16
Testing Django

• Unit tests (unittest)




                          16
Testing Django

• Unit tests (unittest)
• Doctests (doctest)




                          16
Testing Django

• Unit tests (unittest)
• Doctests (doctest)
• Fixtures




                          16
Testing Django

• Unit tests (unittest)
• Doctests (doctest)
• Fixtures
• Test client




                          16
Testing Django

• Unit tests (unittest)
• Doctests (doctest)
• Fixtures
• Test client
• Email capture


                  ...
Unit tests




             17
Unit tests
• “Whitebox” testing




                       17
Unit tests
• “Whitebox” testing
• Verify the small functional units of your
  app




                                    ...
Unit tests
• “Whitebox” testing
• Verify the small functional units of your
  app
• Very fine-grained




                 ...
Unit tests
• “Whitebox” testing
• Verify the small functional units of your
  app
• Very fine-grained
• Familier to most pr...
Unit tests
• “Whitebox” testing
• Verify the small functional units of your
  app
• Very fine-grained
• Familier to most pr...
django.test.TestCase




                       18
django.test.TestCase

• Fixtures.




                       18
django.test.TestCase

• Fixtures.
• Test client.




                       18
django.test.TestCase

• Fixtures.
• Test client.
• Email capture.




                       18
django.test.TestCase

• Fixtures.
• Test client.
• Email capture.
• Database management.




                         18
django.test.TestCase

• Fixtures.
• Test client.
• Email capture.
• Database management.
• Slower than unittest.TestCase.
...
class StoryAddViewTests(TestCase):
   fixtures = ['authtestdata', 'newsbudget_test_data']
   urls = 'newsbudget.urls'

  d...
Doctests




           20
Doctests
• Easy to write & read.




                          20
Doctests
• Easy to write & read.
• Produces self-documenting code.




                                    20
Doctests
• Easy to write & read.
• Produces self-documenting code.
• Great for cases that only use assertEquals.




     ...
Doctests
• Easy to write & read.
• Produces self-documenting code.
• Great for cases that only use assertEquals.
• Somewhe...
Doctests
• Easy to write & read.
• Produces self-documenting code.
• Great for cases that only use assertEquals.
• Somewhe...
Doctests
• Easy to write & read.
• Produces self-documenting code.
• Great for cases that only use assertEquals.
• Somewhe...
class Choices(object):
   """
   Easy declarative "choices" tool::

        >>> STATUSES = Choices("Live", "Draft")

     ...
****************************************************
File "utils.py", line 150, in __main__.Choices
Failed example:
   STA...
Functional tests




                   23
Functional tests
• a.k.a “Behavior Driven Development.”




                                         23
Functional tests
• a.k.a “Behavior Driven Development.”
• “Blackbox,” holistic testing.




                              ...
Functional tests
• a.k.a “Behavior Driven Development.”
• “Blackbox,” holistic testing.
• All the hardcore TDD folks look ...
Functional tests
• a.k.a “Behavior Driven Development.”
• “Blackbox,” holistic testing.
• All the hardcore TDD folks look ...
Functional tests
• a.k.a “Behavior Driven Development.”
• “Blackbox,” holistic testing.
• All the hardcore TDD folks look ...
Functional testing
tools




                     24
Functional testing
tools
• django.test.Client




                       24
Functional testing
tools
• django.test.Client
• webunit




                       24
Functional testing
tools
• django.test.Client
• webunit
• Twill




                       24
Functional testing
tools
• django.test.Client
• webunit
• Twill
• ...



                       24
django.test.Client




                     25
django.test.Client

• Test the whole request path without
  running a web server.




                                    ...
django.test.Client

• Test the whole request path without
  running a web server.
• Responses provide extra information
  ...
class StoryAddViewTests(TestCase):
   fixtures = ['authtestdata', 'newsbudget_test_data']
   urls = 'newsbudget.urls'

  d...
Web browser testing




                      27
Web browser testing

• The ultimate in functional testing for
  web applications.




                                    ...
Web browser testing

• The ultimate in functional testing for
  web applications.
• Run test in a web browser.




       ...
Web browser testing

• The ultimate in functional testing for
  web applications.
• Run test in a web browser.
• Can verif...
Web browser testing

• The ultimate in functional testing for
  web applications.
• Run test in a web browser.
• Can verif...
Browser testing tools




                        28
Browser testing tools


• Selenium




                        28
Browser testing tools


• Selenium
• Windmill




                        28
“Exotic” testing




                   29
“Exotic” testing

• Static source analysis.




                            29
“Exotic” testing

• Static source analysis.
• Smoke testing (crawlers and spiders).




                                  ...
“Exotic” testing

• Static source analysis.
• Smoke testing (crawlers and spiders).
• Monkey testing.




                ...
“Exotic” testing

• Static source analysis.
• Smoke testing (crawlers and spiders).
• Monkey testing.
• Load testing.




...
“Exotic” testing

• Static source analysis.
• Smoke testing (crawlers and spiders).
• Monkey testing.
• Load testing.
• .....
30
Further resources




                    31
Further resources

• Windmill talk here at OSCON
 http://bit.ly/14tkrd




                                31
Further resources

• Windmill talk here at OSCON
 http://bit.ly/14tkrd
• Django testing documentation
 http://bit.ly/djang...
Further resources

• Windmill talk here at OSCON
 http://bit.ly/14tkrd
• Django testing documentation
 http://bit.ly/djang...
Structuring
applications for reuse


                         32
Designing for reuse




                      33
Designing for reuse

• Do one thing, and do it well.




                                  33
Designing for reuse

• Do one thing, and do it well.
• Don’t be afraid of multiple apps.




                             ...
Designing for reuse

• Do one thing, and do it well.
• Don’t be afraid of multiple apps.
• Write for flexibility.




     ...
Designing for reuse

• Do one thing, and do it well.
• Don’t be afraid of multiple apps.
• Write for flexibility.
• Build t...
Designing for reuse

• Do one thing, and do it well.
• Don’t be afraid of multiple apps.
• Write for flexibility.
• Build t...
1.
Do one thing, and do it well.




                                34
Application == encapsulation




                           35
Focus




        36
Focus

• Ask yourself: “What does this
  application do?”




                                  36
Focus

• Ask yourself: “What does this
  application do?”
• Answer should be one or two
  short sentences.




           ...
Good focus




             37
Good focus

• “Handle storage of users and
  authentication of their identities.”




                                    ...
Good focus

• “Handle storage of users and
  authentication of their identities.”
• “Allow content to be tagged, del.icio....
Good focus

• “Handle storage of users and
  authentication of their identities.”
• “Allow content to be tagged, del.icio....
Bad focus




            38
Bad focus


• “Handle entries in a weblog, and users
  who post them, and their authentication,
  and tagging and categori...
Warning signs




                39
Warning signs

• Lots of files.




                  39
Warning signs

• Lots of files.
• Lots of modules.




                     39
Warning signs

• Lots of files.
• Lots of modules.
• Lots of models.




                     39
Warning signs

• Lots of files.
• Lots of modules.
• Lots of models.
• Lots of code.



                     39
Small is good




                40
Small is good

• Many great Django apps are very small.




                                           40
Small is good

• Many great Django apps are very small.
• Even a lot of “simple” Django sites
  commonly have a dozen or m...
Small is good

• Many great Django apps are very small.
• Even a lot of “simple” Django sites
  commonly have a dozen or m...
Approach features skeptically




                                41
Approach features skeptically


• What does the application do?




                                  41
Approach features skeptically


• What does the application do?
• Does this feature have anything to do
  with that?




 ...
Approach features skeptically


• What does the application do?
• Does this feature have anything to do
  with that?
• No?...
2.
Don’t be afraid of many apps.




                                42
The monolith anti-pattern




                            43
The monolith anti-pattern


• The “application” is the whole site.




                                         43
The monolith anti-pattern


• The “application” is the whole site.
• Re-use? YAGNI.




                                  ...
The monolith anti-pattern


• The “application” is the whole site.
• Re-use? YAGNI.
• Plugins that hook into the “main” ap...
The monolith anti-pattern


• The “application” is the whole site.
• Re-use? YAGNI.
• Plugins that hook into the “main” ap...
(I blame Rails)




                  44
The Django mindset




                     45
The Django mindset

• Application: some bit of functionality.




                                            45
The Django mindset

• Application: some bit of functionality.
• Site: several applications.




                          ...
The Django mindset

• Application: some bit of functionality.
• Site: several applications.
• Spin off new “apps” liberall...
The Django mindset

• Application: some bit of functionality.
• Site: several applications.
• Spin off new “apps” liberall...
Django encourages this




                     46
Django encourages this

• INSTALLED_APPS




                     46
Django encourages this

• INSTALLED_APPS
• Applications are just Python packages,
  not some Django-specific “app” or
  “pl...
Django encourages this

• INSTALLED_APPS
• Applications are just Python packages,
  not some Django-specific “app” or
  “pl...
Spin off a new app?




                      47
Spin off a new app?


• Is this feature unrelated to the app’s focus?




                                                ...
Spin off a new app?


• Is this feature unrelated to the app’s focus?
• Is it orthogonal to the rest of the app?




     ...
Spin off a new app?


• Is this feature unrelated to the app’s focus?
• Is it orthogonal to the rest of the app?
• Will I ...
The ideal:



             48
I need a contact form



                        49
urlpatterns = ('',
   …
   (r'^contact/', include('contact_form.urls')),
   …
)




                                      ...
Done.
(http://bitbucket.org/ubernostrum/django-contact-form/)




                                                        ...
But… what about…




                   52
But… what about…

• Site A wants a contact form that just
  collects a message.




                                      ...
But… what about…

• Site A wants a contact form that just
  collects a message.
• Site B’s marketing department wants a
  ...
But… what about…

• Site A wants a contact form that just
  collects a message.
• Site B’s marketing department wants a
  ...
53
53
3.
Write for flexibility.




                        54
Common sense




               55
Common sense


• Sane defaults.




                   55
Common sense


• Sane defaults.
• Easy overrides.




                    55
Common sense


• Sane defaults.
• Easy overrides.
• Don’t set anything in stone.




                                 55
Forms




        56
Forms


• Supply a form class.




                         56
Forms


• Supply a form class.
• Let users specify their own.




                                 56
Templates




            57
Templates


• Specify a default template.




                                57
Templates


• Specify a default template.
• Let users specify their own.




                                 57
Form processing




                  58
Form processing

• You want to redirect after successful
  submission.




                                          58
Form processing

• You want to redirect after successful
  submission.
• Supply a default URL.




                       ...
Form processing

• You want to redirect after successful
  submission.
• Supply a default URL.
 • (Preferably by using rev...
Form processing

• You want to redirect after successful
  submission.
• Supply a default URL.
  • (Preferably by using re...
def edit_entry(request, entry_id):
  form = EntryForm(request.POST or None)
  if form.is_valid():
      form.save()
      ...
def edit_entry(request, entry_id,
          form_class=EntryForm,
          template_name='entry/form.html',
          pos...
URLs




       61
URLs


• Provide a URLConf with all views.




                                      61
URLs


• Provide a URLConf with all views.
• Use named URL patterns.




                                      61
URLs


• Provide a URLConf with all views.
• Use named URL patterns.
• Use reverse lookups (by name).




                ...
4.
Build to distribute (even private code).




                                           62
What the tutorial teaches

myproject/
 settings.py
 urls.py

  myapp/
   models.py

  mysecondapp/
   views.py

  …



   ...
from myproject.myapp.models import …
from myproject. myapp.models import …

…

myproject.settings
myproject.urls




     ...
Project coupling
  kills re-use



                   65
Projects in real life.




                         66
Projects in real life.

• A settings module.




                         66
Projects in real life.

• A settings module.
• A root URLConf.




                         66
Projects in real life.

• A settings module.
• A root URLConf.
• Maybe a manage.py (but…)




                            ...
Projects in real life.

• A settings module.
• A root URLConf.
• Maybe a manage.py (but…)
• And that’s it.



            ...
Advantages




             67
Advantages

• No assumptions about where things live.




                                            67
Advantages

• No assumptions about where things live.
• No PYTHONPATH magic.




                                         ...
Advantages

• No assumptions about where things live.
• No PYTHONPATH magic.
• Reminds you that “projects” are just a
  Py...
You don’t even need a project




                            68
ljworld.com:




               69
ljworld.com:


• worldonline.settings.ljworld




                                 69
ljworld.com:


• worldonline.settings.ljworld
• worldonline.urls.ljworld




                                 69
ljworld.com:


• worldonline.settings.ljworld
• worldonline.urls.ljworld
• And a whole bunch of apps.




                ...
Where apps really live




                         70
Where apps really live
• Single module directly on Python path
  (registration, tagging, etc.).




                      ...
Where apps really live
• Single module directly on Python path
  (registration, tagging, etc.).
• Related modules under a ...
Where apps really live
• Single module directly on Python path
  (registration, tagging, etc.).
• Related modules under a ...
Want to distribute?




                      71
Want to distribute?

• Build a package with distutils/setuptools.




                                               71
Want to distribute?

• Build a package with distutils/setuptools.
• Put it on PyPI (or a private package
  server).




  ...
Want to distribute?

• Build a package with distutils/setuptools.
• Put it on PyPI (or a private package
  server).
• Now ...
General best practices




                         72
General best practices
• Establish dependancy rules.




                                72
General best practices
• Establish dependancy rules.
• Establish a minimum Python version
  (suggestion: Python 2.5).




...
General best practices
• Establish dependancy rules.
• Establish a minimum Python version
  (suggestion: Python 2.5).
• Es...
General best practices
• Establish dependancy rules.
• Establish a minimum Python version
  (suggestion: Python 2.5).
• Es...
Document obsessively.



                    73
5.
Embrace and extend.




                      74
Don’t touch!




               75
Don’t touch!

• Good applications are extensible
  without patching.




                                     75
Don’t touch!

• Good applications are extensible
  without patching.
• Take advantage of every extensibility point
  an ap...
Don’t touch!

• Good applications are extensible
  without patching.
• Take advantage of every extensibility point
  an ap...
But this application
wasn’t meant to be
     extended!


                       76
Python Power!



                77
Extending a view




                   78
Extending a view


• Wrap the view with your own code.




                                      78
Extending a view


• Wrap the view with your own code.
• Doing it repetitively? Write a decorator.




                   ...
Extending a model




                    79
Extending a model


• Relate other models to it.




                               79
Extending a model


• Relate other models to it.
• Subclass it.




                               79
Extending a model


• Relate other models to it.
• Subclass it.
• Proxy subclasses (Django 1.1).




                     ...
Extending a form




                   80
Extending a form


• Subclass it.




                   80
Extending a form


• Subclass it.
• There is no step 2.




                        80
Other tricks




               81
Other tricks

• Signals lets you fire off customized
  behavior when certain events happen.




                           ...
Other tricks

• Signals lets you fire off customized
  behavior when certain events happen.
• Middleware offers full contro...
Other tricks

• Signals lets you fire off customized
  behavior when certain events happen.
• Middleware offers full contro...
If you must make
    changes to
 external code…


                   82
Keep changes to a minimum




                            83
Keep changes to a minimum


• If possible, instead of adding a feature,
  add extensibility.




                         ...
Keep changes to a minimum


• If possible, instead of adding a feature,
  add extensibility.
• Keep as much changed code a...
Stay up-to-date




                  84
Stay up-to-date

• Don’t want to get out of sync with the
  original version of the code!




                            ...
Stay up-to-date

• Don’t want to get out of sync with the
  original version of the code!
• You might miss bugfixes.




  ...
Stay up-to-date

• Don’t want to get out of sync with the
  original version of the code!
• You might miss bugfixes.
• You ...
Use a good VCS




                 85
Use a good VCS
• Subversion vendor branches don’t cut it.




                                             85
Use a good VCS
• Subversion vendor branches don’t cut it.
• DVCSes are perfect for this:




                             ...
Use a good VCS
• Subversion vendor branches don’t cut it.
• DVCSes are perfect for this:
 • Mercurial queues.




        ...
Use a good VCS
• Subversion vendor branches don’t cut it.
• DVCSes are perfect for this:
 • Mercurial queues.
 • Git rebas...
Use a good VCS
• Subversion vendor branches don’t cut it.
• DVCSes are perfect for this:
 • Mercurial queues.
 • Git rebas...
Be a good citizen




                    86
Be a good citizen

• If you change someone else’s code, let
  them know.




                                           86
Be a good citizen

• If you change someone else’s code, let
  them know.
• Maybe they’ll merge your changes in and
  you w...
Further reading




                  87
Deployment



             88
Deployment should...




                       89
Deployment should...
• Be automated.




                       89
Deployment should...
• Be automated.
• Automatically manage dependencies.




                                       89
Deployment should...
• Be automated.
• Automatically manage dependencies.
• Be isolated.




                             ...
Deployment should...
• Be automated.
• Automatically manage dependencies.
• Be isolated.
• Be repeatable.




            ...
Deployment should...
• Be automated.
• Automatically manage dependencies.
• Be isolated.
• Be repeatable.
• Be identical i...
Deployment should...
• Be automated.
• Automatically manage dependencies.
• Be isolated.
• Be repeatable.
• Be identical i...
Dependency
             Isolation   Automation
management




                                      90
Dependency
             Isolation   Automation
management




                                      90
Dependency
               Isolation      Automation
management

 apt/yum/...   virtualenv     Capistrano


easy_install   ...
Dependency
               Isolation      Automation
management

 apt/yum/...   virtualenv     Capistrano


easy_install   ...
Dependency
               Isolation      Automation
management

 apt/yum/...   virtualenv     Capistrano


easy_install   ...
Dependancy management




                    91
Dependancy management

• The Python ecosystem rocks!




                                91
Dependancy management

• The Python ecosystem rocks!
• Python package management doesn’t.




                            ...
Dependancy management

• The Python ecosystem rocks!
• Python package management doesn’t.
• Installing packages — and depe...
Dependancy management

• The Python ecosystem rocks!
• Python package management doesn’t.
• Installing packages — and depe...
Vendor packages




                  92
Vendor packages

• APT, Yum, …




                  92
Vendor packages

• APT, Yum, …
• The good: familiar tools; stability; handles
  dependancies not on PyPI.




            ...
Vendor packages

• APT, Yum, …
• The good: familiar tools; stability; handles
  dependancies not on PyPI.
• The bad: small...
Vendor packages

• APT, Yum, …
• The good: familiar tools; stability; handles
  dependancies not on PyPI.
• The bad: small...
easy_install




               93
easy_install

• The good: multi-version packages.




                                      93
easy_install

• The good: multi-version packages.
• The bad: requires ‘net connection; can’t
  uninstall; can’t handle non...
easy_install

• The good: multi-version packages.
• The bad: requires ‘net connection; can’t
  uninstall; can’t handle non...
pip
http://pip.openplans.org/




                            94
pip
http://pip.openplans.org/

 • “Pip Installs Packages”




                             94
pip
http://pip.openplans.org/

 • “Pip Installs Packages”
 • The good: Just Works™; handles non-
   PyPI packages (includi...
pip
http://pip.openplans.org/

 • “Pip Installs Packages”
 • The good: Just Works™; handles non-
   PyPI packages (includi...
pip
http://pip.openplans.org/

 • “Pip Installs Packages”
 • The good: Just Works™; handles non-
   PyPI packages (includi...
zc.buildout
http://buildout.org/




                       95
zc.buildout
http://buildout.org/

 • The good: incredibly flexible; handles any
   sort of dependancy; repeatable builds;
 ...
zc.buildout
http://buildout.org/

 • The good: incredibly flexible; handles any
   sort of dependancy; repeatable builds;
 ...
zc.buildout
http://buildout.org/

 • The good: incredibly flexible; handles any
   sort of dependancy; repeatable builds;
 ...
Package isolation




                    96
Package isolation

• Why?




                    96
Package isolation

• Why?
 • Site A requires Foo v1.0; site B requires
   Foo v2.0.




                                  ...
Package isolation

• Why?
 • Site A requires Foo v1.0; site B requires
   Foo v2.0.
 • You need to develop against multipl...
Package isolation tools




                          97
Package isolation tools
• Virtual machines (Xen, VMWare, EC2, …)




                                           97
Package isolation tools
• Virtual machines (Xen, VMWare, EC2, …)
• Multiple Python installations.




                    ...
Package isolation tools
• Virtual machines (Xen, VMWare, EC2, …)
• Multiple Python installations.
• “Virtual” Python insta...
Package isolation tools
• Virtual machines (Xen, VMWare, EC2, …)
• Multiple Python installations.
• “Virtual” Python insta...
Package isolation tools
• Virtual machines (Xen, VMWare, EC2, …)
• Multiple Python installations.
• “Virtual” Python insta...
Why automate?




                98
Why automate?
• “I can’t push this fix to the servers until
  Alex gets back from lunch.”




                             ...
Why automate?
• “I can’t push this fix to the servers until
  Alex gets back from lunch.”
• “Sorry, I can’t fix that. I’m ne...
Why automate?
• “I can’t push this fix to the servers until
  Alex gets back from lunch.”
• “Sorry, I can’t fix that. I’m ne...
Why automate?
• “I can’t push this fix to the servers until
  Alex gets back from lunch.”
• “Sorry, I can’t fix that. I’m ne...
Automation basics




                    99
Automation basics


• SSH is right out.




                      99
Automation basics


• SSH is right out.
• Don’t futz with the server. Write a recipe.




                                ...
Automation basics


• SSH is right out.
• Don’t futz with the server. Write a recipe.
• Deploys should be idempotent.




...
Capistrano
http://capify.org/




                     100
Capistrano
http://capify.org/



 • The good: lots of features; good
   documentation; active community.




             ...
Capistrano
http://capify.org/



 • The good: lots of features; good
   documentation; active community.
 • The bad: stale...
Fabric
http://fabfile.org/




                     101
Fabric
http://fabfile.org/




 • The good: very simple; flexible; actively
   developed; Python.




                      ...
Fabric
http://fabfile.org/




 • The good: very simple; flexible; actively
   developed; Python.
 • The bad: no high-level ...
Configuration management




                          102
Configuration management


• CFEngine, Puppet, Chef, …




                              102
Configuration management


• CFEngine, Puppet, Chef, …
• Will handle a lot more than code
  deployment!




               ...
Configuration management


• CFEngine, Puppet, Chef, …
• Will handle a lot more than code
  deployment!
• I only know a lit...
Recommendations
  Pip, Virtualenv, and Fabric

  Buildout and Fabric.

  Buildout and Puppet/Chef/….

  Utility computing ...
Production
environments


               104
net.
                            LiveJournal Backend: Today
                                                           (Ro...
django

database

 media
 server




           106
Application servers




                      107
Application servers

• Apache + mod_python




                        107
Application servers

• Apache + mod_python
• Apache + mod_wsgi




                        107
Application servers

• Apache + mod_python
• Apache + mod_wsgi
• Apache/lighttpd + FastCGI




                           ...
Application servers

• Apache + mod_python
• Apache + mod_wsgi
• Apache/lighttpd + FastCGI
• SCGI, AJP, nginx/mod_wsgi, .....
Use mod_wsgi



               108
WSGIScriptAlias / /home/mysite/mysite.wsgi




                                             109
import os, sys

# Add to PYTHONPATH whatever you need
sys.path.append('/usr/local/django')

# Set DJANGO_SETTINGS_MODULE
o...
“Scale”



          111
Does this scale?

       django

      database

       media
       server




                   112
Does this scale?

       django

      database

       media
       server




    Maybe!
                   112
Things per secong




                    Number of things


                                       113
Real-world example
      Database A

      175 req/s


      Database B

       75 req/s


                     114
Real-world example




    http://tweakers.net/reviews/657/6
                                        115
django

    media
  web server



  database

database server




                  116
Why separate hardware?




                     117
Why separate hardware?


• Resource contention




                        117
Why separate hardware?


• Resource contention
• Separate performance concerns




                                  117
Why separate hardware?


• Resource contention
• Separate performance concerns
• 0 → 1 is much harder than 1 → N




     ...
DATABASE_HOST = '10.0.0.100'




                               118
DATABASE_HOST = '10.0.0.100'




                 FAIL          118
Connection middleware




                        119
Connection middleware
• Proxy between web and database layers




                                          119
Connection middleware
• Proxy between web and database layers
• Most implement hot fallover and
  connection pooling




 ...
Connection middleware
• Proxy between web and database layers
• Most implement hot fallover and
  connection pooling
 • So...
Connection middleware
• Proxy between web and database layers
• Most implement hot fallover and
  connection pooling
 • So...
Connection middleware




                        120
Connection middleware

• PostgreSQL: pgpool




                        120
Connection middleware

• PostgreSQL: pgpool
• MySQL: MySQL Proxy




                        120
Connection middleware

• PostgreSQL: pgpool
• MySQL: MySQL Proxy
• Database-agnostic: sqlrelay




                       ...
Connection middleware

• PostgreSQL: pgpool
• MySQL: MySQL Proxy
• Database-agnostic: sqlrelay
• Oracle: ?



            ...
django           media
  web server      media server




  database
database server




                                 ...
Media server traits




                      122
Media server traits

• Fast




                      122
Media server traits

• Fast
• Lightweight




                      122
Media server traits

• Fast
• Lightweight
• Optimized for high concurrency




                                   122
Media server traits

• Fast
• Lightweight
• Optimized for high concurrency
• Low memory overhead




                     ...
Media server traits

• Fast
• Lightweight
• Optimized for high concurrency
• Low memory overhead
• Good HTTP citizen


   ...
Media servers




                123
Media servers

• Apache?




                123
Media servers

• Apache?
• lighttpd




                123
Media servers

• Apache?
• lighttpd
• nginx




                123
Media servers

• Apache?
• lighttpd
• nginx
• S3



                123
The absolute minimum


        django           media
       web server      media server




       database
     databas...
The absolute minimum


      django                 media




     database


                web server




             ...
proxy                media

           load balancer         media server




django        django          django
       ...
Why load balancers?



                      127
Load balancer traits




                       128
Load balancer traits

• Low memory overhead




                        128
Load balancer traits

• Low memory overhead
• High concurrency




                        128
Load balancer traits

• Low memory overhead
• High concurrency
• Hot fallover




                        128
Load balancer traits

• Low memory overhead
• High concurrency
• Hot fallover
• Other nifty features...



               ...
Load balancers




                 129
Load balancers

• Apache + mod_proxy




                       129
Load balancers

• Apache + mod_proxy
• perlbal




                       129
Load balancers

• Apache + mod_proxy
• perlbal
• nginx




                       129
Load balancers

• Apache + mod_proxy
• perlbal
• nginx
• Varnish




                       129
Load balancers

• Apache + mod_proxy
• perlbal
• nginx
• Varnish
• Squid


                       129
CREATE POOL mypool
  POOL mypool ADD 10.0.0.100
  POOL mypool ADD 10.0.0.101

CREATE SERVICE mysite
  SET listen = my.publ...
you@yourserver:~$ telnet localhost 60000

pool mysite add 10.0.0.102
OK

nodes 10.0.0.101
10.0.0.101 lastresponse 12379874...
proxy             proxy              proxy     media           media

           load balancing cluster                med...
“Shared nothing”



                   133
BALANCE = None

def balance_sheet(request):
  global BALANCE
  if not BALANCE:
      bank = Bank.objects.get(...)
      BA...
BALANCE = None

def balance_sheet(request):
  global BALANCE
  if not BALANCE:
      bank = Bank.objects.get(...)
      BA...
Global variables are
     right out


                       135
from django.cache import cache

def balance_sheet(request):
  balance = cache.get('bank_balance')
  if not balance:
     b...
from django.cache import cache

def balance_sheet(request):
  balance = cache.get('bank_balance')
  if not balance:
     b...
def generate_report(request):
  report = get_the_report()
  open('/tmp/report.txt', 'w').write(report)
  return redirect(v...
def generate_report(request):
  report = get_the_report()
  open('/tmp/report.txt', 'w').write(report)
  return redirect(v...
Filesystem?
What filesystem?


                  138
Further reading




                  139
Further reading

• Cal Henderson, Building Scalable Web Sites




                                               139
Further reading

• Cal Henderson, Building Scalable Web Sites
• John Allspaw, The Art of Capacity Planning




           ...
Further reading

• Cal Henderson, Building Scalable Web Sites
• John Allspaw, The Art of Capacity Planning
• http://kitche...
Further reading

• Cal Henderson, Building Scalable Web Sites
• John Allspaw, The Art of Capacity Planning
• http://kitche...
Monitoring



             140
Goals




        141
Goals
• When the site goes down, know it immediately.




                                                  141
Goals
• When the site goes down, know it immediately.
• Automatically handle common sources of
  downtime.




           ...
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Django In The Real World
Upcoming SlideShare
Loading in...5
×

Django In The Real World

9,637

Published on

There’s plenty of material (documentation, blogs, books) out there that’ll help you write a site using Django… but then what? You’ve still got to test, deploy, monitor, and tune the site; failure at deployment time means all your beautiful code is for naught.

This tutorial examines how best to cope when the Real World intrudes on your carefully designed website.

1 Comment
65 Likes
Statistics
Notes
No Downloads
Views
Total Views
9,637
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
1
Likes
65
Embeds 0
No embeds

No notes for slide
  • Oh hai. I’m Jacob Kaplan-Moss; here’s a few ways to get in touch with me.

    I wear quite a few hats. Today, I’m wearing two: I’m one of the lead developers of Django; I’m a partner at `Revolution Systems`__, a consulting business specializing in, well, the stuff I’m about to talk about.
  • Just a brief shameless plug: my day job is as a consultant working in and around these issues. I get to come talk about this stuff publicly because of this gig, so If you need info specific to your situation please consider giving us a call!
  • Credit where credit is due: I first gave this talk at PyCon 2009 with the help of `James Bennett`__, so much of my thinking and some of the content comes from James.

    __ http://b-list.org/
  • This is the main theme of this tutorial: you’ve written a Django site...
  • ... and you’ve barely started.

    Writing the app is the easy part; what comes next is seriously complicated.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • Here’s just one list (Leonard Lin’s) of what you might need to think about *after* writing your app; I could easily add another dozen more things to this list. Luckily, not every site needs *all* of this... but any site of any size will need to understand at last some of this stuff, and bigger deployments will need to worry about all of it.

    Obviously this is far too much to cover even in a long tutorial, so we’ll pick and choose; the point is that writing the app is usually the easy part. Today we’re talking about what comes next.
  • So here’s the bare minimum you’ll need to think about:

    * You need to test your sites. Without tests, you can’t move forward without downtime.
    * You need to write your code in a way that makes deployment possible — and easy.
    * You need to use deployment tools that automate the repetitive bits and keep you from shooting yourself in the foot.
    * You need a production environment that scales with your traffic.
    * You need to watch your sites in action so that you know what’s up.
    * When things go slow you need to fix it.
  • So here’s the bare minimum you’ll need to think about:

    * You need to test your sites. Without tests, you can’t move forward without downtime.
    * You need to write your code in a way that makes deployment possible — and easy.
    * You need to use deployment tools that automate the repetitive bits and keep you from shooting yourself in the foot.
    * You need a production environment that scales with your traffic.
    * You need to watch your sites in action so that you know what’s up.
    * When things go slow you need to fix it.
  • So here’s the bare minimum you’ll need to think about:

    * You need to test your sites. Without tests, you can’t move forward without downtime.
    * You need to write your code in a way that makes deployment possible — and easy.
    * You need to use deployment tools that automate the repetitive bits and keep you from shooting yourself in the foot.
    * You need a production environment that scales with your traffic.
    * You need to watch your sites in action so that you know what’s up.
    * When things go slow you need to fix it.
  • So here’s the bare minimum you’ll need to think about:

    * You need to test your sites. Without tests, you can’t move forward without downtime.
    * You need to write your code in a way that makes deployment possible — and easy.
    * You need to use deployment tools that automate the repetitive bits and keep you from shooting yourself in the foot.
    * You need a production environment that scales with your traffic.
    * You need to watch your sites in action so that you know what’s up.
    * When things go slow you need to fix it.
  • So here’s the bare minimum you’ll need to think about:

    * You need to test your sites. Without tests, you can’t move forward without downtime.
    * You need to write your code in a way that makes deployment possible — and easy.
    * You need to use deployment tools that automate the repetitive bits and keep you from shooting yourself in the foot.
    * You need a production environment that scales with your traffic.
    * You need to watch your sites in action so that you know what’s up.
    * When things go slow you need to fix it.
  • So here’s the bare minimum you’ll need to think about:

    * You need to test your sites. Without tests, you can’t move forward without downtime.
    * You need to write your code in a way that makes deployment possible — and easy.
    * You need to use deployment tools that automate the repetitive bits and keep you from shooting yourself in the foot.
    * You need a production environment that scales with your traffic.
    * You need to watch your sites in action so that you know what’s up.
    * When things go slow you need to fix it.
  • So now you’ve got a working app, but how do you *know* that it works? Sure, you could just run the site and click around, but that guarantees nothing. The difference between stable and unstable code is often the quality of the tests.

    The “fear” that Beck is talking about here is the fear of not knowing if your new site will run or not. Testing, while often boring, relives you of that fear.
  • `Test Driven Development`__ by Kent Beck is the bible of TDD. Beck requires that you only add new code to your project after an existing test fails. This means writing tests before writing any new code. It can be rewarding -- particularly if you’re pair programming so you can keep each other honest. Unfortunately, it’s also simply not practical for most developers.

    __ http://www.amazon.com/dp/0321146530/
  • This is the pragmatic approach to testing: write tests when you need them. If you need to chase down a bug, write a test to trigger the bug, then when the test passes you know the bug is licked.

    Yes, this leaves you with less than perfect coverage.
  • I’m speaking from experience here. Once you commit with even a *single* breaking test, finding the time to clean up is an uphill battle. Knowing the suite fails discourages you from even running the tests at all, and thus the quality continues to decline.
  • I tend to think of this as a continuum of testing; another way of thinking about this is a continuum between internal/external, or whitebox/blackbox.
  • I tend to think of this as a continuum of testing; another way of thinking about this is a continuum between internal/external, or whitebox/blackbox.
  • I tend to think of this as a continuum of testing; another way of thinking about this is a continuum between internal/external, or whitebox/blackbox.
  • I tend to think of this as a continuum of testing; another way of thinking about this is a continuum between internal/external, or whitebox/blackbox.
  • Testing web applications is *hard*. Anything with a persistent state -- i.e. a database -- and network communications -- HTTP -- is a real pain to test.

    Ruby on Rails really changed the game, though. When it shipped a set of tools that took most of the pain out of automating web tests, it galvenized developers to compete.

    Django’s since come out with our own spin on this set of tools. I’m only going to cover doctests in this intro tutorial, so a brief overview of the other tools is in order:

    * Unit tests: Django can run tests using Python’s standard `unittest` framework, which is based on Java’s JUnit. Perfect for more formal unit tests like you’d see in Java/C#.
    * Fixtures: fixtures are a way of loading preset data into the database for each test, thus letting you test against “real” data.
    * The test client: this lets you “fake” a request to your views and inspect the returned response, template, and context.
    * Email capture: the test suite will intercept sent email so you can test logic involving email.
  • Testing web applications is *hard*. Anything with a persistent state -- i.e. a database -- and network communications -- HTTP -- is a real pain to test.

    Ruby on Rails really changed the game, though. When it shipped a set of tools that took most of the pain out of automating web tests, it galvenized developers to compete.

    Django’s since come out with our own spin on this set of tools. I’m only going to cover doctests in this intro tutorial, so a brief overview of the other tools is in order:

    * Unit tests: Django can run tests using Python’s standard `unittest` framework, which is based on Java’s JUnit. Perfect for more formal unit tests like you’d see in Java/C#.
    * Fixtures: fixtures are a way of loading preset data into the database for each test, thus letting you test against “real” data.
    * The test client: this lets you “fake” a request to your views and inspect the returned response, template, and context.
    * Email capture: the test suite will intercept sent email so you can test logic involving email.
  • Testing web applications is *hard*. Anything with a persistent state -- i.e. a database -- and network communications -- HTTP -- is a real pain to test.

    Ruby on Rails really changed the game, though. When it shipped a set of tools that took most of the pain out of automating web tests, it galvenized developers to compete.

    Django’s since come out with our own spin on this set of tools. I’m only going to cover doctests in this intro tutorial, so a brief overview of the other tools is in order:

    * Unit tests: Django can run tests using Python’s standard `unittest` framework, which is based on Java’s JUnit. Perfect for more formal unit tests like you’d see in Java/C#.
    * Fixtures: fixtures are a way of loading preset data into the database for each test, thus letting you test against “real” data.
    * The test client: this lets you “fake” a request to your views and inspect the returned response, template, and context.
    * Email capture: the test suite will intercept sent email so you can test logic involving email.
  • Testing web applications is *hard*. Anything with a persistent state -- i.e. a database -- and network communications -- HTTP -- is a real pain to test.

    Ruby on Rails really changed the game, though. When it shipped a set of tools that took most of the pain out of automating web tests, it galvenized developers to compete.

    Django’s since come out with our own spin on this set of tools. I’m only going to cover doctests in this intro tutorial, so a brief overview of the other tools is in order:

    * Unit tests: Django can run tests using Python’s standard `unittest` framework, which is based on Java’s JUnit. Perfect for more formal unit tests like you’d see in Java/C#.
    * Fixtures: fixtures are a way of loading preset data into the database for each test, thus letting you test against “real” data.
    * The test client: this lets you “fake” a request to your views and inspect the returned response, template, and context.
    * Email capture: the test suite will intercept sent email so you can test logic involving email.
  • Testing web applications is *hard*. Anything with a persistent state -- i.e. a database -- and network communications -- HTTP -- is a real pain to test.

    Ruby on Rails really changed the game, though. When it shipped a set of tools that took most of the pain out of automating web tests, it galvenized developers to compete.

    Django’s since come out with our own spin on this set of tools. I’m only going to cover doctests in this intro tutorial, so a brief overview of the other tools is in order:

    * Unit tests: Django can run tests using Python’s standard `unittest` framework, which is based on Java’s JUnit. Perfect for more formal unit tests like you’d see in Java/C#.
    * Fixtures: fixtures are a way of loading preset data into the database for each test, thus letting you test against “real” data.
    * The test client: this lets you “fake” a request to your views and inspect the returned response, template, and context.
    * Email capture: the test suite will intercept sent email so you can test logic involving email.
  • Django In The Real World

    1. 1. Django in the Real World Jacob Kaplan-Moss OSCON 2009 http://jacobian.org/TN
    2. 2. Jacob Kaplan-Moss http://jacobian.org / jacob@jacobian.org / @jacobian Lead Developer, Django Partner, Revolution Systems 2
    3. 3. Shameless plug: http://revsys.com/ 3
    4. 4. Hat tip: James Bennett (http://b-list.org) 4
    5. 5. So you’ve written a Django site… 5
    6. 6. … now what? 6
    7. 7. http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    8. 8. • API Metering http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    9. 9. • API Metering • Backups & Snapshots http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    10. 10. • API Metering • Backups & Snapshots • Counters http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    11. 11. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    12. 12. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    13. 13. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    14. 14. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    15. 15. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    16. 16. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    17. 17. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    18. 18. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    19. 19. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    20. 20. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    21. 21. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    22. 22. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    23. 23. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    24. 24. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    25. 25. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    26. 26. • API Metering • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    27. 27. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    28. 28. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    29. 29. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    30. 30. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    31. 31. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Memory Caching • Failover • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    32. 32. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Memory Caching • Failover • Non-relational Key Stores • Node addition/removal and hashing • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    33. 33. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Memory Caching • Failover • Non-relational Key Stores • Node addition/removal and hashing • Rate Limiting • Auto-scaling for cloud resources • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    34. 34. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Memory Caching • Failover • Non-relational Key Stores • Node addition/removal and hashing • Rate Limiting • Auto-scaling for cloud resources • Relational Storage • CSRF/XSS Protection • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    35. 35. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Memory Caching • Failover • Non-relational Key Stores • Node addition/removal and hashing • Rate Limiting • Auto-scaling for cloud resources • Relational Storage • CSRF/XSS Protection • Queues • Data Retention/Archival • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    36. 36. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Memory Caching • Failover • Non-relational Key Stores • Node addition/removal and hashing • Rate Limiting • Auto-scaling for cloud resources • Relational Storage • CSRF/XSS Protection • Queues • Data Retention/Archival • Rate Limiting • Deployment Tools • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    37. 37. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Memory Caching • Failover • Non-relational Key Stores • Node addition/removal and hashing • Rate Limiting • Auto-scaling for cloud resources • Relational Storage • CSRF/XSS Protection • Queues • Data Retention/Archival • Rate Limiting • Deployment Tools • Real-time messaging (XMPP) • Multiple Devs, Staging, Prod • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    38. 38. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Memory Caching • Failover • Non-relational Key Stores • Node addition/removal and hashing • Rate Limiting • Auto-scaling for cloud resources • Relational Storage • CSRF/XSS Protection • Queues • Data Retention/Archival • Rate Limiting • Deployment Tools • Real-time messaging (XMPP) • Multiple Devs, Staging, Prod • Search • Data model upgrades • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    39. 39. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Memory Caching • Failover • Non-relational Key Stores • Node addition/removal and hashing • Rate Limiting • Auto-scaling for cloud resources • Relational Storage • CSRF/XSS Protection • Queues • Data Retention/Archival • Rate Limiting • Deployment Tools • Real-time messaging (XMPP) • Multiple Devs, Staging, Prod • Search • Data model upgrades • Ranging • Rolling deployments • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    40. 40. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Memory Caching • Failover • Non-relational Key Stores • Node addition/removal and hashing • Rate Limiting • Auto-scaling for cloud resources • Relational Storage • CSRF/XSS Protection • Queues • Data Retention/Archival • Rate Limiting • Deployment Tools • Real-time messaging (XMPP) • Multiple Devs, Staging, Prod • Search • Data model upgrades • Ranging • Rolling deployments • Geo • Multiple versions (selective beta) • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    41. 41. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Memory Caching • Failover • Non-relational Key Stores • Node addition/removal and hashing • Rate Limiting • Auto-scaling for cloud resources • Relational Storage • CSRF/XSS Protection • Queues • Data Retention/Archival • Rate Limiting • Deployment Tools • Real-time messaging (XMPP) • Multiple Devs, Staging, Prod • Search • Data model upgrades • Ranging • Rolling deployments • Geo • Multiple versions (selective beta) • Sharding • Bucket Testing • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    42. 42. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Memory Caching • Failover • Non-relational Key Stores • Node addition/removal and hashing • Rate Limiting • Auto-scaling for cloud resources • Relational Storage • CSRF/XSS Protection • Queues • Data Retention/Archival • Rate Limiting • Deployment Tools • Real-time messaging (XMPP) • Multiple Devs, Staging, Prod • Search • Data model upgrades • Ranging • Rolling deployments • Geo • Multiple versions (selective beta) • Sharding • Bucket Testing • Smart Caching • Rollbacks • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    43. 43. • API Metering • Distributed Log storage, analysis • Backups & Snapshots • Graphing • Counters • HTTP Caching • Cloud/Cluster Management Tools • Input/Output Filtering • Instrumentation/Monitoring • Memory Caching • Failover • Non-relational Key Stores • Node addition/removal and hashing • Rate Limiting • Auto-scaling for cloud resources • Relational Storage • CSRF/XSS Protection • Queues • Data Retention/Archival • Rate Limiting • Deployment Tools • Real-time messaging (XMPP) • Multiple Devs, Staging, Prod • Search • Data model upgrades • Ranging • Rolling deployments • Geo • Multiple versions (selective beta) • Sharding • Bucket Testing • Smart Caching • Rollbacks • Dirty-table management • CDN Management • Distributed File Storage http://randomfoo.net/2009/01/28/infrastructure-for-modern-web-sites 7
    44. 44. The bare minimum: 8
    45. 45. The bare minimum: • Test. 8
    46. 46. The bare minimum: • Test. • Structure for deployment. 8
    47. 47. The bare minimum: • Test. • Structure for deployment. • Use deployment tools. 8
    48. 48. The bare minimum: • Test. • Structure for deployment. • Use deployment tools. • Design a production environment. 8
    49. 49. The bare minimum: • Test. • Structure for deployment. • Use deployment tools. • Design a production environment. • Monitor. 8
    50. 50. The bare minimum: • Test. • Structure for deployment. • Use deployment tools. • Design a production environment. • Monitor. • Tune. 8
    51. 51. Testing 9
    52. 52. “ Tests are the Programmer’s stone, transmuting fear into ” boredom. — Kent Beck 10
    53. 53. Hardcore TDD 11
    54. 54. “ I don’t do test driven development. I do stupidity driven testing… I wait until I do something stupid, and ” then write tests to avoid doing it again. — Titus Brown 12
    55. 55. Whatever happens, don’t let your test suite break thinking, “I’ll go back and fix this later.” 13
    56. 56. Unit testing Functional/behavior testing Browser testing 14
    57. 57. Unit testing unittest Functional/behavior testing Browser testing 14
    58. 58. Unit testing unittest doctest Functional/behavior testing Browser testing 14
    59. 59. Unit testing unittest doctest Functional/behavior testing django.test.Client, Twill Browser testing 14
    60. 60. Unit testing unittest doctest Functional/behavior testing django.test.Client, Twill Browser testing Windmill, Selenium 14
    61. 61. You need them all. 15
    62. 62. Testing Django 16
    63. 63. Testing Django • Unit tests (unittest) 16
    64. 64. Testing Django • Unit tests (unittest) • Doctests (doctest) 16
    65. 65. Testing Django • Unit tests (unittest) • Doctests (doctest) • Fixtures 16
    66. 66. Testing Django • Unit tests (unittest) • Doctests (doctest) • Fixtures • Test client 16
    67. 67. Testing Django • Unit tests (unittest) • Doctests (doctest) • Fixtures • Test client • Email capture 16
    68. 68. Unit tests 17
    69. 69. Unit tests • “Whitebox” testing 17
    70. 70. Unit tests • “Whitebox” testing • Verify the small functional units of your app 17
    71. 71. Unit tests • “Whitebox” testing • Verify the small functional units of your app • Very fine-grained 17
    72. 72. Unit tests • “Whitebox” testing • Verify the small functional units of your app • Very fine-grained • Familier to most programmers (JUnit, NUnit, etc.) 17
    73. 73. Unit tests • “Whitebox” testing • Verify the small functional units of your app • Very fine-grained • Familier to most programmers (JUnit, NUnit, etc.) • Provided in Python by unittest 17
    74. 74. django.test.TestCase 18
    75. 75. django.test.TestCase • Fixtures. 18
    76. 76. django.test.TestCase • Fixtures. • Test client. 18
    77. 77. django.test.TestCase • Fixtures. • Test client. • Email capture. 18
    78. 78. django.test.TestCase • Fixtures. • Test client. • Email capture. • Database management. 18
    79. 79. django.test.TestCase • Fixtures. • Test client. • Email capture. • Database management. • Slower than unittest.TestCase. 18
    80. 80. class StoryAddViewTests(TestCase): fixtures = ['authtestdata', 'newsbudget_test_data'] urls = 'newsbudget.urls' def test_story_add_get(self): r = self.client.get('/budget/stories/add/') self.assertEqual(r.status_code, 200) … def test_story_add_post(self): data = { 'title': 'Hungry cat is hungry', 'date': '2009-01-01', } r = self.client.post('/budget/stories/add/', data) self.assertEqual(r.status_code, 302) … 19
    81. 81. Doctests 20
    82. 82. Doctests • Easy to write & read. 20
    83. 83. Doctests • Easy to write & read. • Produces self-documenting code. 20
    84. 84. Doctests • Easy to write & read. • Produces self-documenting code. • Great for cases that only use assertEquals. 20
    85. 85. Doctests • Easy to write & read. • Produces self-documenting code. • Great for cases that only use assertEquals. • Somewhere between unit tests and functional tests. 20
    86. 86. Doctests • Easy to write & read. • Produces self-documenting code. • Great for cases that only use assertEquals. • Somewhere between unit tests and functional tests. • Difficult to debug. 20
    87. 87. Doctests • Easy to write & read. • Produces self-documenting code. • Great for cases that only use assertEquals. • Somewhere between unit tests and functional tests. • Difficult to debug. • Don’t always provide useful test failures. 20
    88. 88. class Choices(object): """ Easy declarative "choices" tool:: >>> STATUSES = Choices("Live", "Draft") # Acts like a choices list: >>> list(STATUSES) [(1, 'Live'), (2, 'Draft')] # Easily convert from code to verbose: >>> STATUSES.verbose(1) 'Live' # ... and vice versa: >>> STATUSES.code("Draft") 2 """ … 21
    89. 89. **************************************************** File "utils.py", line 150, in __main__.Choices Failed example: STATUSES.verbose(1) Expected: 'Live' Got: 'Draft' **************************************************** 22
    90. 90. Functional tests 23
    91. 91. Functional tests • a.k.a “Behavior Driven Development.” 23
    92. 92. Functional tests • a.k.a “Behavior Driven Development.” • “Blackbox,” holistic testing. 23
    93. 93. Functional tests • a.k.a “Behavior Driven Development.” • “Blackbox,” holistic testing. • All the hardcore TDD folks look down on functional tests. 23
    94. 94. Functional tests • a.k.a “Behavior Driven Development.” • “Blackbox,” holistic testing. • All the hardcore TDD folks look down on functional tests. • But they keep your boss happy. 23
    95. 95. Functional tests • a.k.a “Behavior Driven Development.” • “Blackbox,” holistic testing. • All the hardcore TDD folks look down on functional tests. • But they keep your boss happy. • Easy to find problems; harder to find the actual bug. 23
    96. 96. Functional testing tools 24
    97. 97. Functional testing tools • django.test.Client 24
    98. 98. Functional testing tools • django.test.Client • webunit 24
    99. 99. Functional testing tools • django.test.Client • webunit • Twill 24
    100. 100. Functional testing tools • django.test.Client • webunit • Twill • ... 24
    101. 101. django.test.Client 25
    102. 102. django.test.Client • Test the whole request path without running a web server. 25
    103. 103. django.test.Client • Test the whole request path without running a web server. • Responses provide extra information about templates and their contexts. 25
    104. 104. class StoryAddViewTests(TestCase): fixtures = ['authtestdata', 'newsbudget_test_data'] urls = 'newsbudget.urls' def test_story_add_get(self): r = self.client.get('/budget/stories/add/') self.assertEqual(r.status_code, 200) … def test_story_add_post(self): data = { 'title': 'Hungry cat is hungry', 'date': '2009-01-01', } r = self.client.post('/budget/stories/add/', data) self.assertEqual(r.status_code, 302) … 26
    105. 105. Web browser testing 27
    106. 106. Web browser testing • The ultimate in functional testing for web applications. 27
    107. 107. Web browser testing • The ultimate in functional testing for web applications. • Run test in a web browser. 27
    108. 108. Web browser testing • The ultimate in functional testing for web applications. • Run test in a web browser. • Can verify JavaScript, AJAX; even CSS. 27
    109. 109. Web browser testing • The ultimate in functional testing for web applications. • Run test in a web browser. • Can verify JavaScript, AJAX; even CSS. • Test your site across supported browsers. 27
    110. 110. Browser testing tools 28
    111. 111. Browser testing tools • Selenium 28
    112. 112. Browser testing tools • Selenium • Windmill 28
    113. 113. “Exotic” testing 29
    114. 114. “Exotic” testing • Static source analysis. 29
    115. 115. “Exotic” testing • Static source analysis. • Smoke testing (crawlers and spiders). 29
    116. 116. “Exotic” testing • Static source analysis. • Smoke testing (crawlers and spiders). • Monkey testing. 29
    117. 117. “Exotic” testing • Static source analysis. • Smoke testing (crawlers and spiders). • Monkey testing. • Load testing. 29
    118. 118. “Exotic” testing • Static source analysis. • Smoke testing (crawlers and spiders). • Monkey testing. • Load testing. • ... 29
    119. 119. 30
    120. 120. Further resources 31
    121. 121. Further resources • Windmill talk here at OSCON http://bit.ly/14tkrd 31
    122. 122. Further resources • Windmill talk here at OSCON http://bit.ly/14tkrd • Django testing documentation http://bit.ly/django-testing 31
    123. 123. Further resources • Windmill talk here at OSCON http://bit.ly/14tkrd • Django testing documentation http://bit.ly/django-testing • Python Testing Tools Taxonomy http://bit.ly/py-testing-tools 31
    124. 124. Structuring applications for reuse 32
    125. 125. Designing for reuse 33
    126. 126. Designing for reuse • Do one thing, and do it well. 33
    127. 127. Designing for reuse • Do one thing, and do it well. • Don’t be afraid of multiple apps. 33
    128. 128. Designing for reuse • Do one thing, and do it well. • Don’t be afraid of multiple apps. • Write for flexibility. 33
    129. 129. Designing for reuse • Do one thing, and do it well. • Don’t be afraid of multiple apps. • Write for flexibility. • Build to distribute. 33
    130. 130. Designing for reuse • Do one thing, and do it well. • Don’t be afraid of multiple apps. • Write for flexibility. • Build to distribute. • Extend carefully. 33
    131. 131. 1. Do one thing, and do it well. 34
    132. 132. Application == encapsulation 35
    133. 133. Focus 36
    134. 134. Focus • Ask yourself: “What does this application do?” 36
    135. 135. Focus • Ask yourself: “What does this application do?” • Answer should be one or two short sentences. 36
    136. 136. Good focus 37
    137. 137. Good focus • “Handle storage of users and authentication of their identities.” 37
    138. 138. Good focus • “Handle storage of users and authentication of their identities.” • “Allow content to be tagged, del.icio.us style, with querying by tags.” 37
    139. 139. Good focus • “Handle storage of users and authentication of their identities.” • “Allow content to be tagged, del.icio.us style, with querying by tags.” • “Handle entries in a weblog.” 37
    140. 140. Bad focus 38
    141. 141. Bad focus • “Handle entries in a weblog, and users who post them, and their authentication, and tagging and categorization, and some flat pages for static content, and...” 38
    142. 142. Warning signs 39
    143. 143. Warning signs • Lots of files. 39
    144. 144. Warning signs • Lots of files. • Lots of modules. 39
    145. 145. Warning signs • Lots of files. • Lots of modules. • Lots of models. 39
    146. 146. Warning signs • Lots of files. • Lots of modules. • Lots of models. • Lots of code. 39
    147. 147. Small is good 40
    148. 148. Small is good • Many great Django apps are very small. 40
    149. 149. Small is good • Many great Django apps are very small. • Even a lot of “simple” Django sites commonly have a dozen or more applications in INSTALLED_APPS. 40
    150. 150. Small is good • Many great Django apps are very small. • Even a lot of “simple” Django sites commonly have a dozen or more applications in INSTALLED_APPS. • If you’ve got a complex site and a short application list, something’s probably wrong. 40
    151. 151. Approach features skeptically 41
    152. 152. Approach features skeptically • What does the application do? 41
    153. 153. Approach features skeptically • What does the application do? • Does this feature have anything to do with that? 41
    154. 154. Approach features skeptically • What does the application do? • Does this feature have anything to do with that? • No? Don’t add it. 41
    155. 155. 2. Don’t be afraid of many apps. 42
    156. 156. The monolith anti-pattern 43
    157. 157. The monolith anti-pattern • The “application” is the whole site. 43
    158. 158. The monolith anti-pattern • The “application” is the whole site. • Re-use? YAGNI. 43
    159. 159. The monolith anti-pattern • The “application” is the whole site. • Re-use? YAGNI. • Plugins that hook into the “main” application. 43
    160. 160. The monolith anti-pattern • The “application” is the whole site. • Re-use? YAGNI. • Plugins that hook into the “main” application. • Heavy use of middleware-like concepts. 43
    161. 161. (I blame Rails) 44
    162. 162. The Django mindset 45
    163. 163. The Django mindset • Application: some bit of functionality. 45
    164. 164. The Django mindset • Application: some bit of functionality. • Site: several applications. 45
    165. 165. The Django mindset • Application: some bit of functionality. • Site: several applications. • Spin off new “apps” liberally. 45
    166. 166. The Django mindset • Application: some bit of functionality. • Site: several applications. • Spin off new “apps” liberally. • Develop a suite of apps ready for when they’re needed. 45
    167. 167. Django encourages this 46
    168. 168. Django encourages this • INSTALLED_APPS 46
    169. 169. Django encourages this • INSTALLED_APPS • Applications are just Python packages, not some Django-specific “app” or “plugin.” 46
    170. 170. Django encourages this • INSTALLED_APPS • Applications are just Python packages, not some Django-specific “app” or “plugin.” • Abstractions like django.contrib.sites make you think about this as you develop. 46
    171. 171. Spin off a new app? 47
    172. 172. Spin off a new app? • Is this feature unrelated to the app’s focus? 47
    173. 173. Spin off a new app? • Is this feature unrelated to the app’s focus? • Is it orthogonal to the rest of the app? 47
    174. 174. Spin off a new app? • Is this feature unrelated to the app’s focus? • Is it orthogonal to the rest of the app? • Will I need similar functionality again? 47
    175. 175. The ideal: 48
    176. 176. I need a contact form 49
    177. 177. urlpatterns = ('', … (r'^contact/', include('contact_form.urls')), … ) 50
    178. 178. Done. (http://bitbucket.org/ubernostrum/django-contact-form/) 51
    179. 179. But… what about… 52
    180. 180. But… what about… • Site A wants a contact form that just collects a message. 52
    181. 181. But… what about… • Site A wants a contact form that just collects a message. • Site B’s marketing department wants a bunch of info. 52
    182. 182. But… what about… • Site A wants a contact form that just collects a message. • Site B’s marketing department wants a bunch of info. • Site C wants to use Akismet to filter automated spam. 52
    183. 183. 53
    184. 184. 53
    185. 185. 3. Write for flexibility. 54
    186. 186. Common sense 55
    187. 187. Common sense • Sane defaults. 55
    188. 188. Common sense • Sane defaults. • Easy overrides. 55
    189. 189. Common sense • Sane defaults. • Easy overrides. • Don’t set anything in stone. 55
    190. 190. Forms 56
    191. 191. Forms • Supply a form class. 56
    192. 192. Forms • Supply a form class. • Let users specify their own. 56
    193. 193. Templates 57
    194. 194. Templates • Specify a default template. 57
    195. 195. Templates • Specify a default template. • Let users specify their own. 57
    196. 196. Form processing 58
    197. 197. Form processing • You want to redirect after successful submission. 58
    198. 198. Form processing • You want to redirect after successful submission. • Supply a default URL. 58
    199. 199. Form processing • You want to redirect after successful submission. • Supply a default URL. • (Preferably by using reverse resolution). 58
    200. 200. Form processing • You want to redirect after successful submission. • Supply a default URL. • (Preferably by using reverse resolution). • Let users override the default. 58
    201. 201. def edit_entry(request, entry_id): form = EntryForm(request.POST or None) if form.is_valid(): form.save() return redirect('entry_detail', entry_id) return render_to_response('entry/form.html', {…}) 59
    202. 202. def edit_entry(request, entry_id, form_class=EntryForm, template_name='entry/form.html', post_save_redirect=None): form = form_class(request.POST or None) if form.is_valid(): form.save() if post_save_redirect: return redirect(post_save_redirect) else: return redirect('entry_detail', entry_id) return render_to_response([template_name, 'entry/form.html'], {…}) 60
    203. 203. URLs 61
    204. 204. URLs • Provide a URLConf with all views. 61
    205. 205. URLs • Provide a URLConf with all views. • Use named URL patterns. 61
    206. 206. URLs • Provide a URLConf with all views. • Use named URL patterns. • Use reverse lookups (by name). 61
    207. 207. 4. Build to distribute (even private code). 62
    208. 208. What the tutorial teaches myproject/ settings.py urls.py myapp/ models.py mysecondapp/ views.py … 63
    209. 209. from myproject.myapp.models import … from myproject. myapp.models import … … myproject.settings myproject.urls 64
    210. 210. Project coupling kills re-use 65
    211. 211. Projects in real life. 66
    212. 212. Projects in real life. • A settings module. 66
    213. 213. Projects in real life. • A settings module. • A root URLConf. 66
    214. 214. Projects in real life. • A settings module. • A root URLConf. • Maybe a manage.py (but…) 66
    215. 215. Projects in real life. • A settings module. • A root URLConf. • Maybe a manage.py (but…) • And that’s it. 66
    216. 216. Advantages 67
    217. 217. Advantages • No assumptions about where things live. 67
    218. 218. Advantages • No assumptions about where things live. • No PYTHONPATH magic. 67
    219. 219. Advantages • No assumptions about where things live. • No PYTHONPATH magic. • Reminds you that “projects” are just a Python module. 67
    220. 220. You don’t even need a project 68
    221. 221. ljworld.com: 69
    222. 222. ljworld.com: • worldonline.settings.ljworld 69
    223. 223. ljworld.com: • worldonline.settings.ljworld • worldonline.urls.ljworld 69
    224. 224. ljworld.com: • worldonline.settings.ljworld • worldonline.urls.ljworld • And a whole bunch of apps. 69
    225. 225. Where apps really live 70
    226. 226. Where apps really live • Single module directly on Python path (registration, tagging, etc.). 70
    227. 227. Where apps really live • Single module directly on Python path (registration, tagging, etc.). • Related modules under a top-level package (ellington.events, ellington.podcasts, etc.) 70
    228. 228. Where apps really live • Single module directly on Python path (registration, tagging, etc.). • Related modules under a top-level package (ellington.events, ellington.podcasts, etc.) • No projects (ellington.settings doesn’t exist). 70
    229. 229. Want to distribute? 71
    230. 230. Want to distribute? • Build a package with distutils/setuptools. 71
    231. 231. Want to distribute? • Build a package with distutils/setuptools. • Put it on PyPI (or a private package server). 71
    232. 232. Want to distribute? • Build a package with distutils/setuptools. • Put it on PyPI (or a private package server). • Now it works with easy_install, pip, buildout, … 71
    233. 233. General best practices 72
    234. 234. General best practices • Establish dependancy rules. 72
    235. 235. General best practices • Establish dependancy rules. • Establish a minimum Python version (suggestion: Python 2.5). 72
    236. 236. General best practices • Establish dependancy rules. • Establish a minimum Python version (suggestion: Python 2.5). • Establish a minimum Django version (suggestion: Django 1.0). 72
    237. 237. General best practices • Establish dependancy rules. • Establish a minimum Python version (suggestion: Python 2.5). • Establish a minimum Django version (suggestion: Django 1.0). • Test frequently against new versions of dependancies. 72
    238. 238. Document obsessively. 73
    239. 239. 5. Embrace and extend. 74
    240. 240. Don’t touch! 75
    241. 241. Don’t touch! • Good applications are extensible without patching. 75
    242. 242. Don’t touch! • Good applications are extensible without patching. • Take advantage of every extensibility point an application gives you. 75
    243. 243. Don’t touch! • Good applications are extensible without patching. • Take advantage of every extensibility point an application gives you. • You may end up doing something that deserves a new application anyway. 75
    244. 244. But this application wasn’t meant to be extended! 76
    245. 245. Python Power! 77
    246. 246. Extending a view 78
    247. 247. Extending a view • Wrap the view with your own code. 78
    248. 248. Extending a view • Wrap the view with your own code. • Doing it repetitively? Write a decorator. 78
    249. 249. Extending a model 79
    250. 250. Extending a model • Relate other models to it. 79
    251. 251. Extending a model • Relate other models to it. • Subclass it. 79
    252. 252. Extending a model • Relate other models to it. • Subclass it. • Proxy subclasses (Django 1.1). 79
    253. 253. Extending a form 80
    254. 254. Extending a form • Subclass it. 80
    255. 255. Extending a form • Subclass it. • There is no step 2. 80
    256. 256. Other tricks 81
    257. 257. Other tricks • Signals lets you fire off customized behavior when certain events happen. 81
    258. 258. Other tricks • Signals lets you fire off customized behavior when certain events happen. • Middleware offers full control over request/response handling. 81
    259. 259. Other tricks • Signals lets you fire off customized behavior when certain events happen. • Middleware offers full control over request/response handling. • Context processors can make additional information available if a view doesn’t. 81
    260. 260. If you must make changes to external code… 82
    261. 261. Keep changes to a minimum 83
    262. 262. Keep changes to a minimum • If possible, instead of adding a feature, add extensibility. 83
    263. 263. Keep changes to a minimum • If possible, instead of adding a feature, add extensibility. • Keep as much changed code as you can out of the original app. 83
    264. 264. Stay up-to-date 84
    265. 265. Stay up-to-date • Don’t want to get out of sync with the original version of the code! 84
    266. 266. Stay up-to-date • Don’t want to get out of sync with the original version of the code! • You might miss bugfixes. 84
    267. 267. Stay up-to-date • Don’t want to get out of sync with the original version of the code! • You might miss bugfixes. • You might even miss the feature you needed. 84
    268. 268. Use a good VCS 85
    269. 269. Use a good VCS • Subversion vendor branches don’t cut it. 85
    270. 270. Use a good VCS • Subversion vendor branches don’t cut it. • DVCSes are perfect for this: 85
    271. 271. Use a good VCS • Subversion vendor branches don’t cut it. • DVCSes are perfect for this: • Mercurial queues. 85
    272. 272. Use a good VCS • Subversion vendor branches don’t cut it. • DVCSes are perfect for this: • Mercurial queues. • Git rebasing. 85
    273. 273. Use a good VCS • Subversion vendor branches don’t cut it. • DVCSes are perfect for this: • Mercurial queues. • Git rebasing. • At the very least, maintain a patch queue by hand. 85
    274. 274. Be a good citizen 86
    275. 275. Be a good citizen • If you change someone else’s code, let them know. 86
    276. 276. Be a good citizen • If you change someone else’s code, let them know. • Maybe they’ll merge your changes in and you won’t have to fork anymore. 86
    277. 277. Further reading 87
    278. 278. Deployment 88
    279. 279. Deployment should... 89
    280. 280. Deployment should... • Be automated. 89
    281. 281. Deployment should... • Be automated. • Automatically manage dependencies. 89
    282. 282. Deployment should... • Be automated. • Automatically manage dependencies. • Be isolated. 89
    283. 283. Deployment should... • Be automated. • Automatically manage dependencies. • Be isolated. • Be repeatable. 89
    284. 284. Deployment should... • Be automated. • Automatically manage dependencies. • Be isolated. • Be repeatable. • Be identical in staging and in production. 89
    285. 285. Deployment should... • Be automated. • Automatically manage dependencies. • Be isolated. • Be repeatable. • Be identical in staging and in production. • Work the same for everyone. 89
    286. 286. Dependency Isolation Automation management 90
    287. 287. Dependency Isolation Automation management 90
    288. 288. Dependency Isolation Automation management apt/yum/... virtualenv Capistrano easy_install zc.buildout Fabric pip Puppet/Chef/… zc.buildout 90
    289. 289. Dependency Isolation Automation management apt/yum/... virtualenv Capistrano easy_install zc.buildout Fabric pip Puppet/Chef/… zc.buildout 90
    290. 290. Dependency Isolation Automation management apt/yum/... virtualenv Capistrano easy_install zc.buildout Fabric pip Puppet/Chef/… zc.buildout 90
    291. 291. Dependancy management 91
    292. 292. Dependancy management • The Python ecosystem rocks! 91
    293. 293. Dependancy management • The Python ecosystem rocks! • Python package management doesn’t. 91
    294. 294. Dependancy management • The Python ecosystem rocks! • Python package management doesn’t. • Installing packages — and dependancies — correctly is a lot harder than it should be; most defaults are wrong. 91
    295. 295. Dependancy management • The Python ecosystem rocks! • Python package management doesn’t. • Installing packages — and dependancies — correctly is a lot harder than it should be; most defaults are wrong. • Here be dragons. 91
    296. 296. Vendor packages 92
    297. 297. Vendor packages • APT, Yum, … 92
    298. 298. Vendor packages • APT, Yum, … • The good: familiar tools; stability; handles dependancies not on PyPI. 92
    299. 299. Vendor packages • APT, Yum, … • The good: familiar tools; stability; handles dependancies not on PyPI. • The bad: small selection; not (very) portable; hard to supply user packages. 92
    300. 300. Vendor packages • APT, Yum, … • The good: familiar tools; stability; handles dependancies not on PyPI. • The bad: small selection; not (very) portable; hard to supply user packages. • The ugly: installs packages system-wide. 92
    301. 301. easy_install 93
    302. 302. easy_install • The good: multi-version packages. 93
    303. 303. easy_install • The good: multi-version packages. • The bad: requires ‘net connection; can’t uninstall; can’t handle non-PyPI packages; multi-version packages barely work. 93
    304. 304. easy_install • The good: multi-version packages. • The bad: requires ‘net connection; can’t uninstall; can’t handle non-PyPI packages; multi-version packages barely work. • The ugly: stale; unsupported; defaults almost totally wrong; installs system-wide. 93
    305. 305. pip http://pip.openplans.org/ 94
    306. 306. pip http://pip.openplans.org/ • “Pip Installs Packages” 94
    307. 307. pip http://pip.openplans.org/ • “Pip Installs Packages” • The good: Just Works™; handles non- PyPI packages (including direct from SCM); repeatable dependancies; integrates with virtualenv for isolation. 94
    308. 308. pip http://pip.openplans.org/ • “Pip Installs Packages” • The good: Just Works™; handles non- PyPI packages (including direct from SCM); repeatable dependancies; integrates with virtualenv for isolation. • The bad: still young; not yet bundled. 94
    309. 309. pip http://pip.openplans.org/ • “Pip Installs Packages” • The good: Just Works™; handles non- PyPI packages (including direct from SCM); repeatable dependancies; integrates with virtualenv for isolation. • The bad: still young; not yet bundled. • The ugly: haven’t found it yet. 94
    310. 310. zc.buildout http://buildout.org/ 95
    311. 311. zc.buildout http://buildout.org/ • The good: incredibly flexible; handles any sort of dependancy; repeatable builds; reusable “recipes;” good ecosystem; handles isolation, too. 95
    312. 312. zc.buildout http://buildout.org/ • The good: incredibly flexible; handles any sort of dependancy; repeatable builds; reusable “recipes;” good ecosystem; handles isolation, too. • The bad: often cryptic, INI-style configuration file; confusing duplication of recipes; sometimes too flexible. 95
    313. 313. zc.buildout http://buildout.org/ • The good: incredibly flexible; handles any sort of dependancy; repeatable builds; reusable “recipes;” good ecosystem; handles isolation, too. • The bad: often cryptic, INI-style configuration file; confusing duplication of recipes; sometimes too flexible. • The ugly: nearly completely undocumented. 95
    314. 314. Package isolation 96
    315. 315. Package isolation • Why? 96
    316. 316. Package isolation • Why? • Site A requires Foo v1.0; site B requires Foo v2.0. 96
    317. 317. Package isolation • Why? • Site A requires Foo v1.0; site B requires Foo v2.0. • You need to develop against multiple versions of dependancies. 96
    318. 318. Package isolation tools 97
    319. 319. Package isolation tools • Virtual machines (Xen, VMWare, EC2, …) 97
    320. 320. Package isolation tools • Virtual machines (Xen, VMWare, EC2, …) • Multiple Python installations. 97
    321. 321. Package isolation tools • Virtual machines (Xen, VMWare, EC2, …) • Multiple Python installations. • “Virtual” Python installations. 97
    322. 322. Package isolation tools • Virtual machines (Xen, VMWare, EC2, …) • Multiple Python installations. • “Virtual” Python installations. • virtualenv http://pypi.python.org/pypi/virtualenv 97
    323. 323. Package isolation tools • Virtual machines (Xen, VMWare, EC2, …) • Multiple Python installations. • “Virtual” Python installations. • virtualenv http://pypi.python.org/pypi/virtualenv • zc.buildout http://buildout.org/ 97
    324. 324. Why automate? 98
    325. 325. Why automate? • “I can’t push this fix to the servers until Alex gets back from lunch.” 98
    326. 326. Why automate? • “I can’t push this fix to the servers until Alex gets back from lunch.” • “Sorry, I can’t fix that. I’m new here.” 98
    327. 327. Why automate? • “I can’t push this fix to the servers until Alex gets back from lunch.” • “Sorry, I can’t fix that. I’m new here.” • “Oops, I just made the wrong version of our site live.” 98
    328. 328. Why automate? • “I can’t push this fix to the servers until Alex gets back from lunch.” • “Sorry, I can’t fix that. I’m new here.” • “Oops, I just made the wrong version of our site live.” • “It’s broken! What’d you do!?” 98
    329. 329. Automation basics 99
    330. 330. Automation basics • SSH is right out. 99
    331. 331. Automation basics • SSH is right out. • Don’t futz with the server. Write a recipe. 99
    332. 332. Automation basics • SSH is right out. • Don’t futz with the server. Write a recipe. • Deploys should be idempotent. 99
    333. 333. Capistrano http://capify.org/ 100
    334. 334. Capistrano http://capify.org/ • The good: lots of features; good documentation; active community. 100
    335. 335. Capistrano http://capify.org/ • The good: lots of features; good documentation; active community. • The bad: stale development; very “opinionated” and Rails-oriented. 100
    336. 336. Fabric http://fabfile.org/ 101
    337. 337. Fabric http://fabfile.org/ • The good: very simple; flexible; actively developed; Python. 101
    338. 338. Fabric http://fabfile.org/ • The good: very simple; flexible; actively developed; Python. • The bad: no high-level commands; in flux. 101
    339. 339. Configuration management 102
    340. 340. Configuration management • CFEngine, Puppet, Chef, … 102
    341. 341. Configuration management • CFEngine, Puppet, Chef, … • Will handle a lot more than code deployment! 102
    342. 342. Configuration management • CFEngine, Puppet, Chef, … • Will handle a lot more than code deployment! • I only know a little about these. 102
    343. 343. Recommendations Pip, Virtualenv, and Fabric Buildout and Fabric. Buildout and Puppet/Chef/…. Utility computing and Puppet/Chef/…. 103
    344. 344. Production environments 104
    345. 345. net. LiveJournal Backend: Today (Roughly.) BIG-IP perlbal (httpd/proxy) Global Database bigip1 mod_perl bigip2 proxy1 master_a master_b web1 proxy2 web2 proxy3 web3 Memcached slave1 slave2 ... slave5 djabberd proxy4 mc1 web4 djabberd proxy5 ... mc2 User DB Cluster 1 djabberd webN mc3 uc1a uc1b mc4 User DB Cluster 2 ... uc2a uc2b gearmand Mogile Storage Nodes gearmand1 mcN User DB Cluster 3 sto1 sto2 gearmandN uc3a uc3b Mogile Trackers ... sto8 tracker1 tracker3 User DB Cluster N ucNa ucNb MogileFS Database “workers” gearwrkN Job Queues (xN) mog_a mog_b theschwkN jqNa jqNb slave1 slaveN http://danga.com/words/ Brad Fitzpatrik, http://danga.com/words/2007_06_usenix/ 3 105
    346. 346. django database media server 106
    347. 347. Application servers 107
    348. 348. Application servers • Apache + mod_python 107
    349. 349. Application servers • Apache + mod_python • Apache + mod_wsgi 107
    350. 350. Application servers • Apache + mod_python • Apache + mod_wsgi • Apache/lighttpd + FastCGI 107
    351. 351. Application servers • Apache + mod_python • Apache + mod_wsgi • Apache/lighttpd + FastCGI • SCGI, AJP, nginx/mod_wsgi, ... 107
    352. 352. Use mod_wsgi 108
    353. 353. WSGIScriptAlias / /home/mysite/mysite.wsgi 109
    354. 354. import os, sys # Add to PYTHONPATH whatever you need sys.path.append('/usr/local/django') # Set DJANGO_SETTINGS_MODULE os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' # Create the application for mod_wsgi import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() 110
    355. 355. “Scale” 111
    356. 356. Does this scale? django database media server 112
    357. 357. Does this scale? django database media server Maybe! 112
    358. 358. Things per secong Number of things 113
    359. 359. Real-world example Database A 175 req/s Database B 75 req/s 114
    360. 360. Real-world example http://tweakers.net/reviews/657/6 115
    361. 361. django media web server database database server 116
    362. 362. Why separate hardware? 117
    363. 363. Why separate hardware? • Resource contention 117
    364. 364. Why separate hardware? • Resource contention • Separate performance concerns 117
    365. 365. Why separate hardware? • Resource contention • Separate performance concerns • 0 → 1 is much harder than 1 → N 117
    366. 366. DATABASE_HOST = '10.0.0.100' 118
    367. 367. DATABASE_HOST = '10.0.0.100' FAIL 118
    368. 368. Connection middleware 119
    369. 369. Connection middleware • Proxy between web and database layers 119
    370. 370. Connection middleware • Proxy between web and database layers • Most implement hot fallover and connection pooling 119
    371. 371. Connection middleware • Proxy between web and database layers • Most implement hot fallover and connection pooling • Some also provide replication, load balancing, parallel queries, connection limiting, &c 119
    372. 372. Connection middleware • Proxy between web and database layers • Most implement hot fallover and connection pooling • Some also provide replication, load balancing, parallel queries, connection limiting, &c • DATABASE_HOST = '127.0.0.1' 119
    373. 373. Connection middleware 120
    374. 374. Connection middleware • PostgreSQL: pgpool 120
    375. 375. Connection middleware • PostgreSQL: pgpool • MySQL: MySQL Proxy 120
    376. 376. Connection middleware • PostgreSQL: pgpool • MySQL: MySQL Proxy • Database-agnostic: sqlrelay 120
    377. 377. Connection middleware • PostgreSQL: pgpool • MySQL: MySQL Proxy • Database-agnostic: sqlrelay • Oracle: ? 120
    378. 378. django media web server media server database database server 121
    379. 379. Media server traits 122
    380. 380. Media server traits • Fast 122
    381. 381. Media server traits • Fast • Lightweight 122
    382. 382. Media server traits • Fast • Lightweight • Optimized for high concurrency 122
    383. 383. Media server traits • Fast • Lightweight • Optimized for high concurrency • Low memory overhead 122
    384. 384. Media server traits • Fast • Lightweight • Optimized for high concurrency • Low memory overhead • Good HTTP citizen 122
    385. 385. Media servers 123
    386. 386. Media servers • Apache? 123
    387. 387. Media servers • Apache? • lighttpd 123
    388. 388. Media servers • Apache? • lighttpd • nginx 123
    389. 389. Media servers • Apache? • lighttpd • nginx • S3 123
    390. 390. The absolute minimum django media web server media server database database server 124
    391. 391. The absolute minimum django media database web server 125
    392. 392. proxy media load balancer media server django django django web server cluster database database server 126
    393. 393. Why load balancers? 127
    394. 394. Load balancer traits 128
    395. 395. Load balancer traits • Low memory overhead 128
    396. 396. Load balancer traits • Low memory overhead • High concurrency 128
    397. 397. Load balancer traits • Low memory overhead • High concurrency • Hot fallover 128
    398. 398. Load balancer traits • Low memory overhead • High concurrency • Hot fallover • Other nifty features... 128
    399. 399. Load balancers 129
    400. 400. Load balancers • Apache + mod_proxy 129
    401. 401. Load balancers • Apache + mod_proxy • perlbal 129
    402. 402. Load balancers • Apache + mod_proxy • perlbal • nginx 129
    403. 403. Load balancers • Apache + mod_proxy • perlbal • nginx • Varnish 129
    404. 404. Load balancers • Apache + mod_proxy • perlbal • nginx • Varnish • Squid 129
    405. 405. CREATE POOL mypool POOL mypool ADD 10.0.0.100 POOL mypool ADD 10.0.0.101 CREATE SERVICE mysite SET listen = my.public.ip SET role = reverse_proxy SET pool = mypool SET verify_backend = on SET buffer_size = 120k ENABLE mysite 130
    406. 406. you@yourserver:~$ telnet localhost 60000 pool mysite add 10.0.0.102 OK nodes 10.0.0.101 10.0.0.101 lastresponse 1237987449 10.0.0.101 requests 97554563 10.0.0.101 connects 129242435 10.0.0.101 lastconnect 1237987449 10.0.0.101 attempts 129244743 10.0.0.101 responsecodes 200 358 10.0.0.101 responsecodes 302 14 10.0.0.101 responsecodes 207 99 10.0.0.101 responsecodes 301 11 10.0.0.101 responsecodes 404 18 10.0.0.101 lastattempt 1237987449 131
    407. 407. proxy proxy proxy media media load balancing cluster media server cluster django django django cache cache web server cluster cache cluster database database database database server cluster 132
    408. 408. “Shared nothing” 133
    409. 409. BALANCE = None def balance_sheet(request): global BALANCE if not BALANCE: bank = Bank.objects.get(...) BALANCE = bank.total_balance() ... 134
    410. 410. BALANCE = None def balance_sheet(request): global BALANCE if not BALANCE: bank = Bank.objects.get(...) BALANCE = bank.total_balance() ... FAIL 134
    411. 411. Global variables are right out 135
    412. 412. from django.cache import cache def balance_sheet(request): balance = cache.get('bank_balance') if not balance: bank = Bank.objects.get(...) balance = bank.total_balance() cache.set('bank_balance', balance) ... 136
    413. 413. from django.cache import cache def balance_sheet(request): balance = cache.get('bank_balance') if not balance: bank = Bank.objects.get(...) balance = bank.total_balance() cache.set('bank_balance', balance) ... WIN 136
    414. 414. def generate_report(request): report = get_the_report() open('/tmp/report.txt', 'w').write(report) return redirect(view_report) def view_report(request): report = open('/tmp/report.txt').read() return HttpResponse(report) 137
    415. 415. def generate_report(request): report = get_the_report() open('/tmp/report.txt', 'w').write(report) return redirect(view_report) def view_report(request): report = open('/tmp/report.txt').read() return HttpResponse(report) FAIL 137
    416. 416. Filesystem? What filesystem? 138
    417. 417. Further reading 139
    418. 418. Further reading • Cal Henderson, Building Scalable Web Sites 139
    419. 419. Further reading • Cal Henderson, Building Scalable Web Sites • John Allspaw, The Art of Capacity Planning 139
    420. 420. Further reading • Cal Henderson, Building Scalable Web Sites • John Allspaw, The Art of Capacity Planning • http://kitchensoap.com/ 139
    421. 421. Further reading • Cal Henderson, Building Scalable Web Sites • John Allspaw, The Art of Capacity Planning • http://kitchensoap.com/ • http://highscalability.com/ 139
    422. 422. Monitoring 140
    423. 423. Goals 141
    424. 424. Goals • When the site goes down, know it immediately. 141
    425. 425. Goals • When the site goes down, know it immediately. • Automatically handle common sources of downtime. 141

    ×