django production
DESCRIPTION
This was a presentation given at San Diego Python's Django Day: http://www.meetup.com/pythonsd/events/95751792/ https://github.com/pythonsd/learning-djangoTRANSCRIPT
![Page 1: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/1.jpg)
So, You Want to Make a Production Django App
![Page 2: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/2.jpg)
A Little Background• Launching Production Django Apps since '06
and 0.9something beta
• Launched and Managed Django Websites, E-Commerce Sites, and Web Apps for brands such as:
• Rip Curl, UT San Diego, Callaway Golf, DC Shoes, Dunlop, PacSun, and many many more
![Page 3: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/3.jpg)
Goals• Identify Common Pitfalls in Launching
• Point Out Django Gotchas
• Point Out Django Tools
• Help You Have a Smooth Launch
• Give Some Pointers on Managing and Maintaing a Django App
![Page 4: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/4.jpg)
This Only Partially About Djangoand all about launching web apps
![Page 5: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/5.jpg)
Going Production is...• Putting it in front of consumers
• Promoting It
• Customer data
• 24/7/365
• Someone Else Is Using It
• Your billing relationship changes
![Page 6: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/6.jpg)
But First, Some Quick Tips
![Page 7: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/7.jpg)
Simple Django Things
• DEBUG = False
• Style your 500 / 404.html
• HTTPS Admin, login, and .. anywhere sensitive information is submitted
• Make your Sites record accurate
![Page 8: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/8.jpg)
Simple Non-Django Things
• Favicon
• Run a Site Crawler
• Redirect to one domain - www
• Check your browser console for errors
![Page 9: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/9.jpg)
So What Makes Your App Ready?
![Page 10: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/10.jpg)
The Big Questions
• Is it Stable?
• Did I get it Right?
• Does it Perform?
![Page 11: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/11.jpg)
Stability
• Stability is about predictable performance
• Thorough testing
• A way to track bugs and issues
• A way to monitor what's happening
![Page 12: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/12.jpg)
Bug Tracking
• 500 Emails - Send these to a Gmail Account
• Sentry is cool
• Rotate your logs
• Stomp 500s, signal to noise is very important
![Page 13: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/13.jpg)
Refactoring
• You probably didn't get all the data models and functionality right
• Following launch focus on fixing issues, not solving any misconceptions.
![Page 14: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/14.jpg)
Ok, App Works Good, Until the Newsletter Went
Out
![Page 15: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/15.jpg)
Performance part 1• Memcached + ANONYMOUS_CACHE
• Django Debug Toolbar
• {% cache %}
• But, don't cache session specific stuff!
• Use Production Scale Data
![Page 16: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/16.jpg)
Performance part 2• We use Nginx + Gunicorn
• CDNs - use them, but use caution
• Expire Tags - speed without effort
• Premature Optimization is the root of all evil, but the night before launch is no longer premature, so save it in a temporary model on a signal damnit
![Page 17: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/17.jpg)
Performance 2.1• Our Setup
• Static Media from CDN
• Nginx or ELB or other load balancer
• Gunicorn managed by Supervisord
• MySQL or Postgresql
• Memcached, Elasticsearch, RabbitMQ
![Page 18: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/18.jpg)
Performance part 3• New Relic can tell you a lot
• Process long tasks with Celery
• Varnish can do wonders, if your app is doing the right thing
• But first optimize your queries and indexes
• Yes, you can do group bys and aggregates.. annotate and aggregate are not so intuitive
• DB Templates Are Expensive
![Page 19: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/19.jpg)
Aggregate Digression
RateClass.objects.annotate(
hours = Sum("person__timeentry__hours")).filter(
person__timeentry__project__name="My Project')
)
![Page 20: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/20.jpg)
But Enough About Performance
• Performance is usually something you see as you scale up and add complexity
• Your site should perform quickly, but even more importantly..
![Page 21: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/21.jpg)
Things That Really Matter
![Page 22: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/22.jpg)
Content!
• Is all the content the right content?
• No Lorem Ipsum
• Do all your forms submit? Are there human error messages.
• Is the contact information correct?
![Page 23: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/23.jpg)
Browser Compatibility
• Did you check all your browsers?
• As a rule, CEOs use the oldest possible IE version
• There are over 100 million iPads, what does your site look like on one? CEOs usually have one of these too.
![Page 24: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/24.jpg)
Security
• SSL logins! admins! everything!
• CSRF protection
• Are you assuming anything?
![Page 25: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/25.jpg)
Search Matters• robots.txt
• canonical tags
• meta tags
• analytics tags
• duplicate content
![Page 26: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/26.jpg)
What Used to Be Searched Also Matters
• Pages with rankings are valuable
• If it was a link on Google, don't make it a 404
![Page 27: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/27.jpg)
Things You Should Have Already Done
![Page 28: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/28.jpg)
Training
• Not everyone groks abstractions, explain how to do things
• Make sure your client knows that access to Users == Super User
• Take a moment to polish your Change List and Change Form Templates
![Page 29: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/29.jpg)
Dev Site• Have one
• Make sure it doesn't send email to anyone
• Make sure it says "DEV SITE" somewhere on the front end
• Make sure it has robots.txt disallowing all
• If possible, control access
• Keep track of any integration credentials for sandbox vs production
![Page 30: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/30.jpg)
Configuration Management
• Settings Can Get Unruly
• We use cascading imports to structure them
• Beware Stored Passwords!
![Page 31: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/31.jpg)
Testing• Unit Tests are nice, if you have been doing
them all along
• Test it by hand
• Test it like a consumer
• - Bad / Missing Data
• - International Data / Unicode
![Page 32: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/32.jpg)
Migrations
• Use South, or don't but be prepared to tear your hair out either way
• Is there existing data to import? Do a dry run - build this into management commands
![Page 33: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/33.jpg)
Ok We Are Ready!
![Page 34: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/34.jpg)
Launch is in 1 week• Lower your DNS TTL
• Make sure you have access to all the domain information
• Are all integrations ready for production? With production credentials?
• A Dry Run is worth the effort
• Do you know all the DNS records you are changing? Are the nameservers in your control?
• Test your site with /etc/hosts, make sure it is ready to go
• Write up a plan, and let everyone know the timing
![Page 35: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/35.jpg)
Launch!• Sometimes, it is better to just get it done.
• But not always
• But sometimes, really, it just has to fly
• Walk through everything
• Run transactions on your credit card, and then cancel them
• Doubt everything
![Page 36: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/36.jpg)
Deployment
• Revision your deploys with Git
• Automate it as much as possible
• But don't put off automating most of it
• Read the 11-Factor App
![Page 37: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/37.jpg)
DNS Gotchas
• Nameservers! Don't change unless you know all the records that are there
• SPF, PTR - make sure your email makes it through the spam filter
• IT departments don't get CNAMEs, so watch your load balancers!
![Page 38: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/38.jpg)
Woo Hoo! It's Up! Now What
Are We Done?
![Page 39: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/39.jpg)
Post Launch
• Keep track of what you agreed to build.
• Good fences make good neighbors
• When You Launch, You are in Maintenance Mode. Make the Mental Shift!
![Page 40: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/40.jpg)
Refactors, Again• Actually that did need to be a separate
model
• See, South was a good idea
• Ouch you should have used South
• Either way, you need to write a manage command
![Page 41: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/41.jpg)
Maintenance Tips and Tricks
• Manage commands are really useful
• So is the shell
• Know what you plan to do on a server before logging in
![Page 42: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/42.jpg)
The Long View
• Don't focus on the bad things, focus on what was achieved
• What simple things can you do to make the next one smoother
• Checklists!
![Page 43: Django production](https://reader033.vdocuments.site/reader033/viewer/2022052315/554f6d9fb4c905c8088b5366/html5/thumbnails/43.jpg)
Questions?