Slide 1

Slide 1 text

High load projects on Symfony Maksym Moskvychev [email protected]

Slide 2

Slide 2 text

About speaker

Slide 3

Slide 3 text

Agenda Network design Framework abilities to increase performance Architecture principles & examples Cache abilities Tips to save memory

Slide 4

Slide 4 text

Network design

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Session storage allows to share sessions between PHP nodes

Slide 8

Slide 8 text

Media storage allows to sha e use s’ files between PHP nodes

Slide 9

Slide 9 text

Reverse proxy is.. • Application firewall • Cache system • Load distributer • SSL encryption system

Slide 10

Slide 10 text

AppCache in Symfony • Can be turned on in app.php file • Lightweight alternative of Varnish • Useful for development only

Slide 11

Slide 11 text

Framework abilities to increase performance

Slide 12

Slide 12 text

Use ACP cache whenever it possible • Enable APC cache in PHP settings. • Cache class autoloader with APC. • Turn on Doctrine query cache with ACP

Slide 13

Slide 13 text

Architecture principles & examples

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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’);

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Cache abilities

Slide 20

Slide 20 text

Caching in web application Http cache • Full page cache • Gateway cache • PHP accelerator • Reverse proxy Query result cache • APC • File system • Memory Other methods

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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.

Slide 25

Slide 25 text

Tips to save memory

Slide 26

Slide 26 text

Turn off SQL logger. In debug mode it stores in memory information about all executed queries. 1

Slide 27

Slide 27 text

Clear entity manager from time to time $entityManager->clear() 2

Slide 28

Slide 28 text

Flush garbage collector inside of long loops gc_collect_cycles() 3

Slide 29

Slide 29 text

Use pagination to fetch huge amount of data Use few queries with limit/offset instead of one query for all data. 4

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Q & A Maksym Moskvychev [email protected]