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

OSCON - Scaling PHP in the real world!

OSCON - Scaling PHP in the real world!

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

Dustin Whittle

July 23, 2014
Tweet

More Decks by Dustin Whittle

Other Decks in Technology

Transcript

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

    View Slide

  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.

    View Slide

  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

    View Slide

  4. DUSTIN WHITTLE
    • dustinwhittle.com
    • @dustinwhittle
    • Technologist, Pilot, Skier, Diver, Sailor, Golfer

    View Slide

  5. WHAT I HAVE WORKED ON
    • Developer Evangelist @
    • Consultant & Trainer @
    • Developer Evangelist @

    View Slide

  6. DID YOU KNOW FACEBOOK, YAHOO,
    ZYNGA, TUMBLR, ETSY, AND
    WIKIPEDIA WERE ALL STARTED ON
    PHP?

    View Slide

  7. WHY DOES PERFORMANCE
    MATTER?

    View Slide

  8. WHEN MOZILLA SHAVED 2.2 SECONDS
    OFF THEIR LANDING PAGE, FIREFOX
    DOWNLOADS INCREASED 15.4%

    View Slide

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

    View Slide

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

    View Slide

  11. View Slide

  12. PERFORMANCE DIRECTLY
    IMPACTS THE BOTTOM LINE

    View Slide

  13. View Slide

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

    View Slide

  15. HTTP://PHPSADNESS.COM/

    View Slide

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

    View Slide

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

    View Slide

  18. PHP IS NOT YOUR
    PROBLEM!

    View Slide

  19. WHAT VERSION OF PHP DO
    YOU RUN?

    View Slide

  20. UPGRADE YOUR PHP
    ENVIRONMENT TO 2014!

    View Slide

  21. NGINX + PHP-FPM

    View Slide

  22. USE AN OPCODE CACHE!

    View Slide

  23. PHP 5.5 HAS ZEND
    OPCACHE

    View Slide

  24. APC

    View Slide

  25. USE AUTOLOADING AND
    PSR-0

    View Slide

  26. SYMFONY2 CLASSLOADER
    COMPONENT WITH APC
    CACHING

    View Slide

  27. SCALING BEYOND A
    SINGLE SERVER IN PHP

    View Slide

  28. OPTIMIZE YOUR SESSIONS!

    View Slide

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

    View Slide

  30. IT IS BETTER TO STORE
    SESSIONS IN A DATABASE

    View Slide

  31. EVEN BETTER IS TO STORE
    IN A DATABASE WITH A
    SHARED CACHE IN FRONT

    View Slide

  32. PECL INSTALL MEMCACHED

    View Slide

  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”

    View Slide

  34. THE BEST SOLUTION IS TO LIMIT
    SESSION SIZE AND STORE ALL DATA
    IN A SIGNED OR ENCRYPTED COOKIE

    View Slide

  35. LEVERAGE AN IN-MEMORY
    DATA CACHE

    View Slide

  36. MEMCACHED.ORG

    View Slide

  37. REDIS.IO

    View Slide

  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

    View Slide

  39. GUZZLE HTTP CLIENT HAS
    BUILT-IN SUPPORT FOR CACHING
    WEB SERVICE REQUESTS

    View Slide

  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();

    View Slide

  41. DOCTRINE ORM FOR PHP HAS
    BUILT-IN CACHING SUPPORT
    FOR MEMCACHED AND REDIS

    View Slide

  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();

    View Slide

  43. DO BLOCKING WORK IN
    BACKGROUND TASKS VIA
    QUEUES

    View Slide

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

    View Slide

  45. RESQUE

    View Slide

  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

    View Slide

  47. View Slide

  48. View Slide

  49. View Slide

  50. View Slide

  51. LEVERAGE HTTP CACHING

    View Slide

  52. View Slide

  53. View Slide

  54. EXPIRES OR INVALIDATION

    View Slide

  55. EXPIRATION

    View Slide

  56. View Slide

  57. View Slide

  58. VALIDATION

    View Slide

  59. View Slide

  60. View Slide

  61. EXPIRATION AND
    INVALIDATION

    View Slide

  62. View Slide

  63. View Slide

  64. View Slide

  65. SYMFONY2 HTTPFOUNDATION
    COMPONENT WITH HTTP
    CACHING

    View Slide

  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,
    ));

    View Slide

  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();
    }

    View Slide

  68. • Varnish
    • Squid
    • Nginx Proxy Cache
    • Apache Proxy Cache

    View Slide

  69. USE VARNISH AS A REVERSE
    PROXY CACHE TO ALLEVIATE
    LOAD ON YOUR APP SERVERS

    View Slide

  70. OPTIMIZE YOUR
    FRAMEWORK!

    View Slide

  71. View Slide

  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

    View Slide

  73. LEARN TO HOW TO PROFILE
    CODE FOR PHP
    PERFORMANCE

    View Slide

  74. XDEBUG + WEBGRIND

    View Slide

  75. View Slide

  76. XHPROF + XHPROF GUI

    View Slide

  77. View Slide

  78. View Slide

  79. View Slide

  80. View Slide

  81. View Slide

  82. View Slide

  83. PHP AS GLUE

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  87. View Slide

  88. View Slide

  89. View Slide

  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

    View Slide

  91. DON’T FORGET TO
    OPTIMIZE THE CLIENT SIDE

    View Slide

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

    View Slide

  93. USE ASSETIC TO OPTIMIZE
    CLIENT-SIDE ASSETS

    View Slide

  94. GOOGLE PAGESPEED

    View Slide

  95. GOOGLE PAGESPEED
    INSIGHTS

    View Slide

  96. View Slide

  97. View Slide

  98. GOOGLE PAGESPEED API

    View Slide

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

    View Slide

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

    View Slide

  101. QUESTIONS?

    View Slide

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

    View Slide

  103. View Slide