Upload
piyush-goel
View
953
Download
1
Tags:
Embed Size (px)
DESCRIPTION
Slides from my talk at RootConf 2012, Bangalore (http://rootconf.in/bangalore2012). The talk covers some general tips and practices to be followed when building web applications for scale on the LAMPhp stack.
Citation preview
Optimizing LAMPhp Applications
RootConf, 26th May 2012
Piyush GoelCapillary Technologies [email protected]
Optimization
Ø “Premature Optimization is root of all Evil”
- Donald Knuth
Ø Direction and Goal Oriented
26/05/2012 RootConf
Ø Takes Effort and Time
Ø No Silver Bullets, only guidelines and practices
Web Applications are like Onions !!
26/05/2012 RootConf
Web Applications are like Onions !!
26/05/2012 RootConf
Layered Architecture Rocks !!
26/05/2012 RootConf
Apache
Ø Apache 1.3
Ø Pre-forked Model
Ø 1 process per request
Ø Robust: One crashed process dosen’t affect the others
Ø Not as fast as some other possible designs - Really ??
26/05/2012 RootConf
Ø Apache 2.0
Ø Multi-Processing Modules
Ømpm_prefork
Ømpm_worker
Tuning Apache
• MaxClients
• StartServers
• Avoid thundering effect
• MaxRequestsPerChild
• Profiling first
• AllowOverride
26/05/2012 RootConf
• AllowOverride
• HostnameLookups
• TCP Buffering
• Compression : mod_deflate/mod_gzip
• May cause spike in CPU
Tuning Apache Contd
• KeepAlive
• N objects per page
• t1 seconds for TCP connection
• t2 seconds per object
• K seconds timeout
• Non-KeepAlive : N * (t1 + t2)
26/05/2012 RootConf
• Non-KeepAlive : N * (t1 + t2)
• KeepAlive : N * (t2) + t1 + K• For KeepAlive to perform better
K < t1 * ( N -1 )
Ref: http://www.slideshare.net/techdude/building-scalable-php-applications
Tuning Apache Contd…
Ø Offload Static Files
Ø Apache + PHP not really good at static files
Ø Use Nginx or Lighttpd ( or anything else )
26/05/2012 RootConf
26/05/2012 RootConf
PHP
Ø Opcode CachingØ eAccelerator
Ø APC
Ø Zend_Cache
Ø Minimize Includes
Ø 1 include: 1 stat and realpath system call
26/05/2012 RootConf
Ø 1 include: 1 stat and realpath system call
Ø Relative paths : too many stat system calls
Ø Minimal include_path
ØReduce logging : prevents disk writes
Ø error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR
PHP Optimization contd…
Ø Output Buffering
Ø ob_start / ob_flush
26/05/2012 RootConf
Ø Compression
Ø ZLIB module
Ø ob_gzhandler
26/05/2012 RootConf
MySQL under the hood
26/05/2012 RootConf
MySQL Tuning
Ø Hardware Config:Ø Typical workloads are Memory/IO Bound
Ø Size of working data set
Ø Storage Engine:
Ø MyISAM vs InnoDB
Ø InnoDB works well in most cases, unless you need full text search
26/05/2012 RootConf
Ø InnoDB works well in most cases, unless you need full text search
Ø Cache static data:Ø Memcached
Ø Swap Space:
Ø Actually made us
MySQL Tuning Contd…Ø key_buffer_size
Ø innodb_buffer_pool_size
Ø innodb_log_file_size
Ø innodb_log_buffer_size
Ø innodb_flush_log_at_trx_commit
Ø thread_cache
Ø query_cache_size
26/05/2012 RootConf
Ø query_cache_size
Refer:
1) http://dev.mysql.com
2) http://www.mysqlperformanceblog.com/2006/09/29/what-to-tune-in-mysql-server-after-installation/
MySQL Tuning contd…
Ø Slow Query Logs:
1) Most important
2) Regular Monitoring
3) log_queries_not_using_indexes
Ø Analyze Tables periodically
26/05/2012 RootConf
Ø Optimize Table
Ø Abrupt query behaviors
Ø Avoid big deletes
Ø Use soft deletes
Ø Explain & Percona’s query visualizer
26/05/2012 RootConf
Tools and Practices
Tools and Practices
Profiling:
Ø Profile regularly
Ø XDebug / Advanced PHP Debugger ( APD )
Ø KCachegrind / Webgrind
Ø Instrument Code with Timers
Load Testing / Benchmarking
Apache Bench ( ab )
26/05/2012 RootConf
Ø Apache Bench ( ab )
Ø httperf
Ø JMeter ( personal favorite )
Strong Monitoring and Alerting
Ø Store exhaustively, Alert Judiciously
Ø Ganglia, Cacti
Ø Nagios etc
Tools & Practices
1) vmstat : Extremely useful for finding bottlenecks
2) iostat : Disk activity, useful for db systems
3) pmap : Process memory dump
4) strace: Analyzing system calls
5) sar : System activity reporter, good for postanalysis
26/05/2012 RootConf
5) sar : System activity reporter, good for postanalysis
6) netstat : Complete network dump
7) htop : Prefer over top
8) cut / tr : Analyzing Apache Access Logs
What It shouldn’t be !!
26/05/2012 RootConf
SysAdmins vs Developers
Personal Rant/Advice
26/05/2012 RootConf
Understand what’s happening under the hood