sf sf v5
DESCRIPTION
PErforTRANSCRIPT
![Page 1: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/1.jpg)
Nice performance using SF2 cache wrapping sf1
application
Marc Weistroff \ Emmanuel CohenSf Live \ SF 2011
Thursday, March 3 2011
![Page 2: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/2.jpg)
Who we are
Emmanuel Cohen Project manager at Sensio Labs 4 year experience with PHP5 5+ year experience with Java EE Learned Basic on Amstrad CPC 6128
![Page 3: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/3.jpg)
Who we are
Marc Weistroff Developer at Sensio Labs since 11/2009 Lead developer on this project Experiences with C, and PHP from version 3 Encountered programming with Amos Basic on
Amiga 500
![Page 4: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/4.jpg)
This talk
Symfony2 symfony 1 HTTP Cache Edge Side Includes (ESI) Web architecture Online media
![Page 5: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/5.jpg)
Our client
L’Express Magazine Express-Roularta Group Magazine created in 1953
http://www.lexpress.fr Top 3 French online news Need for speed!
![Page 6: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/6.jpg)
![Page 7: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/7.jpg)
![Page 8: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/8.jpg)
![Page 9: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/9.jpg)
The project
A cultural knowledge base
A bridge between hot news and cultural knowledge
An extension of www.lexpress.fr
![Page 10: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/10.jpg)
![Page 11: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/11.jpg)
![Page 12: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/12.jpg)
Technical objectives
Performance
A full scale Proof-of-Concept for the future
Keep it Simple
![Page 13: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/13.jpg)
Constraints
Heterogeneous XML Sources
Solution based on symfony 1
Adaptability (for future extensions)
![Page 14: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/14.jpg)
How does this app work ?
![Page 15: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/15.jpg)
Our symfony 1 application design
HTML Renderer
XML Server
OCARI CultureOCARI Content
• Stores• Normalizes• Aggregates• Serves XML
Sources deliverheterogeneousdata
•Renders HTML with XSL•Renders static layouts
![Page 16: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/16.jpg)
Our symfony 1 application design
Highly specialized applications
Loose coupling
Front dedicated to delivering content fast
![Page 17: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/17.jpg)
Addressing performance with Symfony2
![Page 18: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/18.jpg)
Addressing performance with Sf2
With Symfony2 built-in Gateway :
HTTP cache ESI : Edge Side Includes
![Page 19: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/19.jpg)
Performance with Symfony2 : HTTP Cache
HTTP RFC
http://tools.ietf.org/wg/httpbis/
http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-12
![Page 20: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/20.jpg)
Performance with Symfony2 : HTTP Cache
Symfony2 uses HTTP cache headers to handle cache
s-maxage or max-age Etag, Last-Modified, If-Modified-Since
http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-12
![Page 21: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/21.jpg)
Performance with Symfony2 : HTTP Cache
symfony 1 Applicative cache
Symfony2 Light-weight
HttpKernel The application is not
hit Supports a standard
![Page 22: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/22.jpg)
Find out what’s stale The problem of invalidation
Performance with Symfony2: Cache issues
![Page 23: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/23.jpg)
Selective caching Do not regenerate the whole page when you just
need to regenerate small parts of it
Performance with Symfony2: Cache issues
![Page 24: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/24.jpg)
Performance with Symfony2: ESI
Edge-Side include: A markup language
W3C note from Akamai
http:// www.w3.org/TR/esi-langhttp:// www.akamai.com/html/support/esi.html
![Page 25: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/25.jpg)
ESI : A page in lexpress.fr
![Page 26: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/26.jpg)
Performance with Symfony2: ESI
Each fragment has its own ttl or validation rule => the fragments are selectively refreshed
![Page 27: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/27.jpg)
Performance with Symfony2: ESI
Dependance is defined once and for all
In expiration: simply set your ttl for each fragment
![Page 28: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/28.jpg)
How does ESI work?
![Page 29: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/29.jpg)
Performance with Symfony2: ESI
Represented by an HTML Tag• <esi:include src=“/movie/dogma/critics” />
Processed by a proxy Transformed into HTTP Request to the application Response is inserted in place of the esi tag HTTP cache is handled independently for each esi
tag
![Page 30: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/30.jpg)
/movie/dogma
![Page 31: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/31.jpg)
/movie/dogma
![Page 32: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/32.jpg)
Html Fragment
Pure HTML Inserted in lieu of the
<esi /> tag No <html>, <body>
or <head> tag in this case
![Page 33: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/33.jpg)
What you need
Any client! A proxy that handles ESI (ie: Varnish or Symfony2) An application that delivers HTML and HTTP cache
headers Serve HTML pages that contain ESI tags
![Page 34: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/34.jpg)
First request ever
ClientClient ProxyProxy ApplicationApplication
/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
![Page 35: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/35.jpg)
Request at t+200
ClientClient ProxyProxy ApplicationApplication
/movie/dogma
hit
![Page 36: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/36.jpg)
The application is never hit!
As if the complete page was cached in the reverse proxy
![Page 37: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/37.jpg)
Request at t+500
ClientClient ProxyProxy ApplicationApplication
/movie/dogma
/movie/dogma/critics
Cache-Control: s-maxage=300
hit
![Page 38: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/38.jpg)
The application is partially hit
And has to build only a small fragment of the page
![Page 39: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/39.jpg)
Pros
You use HTTP cache, not the application one. Performance improvements! Granularity: Different cache strategy or TTL on
different parts of your page
![Page 40: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/40.jpg)
Cons
Your app will be hit n times upon the very first request
Your app have to be designed to support the rendering of fragments
![Page 41: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/41.jpg)
But our app is symfony 1 right ?
![Page 42: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/42.jpg)
What to do with your symfony 1 application when you are dying to use Symfony2?
Wrap it.
Our app is symfony 1
![Page 43: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/43.jpg)
Our symfony 1 application design
HTML Renderer
XML Server
OCARI CultureOCARI Content
• Stores• Normalizes• Aggregates• Serves XML
Sources deliverheterogeneousdata
•Renders HTML with XSL•Renders static layouts
Symfony1 wrapper
Edge side Symfony2
![Page 44: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/44.jpg)
Wrapping symfony 1 with Symfony2
![Page 45: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/45.jpg)
Constraint
We use the Symfony2 ESI/Cache proxy All the call to our app is done in the same PHP
process
![Page 46: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/46.jpg)
symfony 1 needs tweaking in order to work around a few obstacles
![Page 47: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/47.jpg)
Which ones ?
It is not reentrant• sfConfig singleton• sfContext singleton
It sends the response directly to the client• Filter chain execution• At the end, sfRenderingFilter sends the complete response
![Page 48: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/48.jpg)
What about Symfony2?
Better architecture: Symfony2 is reentrant Symfony2 is heavily based on interfaces Symfony2 is divided into several components HttpKernel\HttpKernelInterface
![Page 49: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/49.jpg)
HttpKernel\HttpKernelInterface
Lightweight (1 method) Forces to implement a “handle” method that
accepts a Symfony2 Request object and returns a Symfony2 Response object
Used by all the classes that act as a Kernel in Symfony2 (ie: HttpKernel\HttpCache)
![Page 50: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/50.jpg)
Addressing the issues
Reentrance• Override the super globals• Create a fresh new context each time
Filter chain• Replace sfRenderingFilter by a noRenderingFilter class
Create a Symfony2 Response object and returns it
![Page 51: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/51.jpg)
Final app architecture
EsiCacheKernelEsiCacheKernel
SymfonyWrapperKernelSymfonyWrapperKernel
symfony 1applicationsymfony 1application
![Page 52: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/52.jpg)
https://github.com/marcw/sflive-2011
![Page 53: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/53.jpg)
Then…
You have to design your app to serve HTML fragments!
![Page 54: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/54.jpg)
Designing your app
Don’t think partials or components, think actions! Actions render HTML fragments Actions MUST define explicitly the response cache
headers.
![Page 55: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/55.jpg)
And now where happy
![Page 56: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/56.jpg)
Or are we ?
![Page 57: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/57.jpg)
Our choices
The choice of symfony 1 The choice of Symfony2 The choice to mix Expiration over validation
![Page 58: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/58.jpg)
This is symfony1 : DO NOT TRY ANY OF THIS AT HOME!
![Page 59: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/59.jpg)
Our results
It works! The frontend is scalable and extensible Performance is nice, but we needed to improve the
Store.php class
![Page 60: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/60.jpg)
What to do next ?
![Page 61: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/61.jpg)
Implementing cache validation
The application needs to garantee freshness How to avoid hitting the application ?
![Page 62: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/62.jpg)
Proxy ESI
New responseOr304
URL-> {ETag}ETag-> lastmodified
If Etag absentor stale
Response with HTTP headersEtagLastModified
Client
Request withHTTP headersIf-None-MatchIf-Modified-Since
App
New responseOr304
If cache entry is not fresh enough
Cache Validati
on Optimiz
er
![Page 64: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/64.jpg)
Questions ?
Marc Weistroff @futurecat [email protected] http://www.marcw.net
Emmanuel Cohen @emmanuelcohen [email protected]
![Page 65: Sf sf v5](https://reader034.vdocuments.net/reader034/viewer/2022050720/547a9927b4af9fb3658b4847/html5/thumbnails/65.jpg)
Thank you!
Please rate this talk athttp://joind.in/2749