Assembling Pages Last
   Edge Caching, ESI, and Rails


          Aaron Batalion
             5/31/2008




              ...
2
About Me
www.livingsocial.com   blog.livingsocial.com




                                               3
About Me
      www.revolution.com




                                #2




       http://rails100.pbwiki.com/

         ...
Agenda
- Examine a Rails Application
- Apply Standard Caching Techniques
- When thats not enough, then what?
- Edge Cachin...
Agenda
- Examine a Rails Application
- Apply Standard Caching Techniques
- When thats not enough, then what?
- Edge Cachin...
7
8
9
10
11
12
13
http://readers.livingsocial.com




                                  14
def show
 @feed = @person.mini_feed
 @current = @person.currently_reading
 @news = Blog.recent_by_vertical(“readingsocial”...
<html>
 <%= render   :partial   =>   "header/login" -%>
 <%= render   :partial   =>   "feed" -%>
 <%= render   :partial   ...
When thats not enough, then what?




                                    17
When thats not enough, then what?




                                    18
When thats not enough, then what?




                                    19
Agenda
- Examine a Rails Application
- Apply Standard Caching Techniques
- When thats not enough, then what?
- Edge Cachin...
class PeopleController
    caches_page :show
    ...
end




                         21
class PeopleController
    caches_page :show
    ...
end




                         22
class PeopleController
    caches_action :show
    ...
end




                          23
class PeopleController
    caches_action :show
    ...
end




                          24
<% cache(“mini-feed-#{@person.id}”) do %>
   <% @feed = @person.mini_feed %>
   <%= render :partial => "feed" -%>
<% end %...
class Person
  def mini_feed
     cache(“feed-#{id}”) { ...}
  end
end




                                  26
When thats not enough, then what?
When thats not enough....




                                        27
28
Rails isn’t fast....

 It is fast enough

         You can always get
LOTS and LOTS of servers...




                    ...
Use your application
Without hitting your application




                                   30
HTTP/1.1 200 OK
Date: Fri, 15 Dec 2007 17:32:47 GMT
Content-Length: 33286
Cache-Control: max-age=7200
Content-Type: text/h...
HTTP/1.1 200 OK
Date: Fri, 15 Dec 2007 17:32:47 GMT
Content-Length: 33286
Cache-Control: max-age=7200
Content-Type: text/h...
Agenda
- Examine a Rails Application
- Apply Standard Caching Techniques
- When thats not enough, then what?
- Edge Cachin...
ESI
  Edge Side Includes

           2001 W3C Spec
   http://www.w3.org/TR/esi-lang
By: Akamai, Oracle, BEA, Vignette...

...
simple markup language
esi:include                 esi:try
              esi:attempt              esi:except
esi:invalidat...
parsed by ESI server




                       36
Mongrel

      http://readers.livingsocial.com

                                         ESI     html + esi
              ...
4 /header   (ttl = 60.min)




                                                                        2
                 ...
Mongrel

      http://readers.livingsocial.com            html + esi
                                         ESI
        ...
1. http://readers.livingsocial.com




                                     40
1. http://readers.livingsocial.com
2. Page Template (if not cached)
                                     41
4 /header   (ttl = 60.min)




                                                             2
                            ...
4




                                        2

                   1                    3
1. http://readers.livingsocial....
“Assembles”

1.   http://readers.livingsocial.com
2.   Page Template (if not cached)
3.   Retrieve Fragments (if not cache...
1. http://readers.livingsocial.com
2. Pagehttp://readers.livingsocial.com
         Template (cached)
                     ...
4




                                           2

                     1
             “Assembles”
                      ...
4




                                          2

                     1
             “Assembles”
                       ...
So what!
Memcache can do that!



                        48
ESI
ESI: Personalized Full Page Caching
ESI: Concurrency
ESI: Slow/Broken Dependencies
ESI: Application Sharding
ESI: Poly...
ESI: Personalized Full Page Caching
caches_page
cache(“whole_page”) {...}
headers[“Cache-Control”] = “max-age:3600”




  ...
ESI: Concurrency


def show
 @feed = @person.mini_feed
 @current = @person.currently_reading
 @news = Blog.recent_by_verti...
ESI: Concurrency


def show
 @feed = @person.mini_feed # 1s
 @current = @person.currently_reading # 1s
 @news = Blog.recen...
ESI: Concurrency

def mini_feed #1s
 ...
def currently_reading #1s
  ...

def news #1s                3s -> 1s
  ...




 ...
ESI: Slow/Broken Dependencies



def show
 @feed = @person.mini_feed
 @current = @person.currently_reading
 @news = Blog.r...
ESI: Slow/Broken Dependencies



<esi:include src=”/mini_feed”
             max-age=”600”/>




                          ...
ESI: Slow/Broken Dependencies

<esi:try>
 <esi:attempt>
   <esi:include src=”/mini_feed”
        max-age=”600” timeout=”1”...
ESI: Application Sharding



        “Federate as much as you can”
          “A rails process should only
             be ...
ESI: Application Sharding



                            /mini_feed




                                         58
ESI: Polyglot Assembly



                         /mini_feed




                Merb/Erlang




                        ...
ESI: Cached New User Experience
                                    4




                                            2
  ...
ESI: Cached New User Experience
                                       4




                                             ...
ESI:Cached New User Experience
                                      4




                                              2...
ESI: Inline Invalidation



<esi:invalidate>
  ....
    <basicselector uri="/foo/bar/baz"/>
  ...
       <advancedselector...
Agenda
- Examine a Rails Application
- Apply Standard Caching Techniques
- When thats not enough, then what?
- Edge Cachin...
FragmentFu

Project: http://code.google.com/p/mongrel-esi/
google: FragmentFu
            “Proof of Concept”




         ...
FragmentFu


<%= render :esi => fragment_person_path,
           :ttl => 60.minutes %>




                               ...
FragmentFu


def update
 ....
 invalidate_and_redirect_to(person_path(@person))
end




                                  ...
FragmentFu

def latest
 ...
 respond_to |wants| do
   wants.html { ... }
   wants.js { ... }   #X-Requested-With = 'XMLHtt...
FragmentFu

def latest
 ...
 respond_to |wants| do
   wants.html { ... }
   wants.js { ... }      #X-Requested-With = 'XML...
Agenda
- Examine a Rails Application
- Apply Standard Caching Techniques
- When thats not enough, then what?
- Edge Cachin...
- Open Source
- Commerical
- Content Delivery Network




                             71
mongrel-esi
http://code.google.com/p/mongrel-esi/

 - Small, but fast
 - Open Source by Todd Fisher
 - Ragel based parser
...
Squid
http://www.squid-cache.org/

 - In 2002, Zope funded ESI
 - Version 3.0+
 - subset of ESI support




              ...
Varnish
http://varnish.projects.linpro.no/

 - Supposedly Fast
   “Squid is rather old and designed
    like computer prog...
Varnish
 http://www.funnyordie.com/

   - Supposedly Fast
“Squid is rather old and designed like computer programs
where s...
Web Cache 10g
BIG-IP WebAccelerator




                        76
Web Cache 10g
2007 InfoQ Article - RevolutionHealth.com
 http://www.infoq.com/news/2007/02/revolution-health-profile

     ...
Akamai




Most Complete ESI Implementation


                                   78
Agenda
- Examine a Rails Application
- Apply Standard Caching Techniques
- When thats not enough, then what?
- Edge Cachin...
Edge Caching - Cons



YAGNI
                      80
Edge Caching - Cons


          complexity



                       81
Edge Caching - Cons



cache
invalidation

                      82
Edge Caching - Cons

  “There are only two hard things in
 Computer Science: cache invalidation

         and naming thing...
Edge Caching - Cons

              lack of
              mature
                open
              source


              ...
Edge Caching - Cons


             cost of
        deployment

                       85
Edge Caching - Pros

concurrent execution



                       86
Edge Caching - Pros


         efficient
       execution
                      87
Edge Caching - Pros


 A/B
Testing

                       88
Edge Caching - Pros
def mini_feed
 ...
def currently_reading
  ...
                           RESTful
def news
  ...      ...
Edge Caching - Pros

Syndication
for


Free

                            90
Edge Caching - Pros

          Geographically
          Distributed
          Personalization



                         ...
92
http://blog.hungrymachine.com

flickr.com/photos/nickdawson/1484934955      flickr.com/photos/petestott/1281698980

flickr.co...
Q&A

      94
Upcoming SlideShare
Loading in...5
×

Assembling Ruby on Rails Pages Last With ESI

3,846

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,846
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Assembling Ruby on Rails Pages Last With ESI

  1. 1. Assembling Pages Last Edge Caching, ESI, and Rails Aaron Batalion 5/31/2008 1
  2. 2. 2
  3. 3. About Me www.livingsocial.com blog.livingsocial.com 3
  4. 4. About Me www.revolution.com #2 http://rails100.pbwiki.com/ 4
  5. 5. Agenda - Examine a Rails Application - Apply Standard Caching Techniques - When thats not enough, then what? - Edge Caching - FragmentFu - Deployment Options - Pros/Cons of Edge Caching 5
  6. 6. Agenda - Examine a Rails Application - Apply Standard Caching Techniques - When thats not enough, then what? - Edge Caching - FragmentFu - Deployment Options - Pros/Cons of Edge Caching 6
  7. 7. 7
  8. 8. 8
  9. 9. 9
  10. 10. 10
  11. 11. 11
  12. 12. 12
  13. 13. 13
  14. 14. http://readers.livingsocial.com 14
  15. 15. def show @feed = @person.mini_feed @current = @person.currently_reading @news = Blog.recent_by_vertical(“readingsocial”) end 15
  16. 16. <html> <%= render :partial => "header/login" -%> <%= render :partial => "feed" -%> <%= render :partial => "current" -%> <%= render :partial => "news" -%> </html> 16
  17. 17. When thats not enough, then what? 17
  18. 18. When thats not enough, then what? 18
  19. 19. When thats not enough, then what? 19
  20. 20. Agenda - Examine a Rails Application - Apply Standard Caching Techniques - When thats not enough, then what? - Edge Caching - FragmentFu - Deployment Options - Pros/Cons of Edge Caching 20
  21. 21. class PeopleController caches_page :show ... end 21
  22. 22. class PeopleController caches_page :show ... end 22
  23. 23. class PeopleController caches_action :show ... end 23
  24. 24. class PeopleController caches_action :show ... end 24
  25. 25. <% cache(“mini-feed-#{@person.id}”) do %> <% @feed = @person.mini_feed %> <%= render :partial => "feed" -%> <% end %> 25
  26. 26. class Person def mini_feed cache(“feed-#{id}”) { ...} end end 26
  27. 27. When thats not enough, then what? When thats not enough.... 27
  28. 28. 28
  29. 29. Rails isn’t fast.... It is fast enough You can always get LOTS and LOTS of servers... 29
  30. 30. Use your application Without hitting your application 30
  31. 31. HTTP/1.1 200 OK Date: Fri, 15 Dec 2007 17:32:47 GMT Content-Length: 33286 Cache-Control: max-age=7200 Content-Type: text/html 31
  32. 32. HTTP/1.1 200 OK Date: Fri, 15 Dec 2007 17:32:47 GMT Content-Length: 33286 Cache-Control: max-age=7200 Content-Type: text/html 32
  33. 33. Agenda - Examine a Rails Application - Apply Standard Caching Techniques - When thats not enough, then what? - Edge Caching - FragmentFu - Deployment Options - Pros/Cons of Edge Caching 33
  34. 34. ESI Edge Side Includes 2001 W3C Spec http://www.w3.org/TR/esi-lang By: Akamai, Oracle, BEA, Vignette... 34
  35. 35. simple markup language esi:include esi:try esi:attempt esi:except esi:invalidate HTTP_* esi:chose esi:when 35
  36. 36. parsed by ESI server 36
  37. 37. Mongrel http://readers.livingsocial.com ESI html + esi Server Mongrel Mongrel 1. http://readers.livingsocial.com 2. Page Template 37
  38. 38. 4 /header (ttl = 60.min) 2 /recent (ttl = 10.min) “Assembles” 1 /mini_feed (ttl = 30.min) 3 /news (ttl = 60.min) <html> ....... <esi:include src="/mini_feed” max-age="1800"/> ....... </html> 38
  39. 39. Mongrel http://readers.livingsocial.com html + esi ESI Server Mongrel html Mongrel 1. http://readers.livingsocial.com 2. Page Template (if not cached) 3. Retrieve Fragments (if not cached) 39
  40. 40. 1. http://readers.livingsocial.com 40
  41. 41. 1. http://readers.livingsocial.com 2. Page Template (if not cached) 41
  42. 42. 4 /header (ttl = 60.min) 2 /recent (ttl = 10.min) 1 /mini_feed (ttl = 30.min) 3 /news (ttl = 60.min) 1. http://readers.livingsocial.com 2. Page Template (if not cached) 42
  43. 43. 4 2 1 3 1. http://readers.livingsocial.com 2. Page Template (if not cached) 3. Retrieve Fragments (if not cached) 43
  44. 44. “Assembles” 1. http://readers.livingsocial.com 2. Page Template (if not cached) 3. Retrieve Fragments (if not cached) 4. Respond back to User 44
  45. 45. 1. http://readers.livingsocial.com 2. Pagehttp://readers.livingsocial.com Template (cached) 45
  46. 46. 4 2 1 “Assembles” 3 1. http://readers.livingsocial.com 2. Page Template (cached) 3. Retrieve Fragments (cached) 4. Respond back to User 46
  47. 47. 4 2 1 “Assembles” 3 1. http://readers.livingsocial.com (cached) 2. Page Template (cached) 3. Retrieve Fragments (3 is cached) 4. Respond back to User 47
  48. 48. So what! Memcache can do that! 48
  49. 49. ESI ESI: Personalized Full Page Caching ESI: Concurrency ESI: Slow/Broken Dependencies ESI: Application Sharding ESI: Polyglot Assembly ESI: Inline Invalidation ESI: Cached New User Experience 49
  50. 50. ESI: Personalized Full Page Caching caches_page cache(“whole_page”) {...} headers[“Cache-Control”] = “max-age:3600” 50
  51. 51. ESI: Concurrency def show @feed = @person.mini_feed @current = @person.currently_reading @news = Blog.recent_by_vertical(“readingsocial”) end 51
  52. 52. ESI: Concurrency def show @feed = @person.mini_feed # 1s @current = @person.currently_reading # 1s @news = Blog.recent_by_vertical(“readingsocial”) # 1s end 3s 52
  53. 53. ESI: Concurrency def mini_feed #1s ... def currently_reading #1s ... def news #1s 3s -> 1s ... 53
  54. 54. ESI: Slow/Broken Dependencies def show @feed = @person.mini_feed @current = @person.currently_reading @news = Blog.recent_by_vertical(“reading”) #10s end 54
  55. 55. ESI: Slow/Broken Dependencies <esi:include src=”/mini_feed” max-age=”600”/> 55
  56. 56. ESI: Slow/Broken Dependencies <esi:try> <esi:attempt> <esi:include src=”/mini_feed” max-age=”600” timeout=”1”/> </esi:attempt> <esi:except> <esi:include src=”/static/mini_feed.html”/> </esi:except> </esi:try> 56
  57. 57. ESI: Application Sharding “Federate as much as you can” “A rails process should only be doing one controller” 57
  58. 58. ESI: Application Sharding /mini_feed 58
  59. 59. ESI: Polyglot Assembly /mini_feed Merb/Erlang 59
  60. 60. ESI: Cached New User Experience 4 2 /recent (ttl = 10.min) 1 /mini_feed (ttl = 30.min) 3 /news (ttl = 60.min) 60
  61. 61. ESI: Cached New User Experience 4 2 /recent (ttl = 10.min) 1 /mini_feed (ttl = 30.min) 3 /news (ttl = 60.min) ”/mini_feed?uid=$HTTP_COOKIE[“uid”]” 61
  62. 62. ESI:Cached New User Experience 4 2 /recent (ttl = 1 /mini_feed (ttl = 30.min) 3 /news (ttl = 60.min) ”/mini_feed?uid=” Full Cache Hit! 62
  63. 63. ESI: Inline Invalidation <esi:invalidate> .... <basicselector uri="/foo/bar/baz"/> ... <advancedselector uriexp="/people/123/.*"/> </esi:invalidate> 63
  64. 64. Agenda - Examine a Rails Application - Apply Standard Caching Techniques - When thats not enough, then what? - Edge Caching - FragmentFu - Deployment Options - Pros/Cons of Edge Caching 64
  65. 65. FragmentFu Project: http://code.google.com/p/mongrel-esi/ google: FragmentFu “Proof of Concept” 65
  66. 66. FragmentFu <%= render :esi => fragment_person_path, :ttl => 60.minutes %> 66
  67. 67. FragmentFu def update .... invalidate_and_redirect_to(person_path(@person)) end 67
  68. 68. FragmentFu def latest ... respond_to |wants| do wants.html { ... } wants.js { ... } #X-Requested-With = 'XMLHttpRequest' end end 68
  69. 69. FragmentFu def latest ... respond_to |wants| do wants.html { ... } wants.js { ... } #X-Requested-With = 'XMLHttpRequest' wants.fragment { .... } #X-Requested-With = ʻESIRequest' end end 69
  70. 70. Agenda - Examine a Rails Application - Apply Standard Caching Techniques - When thats not enough, then what? - Edge Caching - FragmentFu - Deployment Options - Pros/Cons of Edge Caching 70
  71. 71. - Open Source - Commerical - Content Delivery Network 71
  72. 72. mongrel-esi http://code.google.com/p/mongrel-esi/ - Small, but fast - Open Source by Todd Fisher - Ragel based parser - memcache-backed caching 72
  73. 73. Squid http://www.squid-cache.org/ - In 2002, Zope funded ESI - Version 3.0+ - subset of ESI support 73
  74. 74. Varnish http://varnish.projects.linpro.no/ - Supposedly Fast “Squid is rather old and designed like computer programs where supposed to be designed in 1980.” - Varnish FAQ - basic ESI support - FunnyOrDie.com uses 74
  75. 75. Varnish http://www.funnyordie.com/ - Supposedly Fast “Squid is rather old and designed like computer programs where supposed to be designed in 1980.” http://varnish.projects.linpro.no/wiki/FAQ - subset of ESI support - FunnyOrDie.com uses 56.6M views 75
  76. 76. Web Cache 10g BIG-IP WebAccelerator 76
  77. 77. Web Cache 10g 2007 InfoQ Article - RevolutionHealth.com http://www.infoq.com/news/2007/02/revolution-health-profile “NetCraft says you've got Oracle Application Server 10g as the final public facing piece... “ 77
  78. 78. Akamai Most Complete ESI Implementation 78
  79. 79. Agenda - Examine a Rails Application - Apply Standard Caching Techniques - When thats not enough, then what? - Edge Caching - FragmentFu - Deployment Options - Pros/Cons of Edge Caching 79
  80. 80. Edge Caching - Cons YAGNI 80
  81. 81. Edge Caching - Cons complexity 81
  82. 82. Edge Caching - Cons cache invalidation 82
  83. 83. Edge Caching - Cons “There are only two hard things in Computer Science: cache invalidation and naming things” - Phil Karlton 83
  84. 84. Edge Caching - Cons lack of mature open source 84
  85. 85. Edge Caching - Cons cost of deployment 85
  86. 86. Edge Caching - Pros concurrent execution 86
  87. 87. Edge Caching - Pros efficient execution 87
  88. 88. Edge Caching - Pros A/B Testing 88
  89. 89. Edge Caching - Pros def mini_feed ... def currently_reading ... RESTful def news ... application assembly 89
  90. 90. Edge Caching - Pros Syndication for Free 90
  91. 91. Edge Caching - Pros Geographically Distributed Personalization 91
  92. 92. 92
  93. 93. http://blog.hungrymachine.com flickr.com/photos/nickdawson/1484934955 flickr.com/photos/petestott/1281698980 flickr.com/photos/kamonegi_jp/1860174314 flickr.com/photos/bunchofpants/103515576/ flickr.com/photos/mdd/175287890 flickr.com/photos/seikatsu/686399884/ flickr.com/photos/caseywilliamson/82417809 flickr.com/photos/hand-nor-glove/2311353113 flickr.com/photos/zeemanshuis/1351045987 flickr.com/photos/scobleizer/2341031948/ 93
  94. 94. Q&A 94

×