Everyone loves PHP

778 views
719 views

Published on

PHP / Mysql / Scaling

Published in: Education
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
778
On SlideShare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
Downloads
0
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Everyone loves PHP

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

×