You’ll often hear that some web framework “doesn’t scale”. Well, it’s true! Django, like all web frameworks. offers a ton of shortcuts for common cases, but as sites grow these shortcuts come at the cost of performance. However, you don’t have to throw the baby out with the bathwater; come to this talk to hear how you can win back performance while hanging on to the framework you love.
We’ll cover five “rules of thumb” for scaling Django apps, talk about the ramifications of those rules on our tool choice, and look at some real-world examples of using these rules and tools in practice.
1. You can’t tune what you don’t measure.
All talk of performance is irrelevant without hard data. If your site is “slow,” you can’t do anything about it until you can quantify what’s wrong. So what should we measure, and how should we store and visualize those measurements?
2. Be lazy.
Remember: laziness is a virtue! The fastest code is, of course, code that never runs, so you should always strive to avoid doing extra work. In web applications, this means caching. Cache early, and cache often, and many performance problems just go away.
So you can’t avoid that work… but can you maybe do it later? Most web sites don’t really have to be real-time; eventual consistency usually works just fine. Most web sites benefit greatly from using a background task queue, and luckily Django’s got a fantastic one in Celery.
4. Watch that query count!
As a rule of thumb, the limiting factor on a view’s performance is its query count—do too many queries, even cheap ones, and you’ll never get that thing tuned. We’ll talk about why this is, how to measure your query count, and how to bring that number down.
5. Don’t be afraid of your database.
Django’s ORM is super-convenient, but like all ORMs it breaks down around the edges. In some cases, sticking to the ORM balloons that all-important query count, so don’t be afraid to drop down to hand-coded SQL where performance really matters. We’ll look at when to do so, and a few tricks to keep raw SQL integrated with the rest of your app.
Jacob Kaplan-Moss is one of the lead developers and co-creator of Django. Jacob’s an experienced software developer with a focus on web application architecture. He’s a consultant with Revolution Systems, where he helps companies deploy and scale websites using Python and Django. Jacob previously worked for the Lawrence Journal-World, a locally-owned newspaper in Lawrence, Kansas, where he helped develop and eventually open source Django.
Comments on this page are now closed.
For information on exhibition and sponsorship opportunities at the conference, contact Sharon Cordesse at (707) 827-7065 or firstname.lastname@example.org.
View a complete list of OSCON contacts