23
Optimizing LAMPhp Applications RootConf, 26 th May 2012 Piyush Goel Capillary Technologies [email protected]

Optimizing LAMPhp Applications

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

Page 1: Optimizing LAMPhp Applications

Optimizing LAMPhp Applications

RootConf, 26th May 2012

Piyush GoelCapillary Technologies [email protected]

Page 2: Optimizing LAMPhp Applications

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

Page 3: Optimizing LAMPhp Applications

Web Applications are like Onions !!

26/05/2012 RootConf

Page 4: Optimizing LAMPhp Applications

Web Applications are like Onions !!

26/05/2012 RootConf

Layered Architecture Rocks !!

Page 5: Optimizing LAMPhp Applications

26/05/2012 RootConf

Page 6: Optimizing LAMPhp Applications

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

Page 7: Optimizing LAMPhp Applications

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

Page 8: Optimizing LAMPhp Applications

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

Page 9: Optimizing LAMPhp 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

Page 10: Optimizing LAMPhp Applications

26/05/2012 RootConf

Page 11: Optimizing LAMPhp Applications

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

Page 12: Optimizing LAMPhp Applications

PHP Optimization contd…

Ø Output Buffering

Ø ob_start / ob_flush

26/05/2012 RootConf

Ø Compression

Ø ZLIB module

Ø ob_gzhandler

Page 13: Optimizing LAMPhp Applications

26/05/2012 RootConf

Page 14: Optimizing LAMPhp Applications

MySQL under the hood

26/05/2012 RootConf

Page 15: Optimizing LAMPhp Applications

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

Page 16: Optimizing LAMPhp Applications

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/

Page 17: Optimizing LAMPhp Applications

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

Page 18: Optimizing LAMPhp Applications

26/05/2012 RootConf

Tools and Practices

Page 19: Optimizing LAMPhp Applications

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

Page 20: Optimizing LAMPhp Applications

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

Page 21: Optimizing LAMPhp Applications

What It shouldn’t be !!

26/05/2012 RootConf

SysAdmins vs Developers

Page 22: Optimizing LAMPhp Applications

Personal Rant/Advice

26/05/2012 RootConf

Understand what’s happening under the hood

Page 23: Optimizing LAMPhp Applications

THANKS !

26/05/2012 RootConf

Capillary is looking for awesome DevAdmins! [email protected]

[email protected]