Even Faster Django
Mongodb + Jinja2 + Bigpipe

Gage Tseng / GenieCapital
gagedark@gmail.com
About

Gage
2008

Yahoo! F2E Intern

2009

Master of CS NTU

2010

Genie Capital

2011

Gulu.com

2012

PYCON

Start my Py...
Web Framework

Easy Quick
Performance ?
Hmm.... it is

OK
How to make it

Fast ?
What is

Fast ?
Fast is about
User Experience
and Response Time
http://fast.example.com

1 2

GO
http://crazy.fast.example.com

0.1

GO
It is almost

Impossible
We are talking about
large scale and
dynamic web site
Definition of Fast
Page start to render in 1 second
Show important content in 2 seconds
Request and Response
HTTP Request
DNS Lookup

Data Transfer

DB
Render and Execute JS

Parse HTML
Download CSS, JS

Web
Se...
50%
Database &
Template Engine
Database
Good Query Performance
High Scalability
Easy to Use
NoSQL Database
Schema Free
BSON
Javascript
High Scalability
Django-Nonrel

Django Mongodb Engine
Migration-Free
No more South
$ python manage.py migrate
Embedded Model
Less Query and lookup
class UserProfile(models.Model):
contact = EmbeddedModelField('ContactModel')
...
> p...
SetField / ListFiled / DictField
Easy to use & Query Fast
class UserProfile(models.Model):
skill = ListField(models.CharFi...
Template Engine
High Render Speed
Easy to Use
Jinja2
Fast
Extensible
Django template like
Jinja2
Coffin
https://github.com/coffin/coffin
With Jinja2 You can
<h1>{{ user1.get_nickname() }}</h1>
<h1>{{ user1.fun().attr }}</h1>

No more Complicated template tags
Benchmark
It is faster now
http://faster.example.com

0.9

GO
Can it be

Even Faster
What is Even Faster
Page start to render in 0.5 second
Show important content in 1 second
BigPipe !
Facebook 2009
http://bigpipe.example.com

0.2 0.9

GO
http://bigpipe.example.com

0.2 0.9

GO
Bigpipe
Break web pages into multiple chunks (Pagelet)
Server render a pagelet and response
Prepare CSS
Render Page (HTML ...
Request and Response
HTTP Request
DNS Lookup

Data Transfer

DB
Render and Execute JS

Parse HTML
Download CSS, JS

Web
Se...
HTTP Request
DNS Lookup

Data Transfer

DB
Render and Execute JS

Web
Server

DB Query

DB Query
Parse HTML
Download CSS, ...
http://bigpipe.example.com

0.2 0.9

GO
How fast is it (from facebook)
Facebook

BigPipe
Streaming HttpResponse

https://code.djangoproject.com/ticket/7581

BigPipe Javascript

https://github.c...
Django Part
def home(request):
def stream_generator():
base = render_to_string("base.html")
yield base.ljust(4096)
yield m...
<!DOCTYPE html>
<html>

yield base.ljust(4096)

<head>
<title>I'm BigPipe</title>

<script type="text/javascript" src="big...
http://bigpipe.example.com

0.2 0.9

GO
Even Faster Django
MongoDB + Jinja2 + Bigpipe
+

Genie Dev Team
Even faster django
Upcoming SlideShare
Loading in …5
×

Even faster django

2,784 views

Published on

Even Faster Django shared at PyCon Taiwan 2012.

Published in: Technology, Design
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,784
On SlideShare
0
From Embeds
0
Number of Embeds
26
Actions
Shares
0
Downloads
19
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Even faster django

  1. 1. Even Faster Django Mongodb + Jinja2 + Bigpipe Gage Tseng / GenieCapital gagedark@gmail.com
  2. 2. About Gage 2008 Yahoo! F2E Intern 2009 Master of CS NTU 2010 Genie Capital 2011 Gulu.com 2012 PYCON Start my Python life TechCrunch Hackathon 北京
  3. 3. Web Framework Easy Quick
  4. 4. Performance ? Hmm.... it is OK
  5. 5. How to make it Fast ?
  6. 6. What is Fast ?
  7. 7. Fast is about User Experience and Response Time
  8. 8. http://fast.example.com 1 2 GO
  9. 9. http://crazy.fast.example.com 0.1 GO
  10. 10. It is almost Impossible
  11. 11. We are talking about large scale and dynamic web site
  12. 12. Definition of Fast Page start to render in 1 second Show important content in 2 seconds
  13. 13. Request and Response HTTP Request DNS Lookup Data Transfer DB Render and Execute JS Parse HTML Download CSS, JS Web Server Data Transfer Response DB Query Render Response
  14. 14. 50% Database & Template Engine
  15. 15. Database Good Query Performance High Scalability Easy to Use
  16. 16. NoSQL Database Schema Free BSON Javascript High Scalability
  17. 17. Django-Nonrel Django Mongodb Engine
  18. 18. Migration-Free No more South $ python manage.py migrate
  19. 19. Embedded Model Less Query and lookup class UserProfile(models.Model): contact = EmbeddedModelField('ContactModel') ... > print my_profile.contact.phone > u"0919xxxxxx"
  20. 20. SetField / ListFiled / DictField Easy to use & Query Fast class UserProfile(models.Model): skill = ListField(models.CharField(max_length=24)) ... > print my_profile.skill > [u"python", u"javascript", u"erlang"] > UserProfile.objects.filter(skill="python") > [<UserProfile Gage>, <UserProfile Jason>]
  21. 21. Template Engine High Render Speed Easy to Use
  22. 22. Jinja2 Fast Extensible Django template like
  23. 23. Jinja2 Coffin https://github.com/coffin/coffin
  24. 24. With Jinja2 You can <h1>{{ user1.get_nickname() }}</h1> <h1>{{ user1.fun().attr }}</h1> No more Complicated template tags
  25. 25. Benchmark
  26. 26. It is faster now
  27. 27. http://faster.example.com 0.9 GO
  28. 28. Can it be Even Faster
  29. 29. What is Even Faster Page start to render in 0.5 second Show important content in 1 second
  30. 30. BigPipe ! Facebook 2009
  31. 31. http://bigpipe.example.com 0.2 0.9 GO
  32. 32. http://bigpipe.example.com 0.2 0.9 GO
  33. 33. Bigpipe Break web pages into multiple chunks (Pagelet) Server render a pagelet and response Prepare CSS Render Page (HTML DOM) Execute JS when JS resources are ready
  34. 34. Request and Response HTTP Request DNS Lookup Data Transfer DB Render and Execute JS Parse HTML Download CSS, JS Web Server Data Transfer Response DB Query Render Response
  35. 35. HTTP Request DNS Lookup Data Transfer DB Render and Execute JS Web Server DB Query DB Query Parse HTML Download CSS, JS DB Query Data Transfer Render Response
  36. 36. http://bigpipe.example.com 0.2 0.9 GO
  37. 37. How fast is it (from facebook)
  38. 38. Facebook BigPipe Streaming HttpResponse https://code.djangoproject.com/ticket/7581 BigPipe Javascript https://github.com/msroot/bigpipe/blob/master/bigpipe.js
  39. 39. Django Part def home(request): def stream_generator(): base = render_to_string("base.html") yield base.ljust(4096) yield main_content(request).ljust(4096) #pagelet yield footer(request).ljust(4096) #pagelet yield "</body></html>" return HttpResponse(stream_generator(), mimetype='text/html', stream_content=True) main_content and footer will prepare CSS and JS resource for pagelet.
  40. 40. <!DOCTYPE html> <html> yield base.ljust(4096) <head> <title>I'm BigPipe</title> <script type="text/javascript" src="bigpipe.js"></script> </head> <body> <script> yield main_content(request).ljust(4096) bigPipe.onPageletArrive({"html":"...", "css":{...},"js":{...}}) </script> <script> yield footer(request).ljust(4096) bigPipe.onPageletArrive({"html":"...", "css":{...},"js":{...}}) </script> </body> </html> yield "</body></html>"
  41. 41. http://bigpipe.example.com 0.2 0.9 GO
  42. 42. Even Faster Django MongoDB + Jinja2 + Bigpipe + Genie Dev Team

×