Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Nice performance using SF2 cache wrapping sf1 application Marc Weistroff  Emmanuel Cohen Sf Live  SF 2011 Tuesday, Februar...
Who we are <ul><li>Emmanuel Cohen </li></ul><ul><li>Project manager at Sensio Labs </li></ul><ul><li>4 year experience wit...
Who we are <ul><li>Marc Weistroff </li></ul><ul><li>Developer at Sensio Labs since 11/2009 </li></ul><ul><li>Lead develope...
This talk <ul><li>Symfony2 </li></ul><ul><li>symfony 1 </li></ul><ul><li>HTTP Cache </li></ul><ul><li>Edge Side Includes (...
Our client <ul><li>L’Express Magazine (Express-Roularta Group) </li></ul><ul><li>Magazine created in 1953 </li></ul><ul><l...
 
 
 
The project <ul><li>A cultural knowledge base </li></ul><ul><li>A bridge between hot news and cultural knowledge </li></ul...
 
Technical objectives <ul><li>Performance </li></ul><ul><li>A full scale Proof-of-Concept for the future </li></ul><ul><li>...
Constraints <ul><li>Heterogeneous XML Sources </li></ul><ul><li>Solution based on sf1 </li></ul><ul><li>Adaptability (for ...
How does this app work ?
The application : System design HTML Renderer XML Server OCARI Culture OCARI Content <ul><li>Stores </li></ul><ul><li>Norm...
So <ul><li>Highly specialized applications </li></ul><ul><li>Loose coupling </li></ul><ul><li>Front dedicated to deliverin...
Addressing performance
Addressing performance <ul><li>With Symfony2  built-in Gateway  : </li></ul><ul><li>HTTP cache </li></ul><ul><li>ESI </li>...
HTTP Cache <ul><li>HTTP  RFC http://tools.ietf.org/wg/httpbis/ </li></ul><ul><li>Symfony2 uses HTTP cache headers to handl...
HTTP Cache <ul><li>symfony 1  </li></ul><ul><li>Application cache </li></ul><ul><li>Symfony2  </li></ul><ul><li>light-weig...
Cache Issues <ul><li>Find out what’s stale </li></ul><ul><li>Selective expiration (or validation) </li></ul>
Performance > Cache > ESI <ul><li>A markup language </li></ul><ul><li>W3C note from Akamai </li></ul><ul><li>http:// www.w...
ESI : A page in lexpress.fr
ESI : Edge Side Includes <ul><li>Each fragment has its own ttl or validation rule => the fragments are selectively refresh...
How does ESI work?
What is an ESI? <ul><li>Represented by an HTML Tag </li></ul><ul><ul><li><esi:include src=“/movie/dogma/critics” /> </li><...
/movie/dogma
/movie/dogma
Html Fragment <ul><li>Pure HTML </li></ul><ul><li>Inserted in lieu of the <esi /> tag </li></ul><ul><li>No <html>, <body> ...
What you need <ul><li>Any client! </li></ul><ul><li>A proxy that handles ESI (ie: Varnish or Symfony2) </li></ul><ul><li>A...
First request ever Client Proxy Application /movie/dogma /movie/dogma miss Cache-Control: s-maxage=600 /movie/dogma/castin...
Request at t+200 Client Proxy Application /movie/dogma hit
The application is never hit! As if the complete page was cached in the reverse proxy
Request at t+500 Client Proxy Application /movie/dogma /movie/dogma/critics Cache-Control: s-maxage=300 hit
The application is partially hit And has to build only a small fragment of the page
Pros <ul><li>You use HTTP cache, not the application one. </li></ul><ul><li>Performance improvements! </li></ul><ul><li>Gr...
Cons <ul><li>Your app will be hit n times upon the very first request </li></ul><ul><li>Your app have to be designed to su...
But our app is symfony 1 right ?
<ul><li>What to do with your symfony 1 application when you are dying to use Symfony2?  </li></ul>Wrap it. Our app is symf...
Wrapping symfony 1 with Symfony2
Constraint <ul><li>We use the Symfony2 ESI/Cache proxy </li></ul><ul><li>All the call to our app is done in the same PHP p...
symfony 1 needs tweaking in order to work around a few obstacles
Which ones ? <ul><li>It is  not reentrant </li></ul><ul><ul><li>sfConfig singleton </li></ul></ul><ul><ul><li>sfContext si...
What about Symfony2? <ul><li>Better architecture: Symfony2 is reentrant </li></ul><ul><li>Symfony2 is heavily based on int...
HttpKernelHttpKernelInterface <ul><li>Lightweight (1 method) </li></ul><ul><li>Forces to implement a “handle” method that ...
Addressing the issues <ul><li>Reentrance </li></ul><ul><ul><li>Override the super globals </li></ul></ul><ul><ul><li>Creat...
Final app architecture EsiCacheKernel SymfonyWrapperKernel symfony 1 application
https://github.com/marcw/sflive-2011
Then… You have to design your app to serve HTML fragments!
Designing your app <ul><li>Don’t think partials or components, think actions! </li></ul><ul><li>Actions render HTML fragme...
And now where happy
What to do next ?
Proxy ESI New response Or 304 URL-> {ETag} ETag-> lastmodified If Etag absent or stale Response with HTTP headers Etag Las...
Contact @ L’Express <ul><li>Sébastien Angèle </li></ul><ul><li>[email_address] </li></ul><ul><li>Jérôme Macias </li></ul><...
Questions ? <ul><li>Marc Weistroff </li></ul><ul><li>@futurecat </li></ul><ul><li>[email_address] </li></ul><ul><li>http:/...
Thank you! Please rate this talk at http://joind.in/2596
Upcoming SlideShare
Loading in …5
×

Nice performance using Sf2 cache wrapping Sf1 application

5,151 views

Published on

In collaboration with Emmanuel Cohen.

At a key moment for online press in France, a major French news company chooses PHP and Symfony to extend its popular web site. We will present the architecture we designed at Sensio Labs to meet a very good performance requirement. We used Symfony2 kernel wrapping symfony 1.4 and relied on loose-coupled applications serving content from heterogeneous backend sources.

Published in: Technology
  • Be the first to comment

Nice performance using Sf2 cache wrapping Sf1 application

  1. 1. Nice performance using SF2 cache wrapping sf1 application Marc Weistroff Emmanuel Cohen Sf Live SF 2011 Tuesday, February 8 th 2011
  2. 2. Who we are <ul><li>Emmanuel Cohen </li></ul><ul><li>Project manager at Sensio Labs </li></ul><ul><li>4 year experience with PHP5 </li></ul><ul><li>5+ year experience with Java EE </li></ul><ul><li>Learned Basic on Amstrad CPC 6128 </li></ul>
  3. 3. Who we are <ul><li>Marc Weistroff </li></ul><ul><li>Developer at Sensio Labs since 11/2009 </li></ul><ul><li>Lead developer on this project </li></ul><ul><li>Experiences with C, and PHP from version 3 </li></ul><ul><li>Encountered programming with Amos Basic on Amiga 500 </li></ul>
  4. 4. This talk <ul><li>Symfony2 </li></ul><ul><li>symfony 1 </li></ul><ul><li>HTTP Cache </li></ul><ul><li>Edge Side Includes (ESI) </li></ul><ul><li>Web architecture </li></ul><ul><li>Online media </li></ul>
  5. 5. Our client <ul><li>L’Express Magazine (Express-Roularta Group) </li></ul><ul><li>Magazine created in 1953 </li></ul><ul><li>www.lexpress.fr </li></ul><ul><li>Top 3 French online news </li></ul>
  6. 9. The project <ul><li>A cultural knowledge base </li></ul><ul><li>A bridge between hot news and cultural knowledge </li></ul><ul><li>An extension of www.lexpress.fr </li></ul>
  7. 11. Technical objectives <ul><li>Performance </li></ul><ul><li>A full scale Proof-of-Concept for the future </li></ul><ul><li>Keep it Simple </li></ul>
  8. 12. Constraints <ul><li>Heterogeneous XML Sources </li></ul><ul><li>Solution based on sf1 </li></ul><ul><li>Adaptability (for future extensions) </li></ul>
  9. 13. How does this app work ?
  10. 14. The application : System design HTML Renderer XML Server OCARI Culture OCARI Content <ul><li>Stores </li></ul><ul><li>Normalizes </li></ul><ul><li>Aggregates </li></ul><ul><li>Serves XML </li></ul>Sources deliver heterogeneous data <ul><li>Renders HTML with XSL </li></ul><ul><li>Renders static layouts </li></ul>
  11. 15. So <ul><li>Highly specialized applications </li></ul><ul><li>Loose coupling </li></ul><ul><li>Front dedicated to delivering content fast </li></ul>
  12. 16. Addressing performance
  13. 17. Addressing performance <ul><li>With Symfony2 built-in Gateway : </li></ul><ul><li>HTTP cache </li></ul><ul><li>ESI </li></ul>
  14. 18. HTTP Cache <ul><li>HTTP RFC http://tools.ietf.org/wg/httpbis/ </li></ul><ul><li>Symfony2 uses HTTP cache headers to handle cache (Cache-Control : s-maxage, age, Last-Modified, …) </li></ul><ul><li>http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-12 </li></ul>
  15. 19. HTTP Cache <ul><li>symfony 1 </li></ul><ul><li>Application cache </li></ul><ul><li>Symfony2 </li></ul><ul><li>light-weight HttpKernel supports cache </li></ul><ul><li>uses a standard </li></ul>
  16. 20. Cache Issues <ul><li>Find out what’s stale </li></ul><ul><li>Selective expiration (or validation) </li></ul>
  17. 21. Performance > Cache > ESI <ul><li>A markup language </li></ul><ul><li>W3C note from Akamai </li></ul><ul><li>http:// www.w3.org/TR/esi-lang </li></ul><ul><li>http:// www.akamai.com/html/support/esi.html </li></ul>
  18. 22. ESI : A page in lexpress.fr
  19. 23. ESI : Edge Side Includes <ul><li>Each fragment has its own ttl or validation rule => the fragments are selectively refreshed </li></ul><ul><li>Dependance is defined once and for all </li></ul>
  20. 24. How does ESI work?
  21. 25. What is an ESI? <ul><li>Represented by an HTML Tag </li></ul><ul><ul><li><esi:include src=“/movie/dogma/critics” /> </li></ul></ul><ul><li>Processed by a proxy </li></ul><ul><li>Transformed into HTTP Request to the application </li></ul><ul><li>Response is inserted in place of the esi tag </li></ul><ul><li>HTTP cache is handled independently for each esi tag </li></ul>
  22. 26. /movie/dogma
  23. 27. /movie/dogma
  24. 28. Html Fragment <ul><li>Pure HTML </li></ul><ul><li>Inserted in lieu of the <esi /> tag </li></ul><ul><li>No <html>, <body> or <head> tag in this case </li></ul>
  25. 29. What you need <ul><li>Any client! </li></ul><ul><li>A proxy that handles ESI (ie: Varnish or Symfony2) </li></ul><ul><li>An application that delivers HTML and HTTP cache headers </li></ul><ul><li>Serve HTML pages that contain ESI tags </li></ul>
  26. 30. First request ever Client Proxy Application /movie/dogma /movie/dogma miss Cache-Control: s-maxage=600 /movie/dogma/casting /movie/dogma/critics Cache-Control: s-maxage=3600 Cache-Control: s-maxage=300
  27. 31. Request at t+200 Client Proxy Application /movie/dogma hit
  28. 32. The application is never hit! As if the complete page was cached in the reverse proxy
  29. 33. Request at t+500 Client Proxy Application /movie/dogma /movie/dogma/critics Cache-Control: s-maxage=300 hit
  30. 34. The application is partially hit And has to build only a small fragment of the page
  31. 35. Pros <ul><li>You use HTTP cache, not the application one. </li></ul><ul><li>Performance improvements! </li></ul><ul><li>Granularity: Different cache strategy or TTL on different parts of your page </li></ul>
  32. 36. Cons <ul><li>Your app will be hit n times upon the very first request </li></ul><ul><li>Your app have to be designed to support the rendering of fragments </li></ul>
  33. 37. But our app is symfony 1 right ?
  34. 38. <ul><li>What to do with your symfony 1 application when you are dying to use Symfony2? </li></ul>Wrap it. Our app is symfony 1
  35. 39. Wrapping symfony 1 with Symfony2
  36. 40. Constraint <ul><li>We use the Symfony2 ESI/Cache proxy </li></ul><ul><li>All the call to our app is done in the same PHP process </li></ul>
  37. 41. symfony 1 needs tweaking in order to work around a few obstacles
  38. 42. Which ones ? <ul><li>It is not reentrant </li></ul><ul><ul><li>sfConfig singleton </li></ul></ul><ul><ul><li>sfContext singleton </li></ul></ul><ul><li>It sends the response directly to the client </li></ul><ul><ul><li>Filter chain execution </li></ul></ul><ul><ul><li>At the end, sfRenderingFilter sends the complete response </li></ul></ul>
  39. 43. What about Symfony2? <ul><li>Better architecture: Symfony2 is reentrant </li></ul><ul><li>Symfony2 is heavily based on interfaces </li></ul><ul><li>Symfony2 is divided into several components </li></ul><ul><li>HttpKernelHttpKernelInterface </li></ul>
  40. 44. HttpKernelHttpKernelInterface <ul><li>Lightweight (1 method) </li></ul><ul><li>Forces to implement a “handle” method that accepts a Symfony2 Request object and returns a Symfony2 Response object </li></ul><ul><li>Used by all the classes that act as a Kernel in Symfony2 (ie: HttpKernelHttpCache) </li></ul>
  41. 45. Addressing the issues <ul><li>Reentrance </li></ul><ul><ul><li>Override the super globals </li></ul></ul><ul><ul><li>Create a fresh new context each time </li></ul></ul><ul><li>Filter chain </li></ul><ul><ul><li>Replace sfRenderingFilter by a noRenderingFilter class </li></ul></ul><ul><li>Create a Symfony2 Response object and returns it </li></ul>
  42. 46. Final app architecture EsiCacheKernel SymfonyWrapperKernel symfony 1 application
  43. 47. https://github.com/marcw/sflive-2011
  44. 48. Then… You have to design your app to serve HTML fragments!
  45. 49. Designing your app <ul><li>Don’t think partials or components, think actions! </li></ul><ul><li>Actions render HTML fragments </li></ul><ul><li>Actions MUST define explicitly the response cache headers. </li></ul>
  46. 50. And now where happy
  47. 51. What to do next ?
  48. 52. Proxy ESI New response Or 304 URL-> {ETag} ETag-> lastmodified If Etag absent or stale Response with HTTP headers Etag LastModified Client Request with HTTP headers If-None-Match If-Modified-Since App New response Or 304 If cache entry is not fresh enough Cache Validation Optimizer
  49. 53. Contact @ L’Express <ul><li>Sébastien Angèle </li></ul><ul><li>[email_address] </li></ul><ul><li>Jérôme Macias </li></ul><ul><li>[email_address] </li></ul>
  50. 54. Questions ? <ul><li>Marc Weistroff </li></ul><ul><li>@futurecat </li></ul><ul><li>[email_address] </li></ul><ul><li>http://www.marcw.net </li></ul><ul><li>Emmanuel Cohen </li></ul><ul><li>@emmanuelcohen </li></ul><ul><li>[email_address] </li></ul>
  51. 55. Thank you! Please rate this talk at http://joind.in/2596

×