scaling php in the real world
play

SCALING PHP IN THE REAL WORLD! PHP is used by the likes of - PDF document

SCALING PHP IN THE REAL WORLD! 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


  1. SCALING PHP IN THE REAL WORLD! 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. Why performance matters? The problems with PHP Best practice designs Doing work in the background with queues Fronting with http caching and a reverse proxy cache Distributed data caches with redis and memcached Using the right tool for the job Tools of the trade Opcode Caches Varnish/Squid and reverse proxy caches Xdebug + Valgrind + WebGrind AppDynamics Architecture not applications

  2. Dustin Whittle • dustinwhittle.com • @dustinwhittle • Technologist, Pilot, Skier, Diver, Sailor, Golfer

  3. What I have worked on • Developer Evangelist @ • Consultant & Trainer @ • Developer Evangelist @

  4. Did you know Facebook, Yahoo, Zynga, Tumblr, Etsy, and Wikipedia were all built on PHP?

  5. Why does performance matter? http://www.stevesouders.com/blog/2013/05/09/how-fast-are-we-going-now/ http://www.appdynamics.com/blog/2013/07/04/tools-of-the-trade-for-performance-and- load-testing/ The majority of Americans are said to wait in line (in a real shop) for no longer than 15 minutes. 1 out of 4 customers will abandon a webpage that takes more than 4 seconds to load. http://www.scribd.com/doc/16877317/Shopzillas-Site-Redo-You-Get-What-You-Measure http://velocityconf.com/velocity2010/public/schedule/detail/13019 http://www.aberdeen.com/Aberdeen-Library/5136/RA-performance-web-application.aspx http://blog.mozilla.org/metrics/2010/04/05/firefox-page-load-speed-%E2%80%93-part-ii/

  6. Microsoft found that Bing searches that were 2 seconds slower resulted in a 4.3% drop in revenue per user http://velocityconf.com/velocity2009/public/schedule/detail/8523

  7. When Mozilla shaved 2.2 seconds off their landing page, Firefox downloads increased 15.4% https://blog.mozilla.org/metrics/2010/04/05/firefox-page-load-speed-%E2%80%93-part-ii/ (60 million more downloads)

  8. Making Barack Obama’s website 60% faster increased donation conversions by 14% http://kylerush.net/blog/meet-the-obama-campaigns-250-million-fundraising-platform/

  9. Amazon and Walmart increased revenue 1% for every 100ms of improvement http://www.globaldots.com/how-website-speed-a fg ects-conversion-rates/

  10. Amazon and Walmart increased revenue 1% for every 100ms of improvement http://www.strangeloopnetworks.com/web-performance-infographics/

  11. Performance directly impacts the bottom line

  12. http://www.strangeloopnetworks.com/web-performance-infographics/

  13. http://www.strangeloopnetworks.com/web-performance-infographics/

  14. PHP is slower than Java, C++, Erlang, Scala, and Go!

  15. ...but there are ways to scale to handle high traffic applications

  16. http://phpsadness.com/ Notice how many issues are getting resolved as the PHP team iterates and releases.

  17. PHP is not your problem!

  18. What version of PHP do you run?

  19. Upgrade your PHP environment to 2013! One of the easiest improvements you can make to improve performance and stability is to upgrade your version of PHP. PHP 5.3.x was released in 2009. If you haven’t migrated to PHP 5.4, now is the time! Not only do you benefit from bug fixes and new features, but you will also see faster response times immediately. See PHP.net to get started. Installing the latest PHP on Linux - http://php.net/manual/en/install.unix.debian.php Installing the latest PHP on OSX - http://php.net/manual/en/install.macosx.php Installing the latest PHP on Windows - http://php.net/manual/en/install.windows.php Once you’ve finished upgrading PHP, be sure to disable any unused extensions in production such as xdebug or xhprof.

  20. Nginx + PHP-FPM http://nginx.org/ http://php-fpm.org/

  21. Use an opcode cache! PHP is an interpreted language, which means that every time a PHP page is requested, the server will interpet the PHP file and compile it into something the machine can understand (opcode). Opcode caches preserve this generated code in a cache so that it will only need to be interpreted on the first request. If you aren’t using an opcode cache you’re missing out on a very easy performance gain. Pick your flavor: APC, Zend Optimizer, XCache, or Eaccellerator. I highly recommend APC, written by the creator of PHP, Rasmus Lerdorf.

  22. PHP 5.5 has Zend OpCache

  23. APC http://php.net/manual/en/book.apc.php

  24. Use autoloading and PSR-0 Many developers writing object-oriented applications create one PHP source file per class definition. One of the biggest annoyances in writing PHP is having to write a long list of needed includes at the beginning of each script (one for each class). PHP re-evaluates these require/include expressions over and over during the evaluation period each time a file containing one or more of these expressions is loaded into the runtime. Using an autoloader will enable you to remove all of your require/include statements and benefit from a performance improvement. You can even cache the class map of your autoloader in APC for a small performance improvement.

  25. Symfony2 ClassLoader Component with APC Caching http://symfony.com/doc/master/components/class_loader.html

  26. Scaling beyond a single server in PHP

  27. Optimize your sessions! While HTTP is stateless, most real life web applications require a way to manage user data. In PHP, application state is managed via sessions. The default configuration for PHP is to persist session data to disk. This is extremely slow and not scalable beyond a single server. A better solution is to store your session data in a database and front with an LRU (Least Recently Used) cache with Memcached or Redis. If you are super smart you will realize you should limit your session data size (4096 bytes) and store all session data in a signed or encrypted cookie.

  28. The default in PHP is to persist sessions to disk http://www.php.net/manual/en/book.session.php

  29. It is better to store sessions in a database http://php.net/manual/en/function.session-set-save-handler.php

  30. Even better is to store in a database with a shared cache in front http://php.net/manual/en/memcached.sessions.php

  31. The best solution is to limit session size and store all data in a signed or encrypted cookie http://www.hardened-php.net/suhosin/configuration.html#suhosin.session.encrypt

  32. Leverage an in- memory data cache Applications usually require data. Data is usually structured and organized in a database. Depending on the data set and how it is accessed it can be expensive to query. An easy solution is to cache the result of the first query in a data cache like Memcached or Redis. If the data changes, you invalidate the cache and make another SQL query to get the updated result set from the database. I highly recommend the Doctrine ORM for PHP which has built-in caching support for Memcached or Redis. There are many use cases for a distributed data cache from caching web service responses and app configurations to entire rendered pages.

  33. Memcached.org http://memcached.org/

  34. Redis.io http://redis.io/

  35. • Any data that is expensive to generate/query and long lived should be cached • Web Service Responses • HTTP Responses • Database Result Sets • Configuration Data

  36. Guzzle HTTP client has built-in support for caching web service requests

  37. Doctrine ORM for PHP has built-in caching support for Memcached and Redis http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/caching.html

  38. Do blocking work in background tasks via queues Often times web applications have to run tasks that can take a while to complete. In most cases there is no good reason to force the end-user to have to wait for the job to finish. The solution is to queue blocking work to run in background jobs. Background jobs are jobs that are executed outside the main flow of your program, and usually handled by a queue or message system. There are a lot of great solutions that can help solve running backgrounds jobs. The benefits come in terms of both end-user experience and scaling by writing and processing long running jobs from a queue. I am a big fan of Resque for PHP that is a simple toolkit for running tasks from queues. There are a variety of tools that provide queuing or messaging systems that work well with PHP:

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

  40. Resque https://github.com/chrisboulton/php-resque https://github.com/kamisama/php-resque-ex/ https://github.com/kamisama/ResqueBoard/

  41. • 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

  42. http://kamisama.me/2012/10/09/background-jobs-with-php-and-resque-part-1- introduction/

Recommend


More recommend