PHP Everyone loves PHP
Agenda PHP Basics Why PHP is so popular in Web 2.0 world Comparison of various platforms (php, ruby, java) Scaling out Database tier Scaling out Web tier Caching (APC/Memcached)
PHP Basics
What is PHP? Server side Scripting language Virtual Machine : Zend Engine Written in C Object Oriented (from PHP 5.0) Weak typing
Cool Stuff – PHP Array
PHP Array an ordered map  -  A map is a type that associates  values  to  keys
<?php $arr  = array( &quot;foo&quot;  =>  &quot;bar&quot; ,  12  =>  true ); echo  $arr [ &quot;foo&quot; ];  // bar echo  $arr [ 12 ];     // 1 ?>
<?php $arr  = array( &quot;foo&quot;  =>  array(1, 2) ,  12  =>  array( “ user1 ”=>array( “ name”  => ‘mark’, “ gender”  => ‘male’ ) ) ); ?>
Juggling with variables
Why PHP is so popular in Web 2.0 world 99880… Larry@… Larry 98778… Mark@… Mark phone email name
Execute Query Select * from <table> where email = ‘Mark@…’
What is returned from DB is a  PHP Array object $arr  = array( “ name&quot;  =>”Mark”,   “ email&quot;  =>”Mark@…”,   “ phone&quot;  =>”98778…”, )
The same object can be pushed to JAVA-Script layer for Processing var jsvar  = array( “ name&quot;  =>”Mark”,   “ email&quot;  =>”Mark@…”,   “ phone&quot;  =>”98778…”, )
So what we gained … No need to have an intermediate object like java beans to store the DB result PHP array can be sent directly to javascript layer for processing Java script array notation is same as php array notation … No need to write unnecessary code to handle the DB    Front end communication
 
Scaling
Terminology: Scaling NOT how fast your code is How fast your code will be tomorrow Can it scale out? Run in parallel?
Scale out or Scale up ?
Cloud Computing Amazon Cloud
Amazon Cloud Components Amazon Elastic Cloud Computing (EC2) Amazon Simple Storage Service (S3) Amazon Simple Queuing service (SQS) Utilities Distributed computing framework (HADOOP) Amazon Payment service
Growing Up, “One Box” solution   Basic web application (discussion board, etc.)   Low traffic   Apache/PHP/MySQL on one machine   Bottlenecks will start showing up:   Most likely database before apache/php   Disk I/O (Innodb) or locking wait states (MyISAM)
InnoDB vs. MyISAM
90% Read 60% Read 99880… Larry@… Larry 98778… Mark@… Mark phone email name I like it … Photo #4 Larry@… This is great  Photo #1 Mark@… comment resource email
 
Growing Up, “Two Box” solution Higher traffic application Apache/PHP on Box A, MySQL on Box B Bottlenecks will be Disk I/O on MySQL Box Locking on MyISAM tables Network I/O
 
Growing Up, “Many Boxes with Replication” solution   Yet even higher traffic   Writes are separated from reads (master gets IN/UP/DEL, slaves get SELECTs)   SELECTs, IN/UP/DEL can be specified within the application,   Or Load-balancing can be used
 
Bottlenecks … As you have multiple slaves  Your code will decide “Execute Select command in Slave #4” But how will you decide it ?
Using Hardware Load Balancer
How its usually done? Standard MySQL Master/Slave replication All writes (Insert/Delete/Update) from application go to master All reads (Selects) from application go to a load balancer which will spread out load across all slaves.
 
What is good about load balancing ? You can add/remove slaves without affecting the application. Additional monitoring point and automatic failure detection You can treat all your slave pool as one resource.
Only one Master Database … the Next Bottleneck …
Use NDB Cluster
 
Scaling out Web tier
Design Change Store your Session variable in Database Always access Session from DB  Remove the Session entry from DB on Logout Update the session entry periodically to handle session hijacking attack …
#1 #2 #3 #4 Load Balancer DB Layer
Caching (APC/Memcached)
 
 
Where should we store the value  Local database is faster than shared database Local Disk faster than database RAM is faster than disk
What we will store Anything you don’t want to fetch or compute every time your code runs Anything that’s not going to change very often
Everybody is doing it WP_Cache (Wordpress) Memcache Live journal Slashdot wikipedia
Even PHP Itself Is Getting Cached   PHP opcode caches compile your scripts and run these pre-parsed versions instead.   Zend Optimizer   APC (Alternative PHP Cache)   Xcache   eAccelerator   ionCube
 
Of course OS caches files Modern operating systems use all your free RAM as a disk cache.   If you read the same file over & over, it's probably being read out of RAM the second time on. So, it's usually ok to read a small file over & over.
Memcached memcached is a high-performance, distributed memory object caching system
 
 
Thank You
 

Everyone loves PHP

  • 1.
  • 2.
    Agenda PHP BasicsWhy PHP is so popular in Web 2.0 world Comparison of various platforms (php, ruby, java) Scaling out Database tier Scaling out Web tier Caching (APC/Memcached)
  • 3.
  • 4.
    What is PHP?Server side Scripting language Virtual Machine : Zend Engine Written in C Object Oriented (from PHP 5.0) Weak typing
  • 5.
    Cool Stuff –PHP Array
  • 6.
    PHP Array anordered map - A map is a type that associates  values  to  keys
  • 7.
    <?php $arr  = array(&quot;foo&quot;  =>  &quot;bar&quot; ,  12  =>  true ); echo  $arr [ &quot;foo&quot; ];  // bar echo  $arr [ 12 ];     // 1 ?>
  • 8.
    <?php $arr  = array(&quot;foo&quot;  =>  array(1, 2) ,  12  =>  array( “ user1 ”=>array( “ name” => ‘mark’, “ gender” => ‘male’ ) ) ); ?>
  • 9.
  • 10.
    Why PHP isso popular in Web 2.0 world 99880… Larry@… Larry 98778… Mark@… Mark phone email name
  • 11.
    Execute Query Select* from <table> where email = ‘Mark@…’
  • 12.
    What is returnedfrom DB is a PHP Array object $arr  = array( “ name&quot;  =>”Mark”, “ email&quot;  =>”Mark@…”, “ phone&quot;  =>”98778…”, )
  • 13.
    The same objectcan be pushed to JAVA-Script layer for Processing var jsvar  = array( “ name&quot;  =>”Mark”, “ email&quot;  =>”Mark@…”, “ phone&quot;  =>”98778…”, )
  • 14.
    So what wegained … No need to have an intermediate object like java beans to store the DB result PHP array can be sent directly to javascript layer for processing Java script array notation is same as php array notation … No need to write unnecessary code to handle the DB  Front end communication
  • 15.
  • 16.
  • 17.
    Terminology: Scaling NOThow fast your code is How fast your code will be tomorrow Can it scale out? Run in parallel?
  • 18.
    Scale out orScale up ?
  • 19.
  • 20.
    Amazon Cloud ComponentsAmazon Elastic Cloud Computing (EC2) Amazon Simple Storage Service (S3) Amazon Simple Queuing service (SQS) Utilities Distributed computing framework (HADOOP) Amazon Payment service
  • 21.
    Growing Up, “OneBox” solution Basic web application (discussion board, etc.) Low traffic Apache/PHP/MySQL on one machine Bottlenecks will start showing up: Most likely database before apache/php Disk I/O (Innodb) or locking wait states (MyISAM)
  • 22.
  • 23.
    90% Read 60%Read 99880… Larry@… Larry 98778… Mark@… Mark phone email name I like it … Photo #4 Larry@… This is great Photo #1 Mark@… comment resource email
  • 24.
  • 25.
    Growing Up, “TwoBox” solution Higher traffic application Apache/PHP on Box A, MySQL on Box B Bottlenecks will be Disk I/O on MySQL Box Locking on MyISAM tables Network I/O
  • 26.
  • 27.
    Growing Up, “ManyBoxes with Replication” solution Yet even higher traffic Writes are separated from reads (master gets IN/UP/DEL, slaves get SELECTs) SELECTs, IN/UP/DEL can be specified within the application, Or Load-balancing can be used
  • 28.
  • 29.
    Bottlenecks … Asyou have multiple slaves Your code will decide “Execute Select command in Slave #4” But how will you decide it ?
  • 30.
  • 31.
    How its usuallydone? Standard MySQL Master/Slave replication All writes (Insert/Delete/Update) from application go to master All reads (Selects) from application go to a load balancer which will spread out load across all slaves.
  • 32.
  • 33.
    What is goodabout load balancing ? You can add/remove slaves without affecting the application. Additional monitoring point and automatic failure detection You can treat all your slave pool as one resource.
  • 34.
    Only one MasterDatabase … the Next Bottleneck …
  • 35.
  • 36.
  • 37.
  • 38.
    Design Change Storeyour Session variable in Database Always access Session from DB Remove the Session entry from DB on Logout Update the session entry periodically to handle session hijacking attack …
  • 39.
    #1 #2 #3#4 Load Balancer DB Layer
  • 40.
  • 41.
  • 42.
  • 43.
    Where should westore the value Local database is faster than shared database Local Disk faster than database RAM is faster than disk
  • 44.
    What we willstore Anything you don’t want to fetch or compute every time your code runs Anything that’s not going to change very often
  • 45.
    Everybody is doingit WP_Cache (Wordpress) Memcache Live journal Slashdot wikipedia
  • 46.
    Even PHP ItselfIs Getting Cached PHP opcode caches compile your scripts and run these pre-parsed versions instead. Zend Optimizer APC (Alternative PHP Cache) Xcache eAccelerator ionCube
  • 47.
  • 48.
    Of course OScaches files Modern operating systems use all your free RAM as a disk cache. If you read the same file over & over, it's probably being read out of RAM the second time on. So, it's usually ok to read a small file over & over.
  • 49.
    Memcached memcached isa high-performance, distributed memory object caching system
  • 50.
  • 51.
  • 52.
  • 53.