OSCON - Scaling PHP in the real world!

OSCON - Scaling PHP in the real world!

Scaling PHP in the real world! - Presented at OSCON 2014.

6d9b2c900628962b16a5cb5e4e73990e?s=128

Dustin Whittle

July 23, 2014
Tweet

Transcript

  1. Scaling PHP in the real world Dustin Whittle - @dustinwhittle

  2. PHP IS USED BY THE LIKES OF FACEBOOK, YAHOO, ZYNGA,

    TUMBLR, ETSY, AND WIKIPEDIA. HOW DO THE LARGEST INTERNET COMPANIES SCALE PHP TO MEET THEIR DEMAND? ! JOIN THIS SESSION AND FIND OUT HOW TO USE THE LATEST TOOLS IN PHP FOR DEVELOPING HIGH PERFORMANCE APPLICATIONS. WE’LL TAKE A LOOK AT COMMON TECHNIQUES FOR SCALING PHP APPLICATIONS AND BEST PRACTICES FOR PROFILING AND OPTIMIZING PERFORMANCE. AFTER THIS SESSION, YOU’LL LEAVE PREPARED TO TACKLE YOUR NEXT ENTERPRISE PHP PROJECT.
  3. AGENDA • Why performance matters? • The problems with PHP

    • Best practice designs • Distributed data caches with Redis and Memcached • Doing work in the background with queues • Http caching and a reverse proxy with Varnish • Using the right tool for the job • Tools of the trade • Xdebug + WebGrind • XHProf + XHProf GUI • AppDynamics • Google PageSpeed • Architecture not applications
  4. DUSTIN WHITTLE • dustinwhittle.com • @dustinwhittle • Technologist, Pilot, Skier,

    Diver, Sailor, Golfer
  5. WHAT I HAVE WORKED ON • Developer Evangelist @ •

    Consultant & Trainer @ • Developer Evangelist @
  6. DID YOU KNOW FACEBOOK, YAHOO, ZYNGA, TUMBLR, ETSY, AND WIKIPEDIA

    WERE ALL STARTED ON PHP?
  7. WHY DOES PERFORMANCE MATTER?

  8. WHEN MOZILLA SHAVED 2.2 SECONDS OFF THEIR LANDING PAGE, FIREFOX

    DOWNLOADS INCREASED 15.4%
  9. MAKING BARACK OBAMA’S WEBSITE 60% FASTER INCREASED DONATION CONVERSIONS BY

    14%
  10. AMAZON AND WALMART INCREASED REVENUE 1% FOR EVERY 100MS OF

    IMPROVEMENT
  11. None
  12. PERFORMANCE DIRECTLY IMPACTS THE BOTTOM LINE

  13. None
  14. PHP IS SLOWER THAN JAVA, C++, ERLANG, SCALA, AND GO!

  15. HTTP://PHPSADNESS.COM/

  16. ...AND PHP HAS SOME SERIOUS DESIGN ISSUES AND INCONSISTENCIES!

  17. ...BUT THERE ARE WAYS TO SCALE TO HANDLE HIGH TRAFFIC

    APPLICATIONS
  18. PHP IS NOT YOUR PROBLEM!

  19. WHAT VERSION OF PHP DO YOU RUN?

  20. UPGRADE YOUR PHP ENVIRONMENT TO 2014!

  21. NGINX + PHP-FPM

  22. USE AN OPCODE CACHE!

  23. PHP 5.5 HAS ZEND OPCACHE

  24. APC

  25. USE AUTOLOADING AND PSR-0

  26. SYMFONY2 CLASSLOADER COMPONENT WITH APC CACHING

  27. SCALING BEYOND A SINGLE SERVER IN PHP

  28. OPTIMIZE YOUR SESSIONS!

  29. THE DEFAULT IN PHP IS TO PERSIST SESSIONS TO DISK

  30. IT IS BETTER TO STORE SESSIONS IN A DATABASE

  31. EVEN BETTER IS TO STORE IN A DATABASE WITH A

    SHARED CACHE IN FRONT
  32. PECL INSTALL MEMCACHED

  33. session.save_handler = memcached session.save_path = "10.0.0.10:11211,10.0.0.11:11211,10.0.0.12:11211" memcached.sess_prefix = “session.” memcached.sess_consistent_hash

    = On memcached.sess_remove_failed = 1 memcached.sess_number_of_replicas = 2 memcached.sess_binary = On memcached.sess_randomize_replica_read = On memcached.sess_locking = On memcached.sess_connect_timeout = 200 memcached.serializer = “igbinary”
  34. THE BEST SOLUTION IS TO LIMIT SESSION SIZE AND STORE

    ALL DATA IN A SIGNED OR ENCRYPTED COOKIE
  35. LEVERAGE AN IN-MEMORY DATA CACHE

  36. MEMCACHED.ORG

  37. REDIS.IO

  38. • Any data that is expensive to generate/query and long

    lived should be cached • Web Service Responses • HTTP Responses • Database Result Sets • Configuration Data
  39. GUZZLE HTTP CLIENT HAS BUILT-IN SUPPORT FOR CACHING WEB SERVICE

    REQUESTS
  40. $memcache = new Memcache(); $memcache->connect('localhost', 11211); ! $memcacheDriver = new

    Doctrine\Common\Cache\MemcacheCache(); $memcacheDriver->setMemcache($memcache); ! $client = new Guzzle\HttpClient(‘http://www.test.com/’); ! $cachePlugin = new Guzzle\Plugin\Cache\CachePlugin(array( ‘storage’ => new Guzzle\Plugin\Cache\DefaultCacheStorage( new Guzzle\Plugin\Cache\DoctrineCacheAdapter($memcacheDriver) ) )); $client->addSubscriber($cachePlugin); ! $response = $client->get(‘http://www.wikipedia.org/’)->send(); ! $response = $client->get(‘http://www.wikipedia.org/’)->send();
  41. DOCTRINE ORM FOR PHP HAS BUILT-IN CACHING SUPPORT FOR MEMCACHED

    AND REDIS
  42. $memcache = new Memcache(); $memcache->connect('localhost', 11211); ! $memcacheDriver = new

    Doctrine\Common\Cache\MemcacheCache(); $memcacheDriver->setMemcache($memcache); ! $config = new Doctrine\ORM\Configuration(); $config->setQueryCacheImpl($memcacheDriver); $config->setMetadataCacheImpl($memcacheDriver); $config->setResultCacheImpl($memcacheDriver); ! $entityManager = Doctrine\ORM\EntityManager::create(array(‘driver’ => ‘pdo_sqlite’, ‘path’ => __DIR__ . ‘/db.sqlite’), $config); ! $query = $em->createQuery(‘select u from EntitiesUser u’); $query->useResultCache(true, 60); ! $users = $query->getResult();
  43. DO BLOCKING WORK IN BACKGROUND TASKS VIA QUEUES

  44. • Resque • Gearman • RabbitMQ • Kafka • Beanstalkd

    • ZeroMQ • ActiveMQ
  45. RESQUE

  46. • Any process that is slow and not important for

    the http response should be queued • Sending notifications + posting to social accounts • Analytics + Instrumentation • Updating profiles and discovering friends from social accounts • Consuming web services like Twitter Streaming API
  47. None
  48. None
  49. None
  50. None
  51. LEVERAGE HTTP CACHING

  52. None
  53. None
  54. EXPIRES OR INVALIDATION

  55. EXPIRATION

  56. None
  57. None
  58. VALIDATION

  59. None
  60. None
  61. EXPIRATION AND INVALIDATION

  62. None
  63. None
  64. None
  65. SYMFONY2 HTTPFOUNDATION COMPONENT WITH HTTP CACHING

  66. use Symfony\Component\HttpFoundation\Response; ! $response = new Response(‘Hello World!’, 200, array(‘content-type’

    => ‘text/html’)); ! $response->setCache(array( ‘etag’ => ‘a_unique_id_for_this_resource’, ‘last_modified’ => new DateTime(), ‘max_age’ => 600, ‘s_maxage’ => 600, ‘private’ => false, ‘public’ => true, ));
  67. use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; ! $request = Request::createFromGlobals(); ! $response

    = new Response(‘Hello World!’, 200, array(‘content-type’ => ‘text/html’)); ! if ($response->isNotModified($request)) { $response->send(); }
  68. • Varnish • Squid • Nginx Proxy Cache • Apache

    Proxy Cache
  69. USE VARNISH AS A REVERSE PROXY CACHE TO ALLEVIATE LOAD

    ON YOUR APP SERVERS
  70. OPTIMIZE YOUR FRAMEWORK!

  71. None
  72. • Stay up-to-date with the latest stable version of your

    favorite framework • Disable features you are not using (I18N, Security, etc) • Always use a data cache like Memcached/Redis • Enable caching features for views and database result sets • Always use a HTTP cache like Varnish
  73. LEARN TO HOW TO PROFILE CODE FOR PHP PERFORMANCE

  74. XDEBUG + WEBGRIND

  75. None
  76. XHPROF + XHPROF GUI

  77. None
  78. None
  79. None
  80. None
  81. None
  82. None
  83. PHP AS GLUE

  84. BACKEND = JAVA/SCALA/ ERLANG/GO/NODE ! FRONTEND = PHP OR JAVASCRIPT

  85. YAHOO! & YPHP Move slow code to PHP extensions

  86. FACEBOOK & HHVM A drop-in replacement for PHP (mostly)

  87. None
  88. None
  89. None
  90. • Upgrade to PHP 5.5 with Zend OpCache using PHP-PFM

    + Nginx • Stay up to date with your framework + dependencies (using Composer) • Optimize your session store to use signed cookies or database with caching • Cache your database and web service access with Memcache or Redis • Do blocking work in the background with queues and tasks using Resque • Use HTTP caching and a reverse proxy cache like Varnish • Profile code with Xdebug + Webgrind and monitor production performance
  91. DON’T FORGET TO OPTIMIZE THE CLIENT SIDE

  92. IN MODERN WEB APPLICATIONS MOST OF THE LATENCY COMES FROM

    THE CLIENT SIDE
  93. USE ASSETIC TO OPTIMIZE CLIENT-SIDE ASSETS

  94. GOOGLE PAGESPEED

  95. GOOGLE PAGESPEED INSIGHTS

  96. None
  97. None
  98. GOOGLE PAGESPEED API

  99. CURL "HTTPS://WWW.GOOGLEAPIS.COM/ PAGESPEEDONLINE/V1/RUNPAGESPEED? URL=HTTP://DUSTINWHITTLE.COM/ &KEY=XXX"

  100. SCALABILITY IS ABOUT THE ENTIRE ARCHITECTURE, NOT SOME MINOR CODE

    OPTIMIZATIONS.
  101. QUESTIONS?

  102. FIND THESE SLIDES ON SPEAKERDECK: ! SPEAKERDECK.COM/DUSTINWHITTLE

  103. None