Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Optimizing LAMPhp Applications

pigol
June 02, 2012

Optimizing LAMPhp Applications

Slides from my talk at RootConf 2012, Bangalore. The talk covered some general tips and practices to be followed when building web applications for scale using the LAMPhp stack

pigol

June 02, 2012
Tweet

More Decks by pigol

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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/
  9. 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
  10. 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
  11. 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