high performance php: scaling and getting the most out of your infrastructure
DESCRIPTION
ZendCon 2010 session on performance optimization tips and best practicesTRANSCRIPT
©All rights reserved. Zend Technologies, Inc.
High performance PHP: How do I make it go faster?Maurice Kherlakian
Professional Services Consultant
©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
©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
©All rights reserved. Zend Technologies, Inc.4
The network
• Reverse proxyAvoid 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
©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 ServerApache-based, on Linux, Windows, IBM i
Zend engine
©All rights reserved. Zend Technologies, Inc.6
Web servers clustering
• 1st step towards scaling solutionAfter moving the database to a separate server
Configuration syncing: rsync, rmp/deb, svn
Application code deployment
ZSCM
©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
©All rights reserved. Zend Technologies, Inc.8
Web servers session clustering
• What is session clustering
• Failing over to other servers
• Mechanisms for session clusteringMemcached
Lightweight key/value server
Database
Zend Server SC
©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 cachesAPC, eaccelerator, xcache
Zend Server Optimizer+
• Extra performance boost – check timestamp
©All rights reserved. Zend Technologies, Inc.10
strace with and without O+ activated
• 10 times less system calls with O+ active
©All rights reserved. Zend Technologies, Inc.11
Static content
• Why should we separate static/dynamic content
• Techniques for content separationCDN
Separate sub-domains
Reverse proxying with rules for static content
©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 serverLighty, nginx
• Securing large files downloadModules for web servers – authentication logic, then
offload to web server. Usually through headers
Zend Download server
©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 serverOptimizer + for code caching
Full page caching
ZSCM for server sync
Code tracing/monitoring for app monitoring
Zend Download server
©All rights reserved. Zend Technologies, Inc.14
Linux(rpm/web repositories)
IBM i(PTF)
Windows(MSI)
ApplicationPerformance
Acceleration
Optimization
Caching
Reliability& Management
Monitoring
Root-Cause
Configuration
Scale-Out
Clustering
Job Queue
Downloads
Business-gradePHP
Hot Fixes
Support
Java Bridge
Zend Framework
PHP
Zend Server
Zen
d S
tudio
(Ecl
ipse
-base
d)
Colla
bora
teD
ebug
Pro
file
Test
Zend Server14
©All rights reserved. Zend Technologies, Inc.15
Database scaling
• Scaling techniquesReplication
Functional separation
Sharding
• Schemaless databases eg: MongoDB
• For analytics, consider a column-oriented database like Infobright
©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!
©All rights reserved. Zend Technologies, Inc.17
The application
• No cookbook for good application design
• Don’t optimize in early development, risk over-optimization
• FrameworksFrameworks 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
©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)
©All rights reserved. Zend Technologies, Inc.19
The secrets to application optimization
• Using the right toolsXdebug
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
©All rights reserved. Zend Technologies, Inc.20
Database optimizations
• Statements tuning
• Tables indexing
• Monitor all queries to DBUse 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
©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
• CachingFile 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.
©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?
©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
©All rights reserved. Zend Technologies, Inc.
Thank you!Maurice Kherlakian
http://framework.zend.com
http://www.zend.com