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.

Transforming legacy PHP applications with Symfony2 and Varnish

4,897 views

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, 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
  • Dating direct: ❶❶❶ http://bit.ly/369VOVb ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Follow the link, new dating source: ❤❤❤ http://bit.ly/369VOVb ❤❤❤
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Transforming legacy PHP applications with Symfony2 and Varnish

  1. 1. Transforming legacy applications with Symfony2 and Varnish
  2. 2. JULY 2012 10 CONCURRENT USERS ~20 SECONDS AVERAGE HOMEPAGE -> LOGIN -> PERSONAL DETAILS
  3. 3. Why so slow? PHP4 SQL Server and Windows Server Sub-optimal caching strategy
  4. 4. “Rewriting the code from scratch ... (is) the single worst strategic mistake a software company can make.” - Joel Spolsky
  5. 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. 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. 7. Legacy CMS New Applications CAREERSWALES.COM
  8. 8. ... ENTER SYMFONY2
  9. 9. Why Symfony2? Used successfully in other projects Excellent 3rd party libraries Flexible and easy to extend Facilitates best practice
  10. 10. PART 1: THE CMS
  11. 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. 12. PART 2: THE API
  13. 13. Take it to the bridge Bridges MSSQL & MySQL databases Acts as a facade Linux + MSSQL = PAIN Caching with Symfony2 and Doctrine
  14. 14. SQL Server API Application (Symfony2) Third parties MySQL Website Application (Symfony2)
  15. 15. PART 3: SINGLE SIGN ON
  16. 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. 17. PART 4: VARNISH
  18. 18. Varnish Frontend proxy or HTTP accelerator Sits between client and server Shares cached responses between clients Can perform load balancing and routing
  19. 19. Without Varnish Client Client Client HTTP://WWW.CAREERSWALES.COM/EN/
  20. 20. Without Varnish Client Client Client TTL = 1 DAY TTL = 1 DAY TTL = 1 DAY HTTP://WWW.CAREERSWALES.COM/EN/
  21. 21. With Varnish Client Client Client Varnish HTTP://WWW.CAREERSWALES.COM/EN/
  22. 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. 23. With Varnish Client Client Client Varnish Client Client Client Client HTTP://WWW.CAREERSWALES.COM/EN/
  24. 24. With Varnish Client Client Client Varnish Client Client Client Client HTTP://WWW.CAREERSWALES.COM/EN/
  25. 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. 26. TTL = 5 MINS TTL = 5 DAYS
  27. 27. INVALIDATED INVALIDATED INVALIDATED
  28. 28. Carousel Block Caching Straightforward caching No personalisation, same for all users
  29. 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. 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. 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. 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. 33. Login Button Caching public function onKernelResponse( FilterResponseEvent $event ) { $response = $event->getResponse(); $cookie = new Cookie('Logged-In', $loggedIn, ...); $response ->headers ->setCookie($cookie); }
  34. 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. 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. 36. Routing Old applications New applications 10.1.2.1 10.1.2.2 Client Varnish Examine request
  37. 37. Defining backends backend legacy_applications { .host = "10.1.2.1"; .port = "80"; } backend new_applications { .host = "10.1.2.2"; .port = "80"; }
  38. 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. 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. 40. JULY 2012 10 CONCURRENT USERS ~20 SECONDS AVERAGE HOMEPAGE -> LOGIN -> PERSONAL DETAILS
  41. 41. JULY 2013 10 CONCURRENT USERS ~4 SECONDS AVERAGE HOMEPAGE -> LOGIN -> PERSONAL DETAILS
  42. 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. 43. Questions? @craigmarvelley

×