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
Tweet

More Decks by Maksym Moskvychev

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. Reverse proxy is.. • Application firewall • Cache system •

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

    file • Lightweight alternative of Varnish • Useful for development only
  5. Use ACP cache whenever it possible • Enable APC cache

    in PHP settings. • Cache class autoloader with APC. • Turn on Doctrine query cache with ACP
  6. 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
  7. 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
  8. 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’);
  9. 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
  10. 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
  11. Caching in web application Http cache • Full page cache

    • Gateway cache • PHP accelerator • Reverse proxy Query result cache • APC • File system • Memory Other methods
  12. 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
  13. 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
  14. 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
  15. 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.
  16. Turn off SQL logger. In debug mode it stores in

    memory information about all executed queries. 1
  17. Use pagination to fetch huge amount of data Use few

    queries with limit/offset instead of one query for all data. 4
  18. 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