Your SlideShare is downloading. ×

Transforming legacy PHP applications with Symfony2 and Varnish

3,538

Published on

The careerswales.com platform manages the education and professional progression of millions of people in Wales. When challenged with upgrading its ageing application stack to suit modern standards, …

The careerswales.com platform manages the education and professional progression of millions of people in Wales. When challenged with upgrading its ageing application stack to suit modern standards, we devised a solution with Symfony2 at its core. In this talk I'll discuss how the new stack was implemented, focussing on how we took advantage of Symfony2's first-class support for reverse-proxy Varnish to phase the deployment of the new system and improve performance by a significant amount. I'll also talk about the API we created to bridge its SQL Server / MySQL database platforms, and how Symfony2 was used to provide a single-sign-on solution across all of its web applications.

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

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Transforming legacy applications with Symfony2 and Varnish
  • 2. JULY 2012 10 CONCURRENT USERS ~20 SECONDS AVERAGE HOMEPAGE -> LOGIN -> PERSONAL DETAILS
  • 3. Why so slow? PHP4 SQL Server and Windows Server Sub-optimal caching strategy
  • 4. “Rewriting the code from scratch ... (is) the single worst strategic mistake a software company can make.” - Joel Spolsky
  • 5. “... the quick fix is like quicksand: The clarity of code goes down and confusion is harvested in its place.” - Venkat Subramaniam and Andy Hunt
  • 6. The Plan Reduce cost, improve performance Develop new application framework Gradual deployment to careerswales.com API to access shared data Single sign on and shared sessions
  • 7. Legacy CMS New Applications CAREERSWALES.COM
  • 8. ... ENTER SYMFONY2
  • 9. Why Symfony2? Used successfully in other projects Excellent 3rd party libraries Flexible and easy to extend Facilitates best practice
  • 10. PART 1: THE CMS
  • 11. Catalyst CMS Cutting edge stack - PHP 5.4, Nginx, FPM Reusable code through Symfony2 bundles Inspired by Symfony2 CMF and Sonata Fraction of the code to maintain
  • 12. PART 2: THE API
  • 13. Take it to the bridge Bridges MSSQL & MySQL databases Acts as a facade Linux + MSSQL = PAIN Caching with Symfony2 and Doctrine
  • 14. SQL Server API Application (Symfony2) Third parties MySQL Website Application (Symfony2)
  • 15. PART 3: SINGLE SIGN ON
  • 16. Single sign on Single authentication system for all applications Uses Central Authentication Service (CAS) protocol BeSimpleSsoAuthBundle does heavy lifting Custom modifications via DI container
  • 17. PART 4: VARNISH
  • 18. Varnish Frontend proxy or HTTP accelerator Sits between client and server Shares cached responses between clients Can perform load balancing and routing
  • 19. Without Varnish Client Client Client HTTP://WWW.CAREERSWALES.COM/EN/
  • 20. Without Varnish Client Client Client TTL = 1 DAY TTL = 1 DAY TTL = 1 DAY HTTP://WWW.CAREERSWALES.COM/EN/
  • 21. With Varnish Client Client Client Varnish HTTP://WWW.CAREERSWALES.COM/EN/
  • 22. With Varnish Client Client Client Varnish TTL = 1 DAY TTL = 1 DAY TTL = 1 DAY TTL = 1 DAY HTTP://WWW.CAREERSWALES.COM/EN/
  • 23. With Varnish Client Client Client Varnish Client Client Client Client HTTP://WWW.CAREERSWALES.COM/EN/
  • 24. With Varnish Client Client Client Varnish Client Client Client Client HTTP://WWW.CAREERSWALES.COM/EN/
  • 25. ESI Caching Edge Side Includes Identifies blocks of HTML which are dynamic Blocks are independent of the main response Blocks can be cached and expired individually Blocks can be identified as public (cacheable) or private (not cacheable)
  • 26. TTL = 5 MINS TTL = 5 DAYS
  • 27. INVALIDATED INVALIDATED INVALIDATED
  • 28. Carousel Block Caching Straightforward caching No personalisation, same for all users
  • 29. Carousel Block Caching public function indexAction() { $response = new Response(); // cache for 1 minute $response->setMaxAge(60); $response->setPublic(); return $this->render( 'BoxUK:Default:index.html.twig', array(), $response ); }
  • 30. Login Button Caching More complex: two different states Can use varying to store two different caches Use cookies to provide information to Varnish BUT don’t want to vary on cookie
  • 31. Login Button Caching public function indexAction() { $response = new Response(); // cache for 1 minute $response->setMaxAge(60); $response->setPublic(); $response->setVary('Logged-In'); return $this->render( 'BoxUK:Default:index.html.twig', array(), $response ); }
  • 32. Login Button Caching public function onKernelResponse( FilterResponseEvent $event ) { $response = $event->getResponse(); $loggedIn = $this->context->isGranted( 'IS_AUTHENTICATED_FULLY' ) ? 'true' : 'false'; $response ->headers ->set('Logged-In', $loggedIn); }
  • 33. Login Button Caching public function onKernelResponse( FilterResponseEvent $event ) { $response = $event->getResponse(); $cookie = new Cookie('Logged-In', $loggedIn, ...); $response ->headers ->setCookie($cookie); }
  • 34. Login Button Caching if (req.http.Cookie ~ 'Logged-In=true') { set req.http.Logged-In = 'true'; } Varnish cache keys are hashed on URL (host, path, etc) and vary data Symfony takes care of the way out On the way in, we have to fake the header to reproduce the hash
  • 35. Routing Varnish directs requests to given ‘backends’ Load balancing, e.g. round robin More complex logic via VCL (Varnish Configuration Language) No EC2 load balancers!
  • 36. Routing Old applications New applications 10.1.2.1 10.1.2.2 Client Varnish Examine request
  • 37. Defining backends backend legacy_applications { .host = "10.1.2.1"; .port = "80"; } backend new_applications { .host = "10.1.2.2"; .port = "80"; }
  • 38. Routing by regex sub vcl_recv { ! if (req.url ~ "^(.*)www.careerswales.com/old-request-format") { ! set req.backend = legacy_applications; ! return(pipe); ! } ! if (req.url ~ "^(.*)www.careerswales.com/new-request-format") { ! set req.backend = new_applications; ! return(pipe); ! } }
  • 39. OLD APPLICATIONS Client Varnish NEW APPLICATIONS SQL Server MySQL SSO App API APP www.careerswales.com /old-request-format www.careerswales.com /new-request-format
  • 40. JULY 2012 10 CONCURRENT USERS ~20 SECONDS AVERAGE HOMEPAGE -> LOGIN -> PERSONAL DETAILS
  • 41. JULY 2013 10 CONCURRENT USERS ~4 SECONDS AVERAGE HOMEPAGE -> LOGIN -> PERSONAL DETAILS
  • 42. Conclusions Observable speed increase of 560%No loss of functionality or downtime No loss of functionality or downtime Substantially less ‘hardware’ More complex environment
  • 43. Questions? @craigmarvelley

×