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.
© All rights reserved. Zend Technologies, Inc.
High performance PHP: How
do I make it go faster?
Maurice Kherlakian
Profes...
© All rights reserved. Zend Technologies, Inc.2
Agenda
• What we will be covering
 The network, database, and application...
© All rights reserved. Zend Technologies, Inc.3
The network
•Entry point into your system
•Needs to be secured
Firewall
...
© All rights reserved. Zend Technologies, Inc.4
The network
•Reverse proxy
Avoid having application servers in an externa...
© All rights reserved. Zend Technologies, Inc.5
The web server
•Different alternatives – choose based on performance
Apac...
© All rights reserved. Zend Technologies, Inc.6
Web servers clustering
•1st step towards scaling solution
After moving th...
© All rights reserved. Zend Technologies, Inc.7
Deployment strategies
•SVN as a deployment tool
•RPM/deb
•PEAR
•Zend Serve...
© All rights reserved. Zend Technologies, Inc.8
Web servers session clustering
•What is session clustering
•Failing over t...
© All rights reserved. Zend Technologies, Inc.9
Opcode caching
•What is it, why virtually every PHP site uses it
•Drastic ...
© All rights reserved. Zend Technologies, Inc.10
strace with and without O+ activated
•10 times less system calls with O+ ...
© All rights reserved. Zend Technologies, Inc.11
Static content
•Why should we separate static/dynamic content
•Techniques...
© All rights reserved. Zend Technologies, Inc.12
Handling large files
•Why large files should not be served from PHP
•Serv...
© All rights reserved. Zend Technologies, Inc.13
•To achieve an optimal cluster, many different packages
•What if one solu...
© All rights reserved. Zend Technologies, Inc.14
Linux
(rpm/web repositories)
IBM i
(PTF)
Windows
(MSI)
Application
Perfor...
© All rights reserved. Zend Technologies, Inc.15
Database scaling
•Scaling techniques
Replication
Functional separation
...
© All rights reserved. Zend Technologies, Inc.16
Database results caching
•Shared memory (APC)
•Distributed caching (memca...
© All rights reserved. Zend Technologies, Inc.17
The application
•No cookbook for good application design
•Don’t optimize ...
© All rights reserved. Zend Technologies, Inc.18
The application
•Write application for what it is intended to be used
•(e...
© All rights reserved. Zend Technologies, Inc.19
The secrets to application optimization
•Using the right tools
Xdebug
Z...
© All rights reserved. Zend Technologies, Inc.20
Database optimizations
•Statements tuning
•Tables indexing
•Monitor all q...
© All rights reserved. Zend Technologies, Inc.21
How do we fix problems
•Found a bottleneck, how do we fix it?
Rewrite lo...
© All rights reserved. Zend Technologies, Inc.22
High server loads
•App is optimized, traffic growing, servers start to co...
© All rights reserved. Zend Technologies, Inc.23
Recap
•Be smart!
•Look at the problem, understand it, and solve it.
•Usin...
© All rights reserved. Zend Technologies, Inc.
Thank you!
Maurice Kherlakian
maurice.k@zend.com
http://framework.zend.com
...
Upcoming SlideShare
Loading in …5
×

High performance PHP: Scaling and getting the most out of your infrastructure

6,506 views

Published on

ZendCon 2010 session on performance optimization tips and best practices

Published in: Technology
  • Be the first to comment

High performance PHP: Scaling and getting the most out of your infrastructure

  1. 1. © All rights reserved. Zend Technologies, Inc. High performance PHP: How do I make it go faster? Maurice Kherlakian Professional Services Consultant maurice.k@zend.com
  2. 2. © All rights reserved. Zend Technologies, Inc.2 Agenda • What we will be covering  The network, database, and application aspects in optimization  Tools and techniques that assist in optimization (Code tracing, profiling)  OS level optimizations and tools  Best practices in application development for an optimized application  Server clustering  DEMO
  3. 3. © All rights reserved. Zend Technologies, Inc.3 The network •Entry point into your system •Needs to be secured Firewall Layer separation: External – DMZ - internal •Unless you have one server, need to find a traffic distribution mechanism – Load balancers Different kinds of load balancers: hardware v/s software Hardware appliances are costly but robust, do layer 7 lb Software alternatives are cheaper, do a good job also • HAProxy • Ldirectord • Nginx, Apache with balancer modules
  4. 4. © All rights reserved. Zend Technologies, Inc.4 The network •Reverse proxy Avoid having application servers in an externally accessible environment Can also be used to load balance Apache, nginx, lighttpd Usually reverse proxy connect to backend servers
  5. 5. © All rights reserved. Zend Technologies, Inc.5 The web server •Different alternatives – choose based on performance Apache – mod_php Nginx, lighty with php-fcgi or php-fpm Windows IIS – FastCGI •Zend Server Apache-based, on Linux, Windows, IBM i Zend engine
  6. 6. © All rights reserved. Zend Technologies, Inc.6 Web servers clustering •1st step towards scaling solution After moving the database to a separate server Configuration syncing: rsync, rmp/deb, svn Application code deployment ZSCM
  7. 7. © All rights reserved. Zend Technologies, Inc.7 Deployment strategies •SVN as a deployment tool •RPM/deb •PEAR •Zend Server .zpk •More info: watch deployment strategies webinar (http://www.zend.com/en/resources/webinars/php) •Check out Shahar’s Best practices in deploying PHP apps
  8. 8. © All rights reserved. Zend Technologies, Inc.8 Web servers session clustering •What is session clustering •Failing over to other servers •Mechanisms for session clustering Memcached Lightweight key/value server Database Zend Server SC
  9. 9. © All rights reserved. Zend Technologies, Inc.9 Opcode caching •What is it, why virtually every PHP site uses it •Drastic effects of code tracing (Screenshot next slide) •Different opcode caches APC, eaccelerator, xcache Zend Server Optimizer+ •Extra performance boost – check timestamp
  10. 10. © All rights reserved. Zend Technologies, Inc.10 strace with and without O+ activated •10 times less system calls with O+ active
  11. 11. © All rights reserved. Zend Technologies, Inc.11 Static content •Why should we separate static/dynamic content •Techniques for content separation CDN Separate sub-domains Reverse proxying with rules for static content
  12. 12. © All rights reserved. Zend Technologies, Inc.12 Handling large files •Why large files should not be served from PHP •Serve large files from a lightweight web server Lighty, nginx •Securing large files download Modules for web servers – authentication logic, then offload to web server. Usually through headers Zend Download server
  13. 13. © All rights reserved. Zend Technologies, Inc.13 •To achieve an optimal cluster, many different packages •What if one solution existed that could do all this? •Zend server Optimizer + for code caching Full page caching ZSCM for server sync Code tracing/monitoring for app monitoring Zend Download server
  14. 14. © All rights reserved. Zend Technologies, Inc.14 Linux (rpm/web repositories) IBM i (PTF) Windows (MSI) Application Performance Acceleration Optimization Caching Reliability & Management Monitoring Root-Cause Configuration Scale-Out Clustering Job Queue Downloads Business-grade PHP Hot Fixes Support Java Bridge Zend Framework PHP Zend Server ZendStudio(Eclipse-based) CollaborateDebugProfileTest Zend Server1
  15. 15. © All rights reserved. Zend Technologies, Inc.15 Database scaling •Scaling techniques Replication Functional separation Sharding •Schemaless databases eg: MongoDB •For analytics, consider a column-oriented database like Infobright
  16. 16. © All rights reserved. Zend Technologies, Inc.16 Database results caching •Shared memory (APC) •Distributed caching (memcached) •Zend Server data cache •But all of that doesn’t matter if your application is not optimized!
  17. 17. © All rights reserved. Zend Technologies, Inc.17 The application •No cookbook for good application design •Don’t optimize in early development, risk over-optimization •Frameworks Frameworks can make you fall in over-architecting trap Possible mistakes using DBAL or ORM – like lazy joins End up making a lot more queries than necessary
  18. 18. © All rights reserved. Zend Technologies, Inc.18 The application •Write application for what it is intended to be used •(ex: file processing: load entire file v/s one line at a time: first approach works well for a few queries, but under high load 2nd approach better)
  19. 19. © All rights reserved. Zend Technologies, Inc.19 The secrets to application optimization •Using the right tools Xdebug Zend Server Code tracing (demo) Profiling (demo) •Monitor execution time and memory usage •Design intelligently •Know the hardware you’re writing for and how it operates. The more you know about it, the better your app will be
  20. 20. © All rights reserved. Zend Technologies, Inc.20 Database optimizations •Statements tuning •Tables indexing •Monitor all queries to DB Use profiling tools, if using DBAL usually provides one ZF provides Zend_Db_Profiler that allows logging of queries to later study them (demo) Zend_Db_Profiler_Firebug: cool tool that allows displaying all queries on Firebug. Completely independent from page itself, no interference with JSON or XML requests •Use Explain, tune criteria, avoid lazy joins
  21. 21. © All rights reserved. Zend Technologies, Inc.21 How do we fix problems •Found a bottleneck, how do we fix it? Rewrite logic to be less demanding, or more demanding, but get to endpoint faster •Caching File caching Shared mem caching Distributed (memcached) •Adding caching should be easy on a well architected app Domain model •Caching in most cases is NOT a silver bullet.
  22. 22. © All rights reserved. Zend Technologies, Inc.22 High server loads •App is optimized, traffic growing, servers start to cough… •Where is the problem? •Your application can be cpu-bound, io-bound or memory bound. So how do you find out? Use tools like top, vmstat, iostat •Why is this important?
  23. 23. © All rights reserved. Zend Technologies, Inc.23 Recap •Be smart! •Look at the problem, understand it, and solve it. •Using var_dump is NOT the solution, neither is shooting in the dark •Use the right toolset •Wealth of resources (white papers/webinars…) on zend.com
  24. 24. © All rights reserved. Zend Technologies, Inc. Thank you! Maurice Kherlakian maurice.k@zend.com http://framework.zend.com http://www.zend.com

×