Share
Embed
High load projects on Symfony
Maksym Moskvychev
November 03, 2014
Transcript
31 slides
-
Agenda Network design Framework abilities to increase performance Architecture principles
& examples Cache abilities Tips to save memory -
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 -
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 -
Reverse proxy is.. • Application firewall • Cache system •
Load distributer • SSL encryption system -
AppCache in Symfony • Can be turned on in app.php
file • Lightweight alternative of Varnish • Useful for development only -
Use ACP cache whenever it possible • Enable APC cache
in PHP settings. • Cache class autoloader with APC. • Turn on Doctrine query cache with ACP -
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 -
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 -
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’); -
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 -
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 -
Caching in web application Http cache • Full page cache
• Gateway cache • PHP accelerator • Reverse proxy Query result cache • APC • File system • Memory Other methods -
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 -
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 -
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 -
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. -
Turn off SQL logger. In debug mode it stores in
memory information about all executed queries. 1 -
Use pagination to fetch huge amount of data Use few
queries with limit/offset instead of one query for all data. 4 -
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