Upgrade to Pro — share decks privately, control downloads, hide ads and more …

High load projects on Symfony

High load projects on Symfony

Maksym Moskvychev

November 03, 2014

More Decks by Maksym Moskvychev

Other Decks in Programming


  1. High load projects on Symfony Maksym Moskvychev maksym.moskvychev@gmail.com

  2. About speaker

  3. Agenda Network design Framework abilities to increase performance Architecture principles

    & examples Cache abilities Tips to save memory
  4. Network design

  5. Network design of typical PHP application PHP node Session storage

    Database cluster MySQL node MySQL node MySQL node Load balancer PHP node Reverse proxy PHP node PHP node Internet Media storage
  6. Master / slave database connection in Symfony Master node Slave

    node Slave node Slave node Load balancer Read/write connection Read connection Default entity manager Read entity manager
  7. Session storage allows to share sessions between PHP nodes

  8. Media storage allows to sha e use s’ files between

    PHP nodes
  9. Reverse proxy is.. • Application firewall • Cache system •

    Load distributer • SSL encryption system
  10. AppCache in Symfony • Can be turned on in app.php

    file • Lightweight alternative of Varnish • Useful for development only
  11. Framework abilities to increase performance

  12. Use ACP cache whenever it possible • Enable APC cache

    in PHP settings. • Cache class autoloader with APC. • Turn on Doctrine query cache with ACP
  13. Architecture principles & examples

  14. Storing logic in SQL queries Logic in PHP classes Logic

    in SQL queries + easy to implement + easy to maintain + complex logic could be split into classes + could be covered with unit test + fast for batch operations VS
  15. Admin area Storing logic in SQL queries Product publication logic

    (method in repository) ->joinLeft(… Front end Rest API Query in SonataAdminBundle Query for frontend Rest controller
  16. Pre-loading data in Doctrine • By default Doctrine loads data

    from related entities at the moment getter is used. • There is a possi ility to do load all relatio s data in one query. $query->joinLeft(‘product.category’);
  17. Personalization & full page cache Full page cache adds some

    limitations, because most requests never reach real server - There is no user sessions There is JavaScript There are headers, cookies There are IFrames There are ESI tags
  18. Personalization & full page cache Some page JavaScript Google analytics

    & another tracking systems Recently seen pages. Data stored in Cookies User menu in IFrame. Cached by private cache Page itself. Cached by public cache
  19. Cache abilities

  20. Caching in web application Http cache • Full page cache

    • Gateway cache • PHP accelerator • Reverse proxy Query result cache • APC • File system • Memory Other methods
  21. Http cache Unified standard for caching, defined in HTTP protocol

    Cache communicates with application via HTTP headers and status codes Expiration and validation caching models Fully supported by Symfony HTTP Foundation component
  22. Expiration model in HTTP cache Reverse proxy PHP node Internet

    Hey, this response would be OK for next 2 hours . Re uest to /contacts page Reverse proxy Internet 2. Re uest to /contacts page within hou s
  23. Validation model in HTTP cache Reverse proxy PHP node Internet

    Hey, this response has ETag d54sd65yf6cytfrtfg7w4fgy . Re uest to /a ticle page MySQL node Reverse proxy PHP node Internet Gi e e /arti le1 FYI: I know response for ETag d54sd65yf6cytfrtfg7w4fgy 2. Re uest to /a ticle page MySQL node Yes, this is exactly d54sd65yf6cytfrtfg7w4fgy • Fetch only hash of article • Fetch all data for article • Fetch only hash of article
  24. Caching query results in Symfony 1. Choose one of existing

    caching providers: APC, memory (arrays), file system, etc.. 2. “et result query cache provider duri g uildi g a query 3. Choose and set cache lifetime during building a query.
  25. Tips to save memory

  26. Turn off SQL logger. In debug mode it stores in

    memory information about all executed queries. 1
  27. Clear entity manager from time to time $entityManager->clear() 2

  28. Flush garbage collector inside of long loops gc_collect_cycles() 3

  29. Use pagination to fetch huge amount of data Use few

    queries with limit/offset instead of one query for all data. 4
  30. Typical scheme of console command in Symfony Turn off SQL

    logger Fetch batch of data Apply some logic to each data row Clear entity manager Flush garbage collector
  31. Q & A Maksym Moskvychev maksym.moskvychev@gmail.com