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

Optimizing LAMPhp Applications

Cc91b2930b6a4bce3e16c4ead40d9679?s=47 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

Cc91b2930b6a4bce3e16c4ead40d9679?s=128

pigol

June 02, 2012
Tweet

Transcript

  1. Optimizing LAMPhp Applications RootConf, 26th May 2012 Piyush Goel Capillary

    Technologies piyush.goel@capillary.co.in
  2. 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
  3. Web Applications are like Onions !! 26/05/2012 RootConf

  4. Web Applications are like Onions !! 26/05/2012 RootConf Layered Architecture

    Rocks !!
  5. 26/05/2012 RootConf

  6. 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
  7. 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
  8. 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
  9. 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
  10. 26/05/2012 RootConf

  11. 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
  12. PHP Optimization contd… Output Buffering ob_start / ob_flush 26/05/2012 RootConf

    Compression ZLIB module ob_gzhandler
  13. 26/05/2012 RootConf

  14. MySQL under the hood 26/05/2012 RootConf

  15. 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
  16. 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/
  17. 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
  18. 26/05/2012 RootConf Tools and Practices

  19. 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
  20. 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
  21. What It shouldn’t be !! 26/05/2012 RootConf SysAdmins vs Developers

  22. Personal Rant/Advice 26/05/2012 RootConf Understand what’s happening under the hood

  23. THANKS ! 26/05/2012 RootConf Capillary is looking for awesome DevAdmins!

    piyush.goel@capillary.co.in career-tech@capillary.co.in