Your SlideShare is downloading. ×
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Scaling PHP in the real world!
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Scaling PHP in the real world!

8,249

Published on

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
8,249
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
8
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Scaling PHP in the real world!
  • 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 withVarnish/Squid • Using the right tool for the job • Tools of the trade • Xdebug + WebGrind • AppDynamics • Google PageSpeed • Architecture not applications We all know performance is important, but performance tuning is too often an afterthought. As a result, taking on a performance tuning project for a slow application can be pretty intimidating – where do you even begin? In this series I’ll tell you about the strategies and technologies that (in my experience) have been the most successful in improving PHP performance. To start off, however, we’ll talk about some of the easy wins in PHP performance tuning. These are the things you can do that’ll get you the most performance bang for your buck, and you should be sure you’ve checked off all of them before you take on any of the more complex stuff.
  • 4. Who am I? • 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 built on PHP?
  • 7. Why does performance matter? http://www.stevesouders.com/blog/2013/05/09/how-fast-are-we-going-now/
  • 8. 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
  • 9. 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/
  • 10. Making Barack Obama’s website 60% faster increased donation conversions by 14% (a $47.5 million dollar impact) http://kylerush.net/blog/meet-the-obama-campaigns-250-million-fundraising-platform/
  • 11. Performance affects the bottom line
  • 12. PHP is slower than Java, C++, Erlang, Scala, and Go!
  • 13. ...but there are ways to scale to handle high traffic applications
  • 14. PHP has some issues and inconsistencies!
  • 15. http://phpsadness.com/ Notice how many issues are getting resolved as the PHP team iterates and releases.
  • 16. PHP is not your problem!
  • 17. What version of PHP do you run?
  • 18. 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.
  • 19. Nginx + PHP-FPM http://nginx.org/ http://php-fpm.org/
  • 20. 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.
  • 21. APC http://php.net/manual/en/book.apc.php
  • 22. Zend Optimizer https://github.com/zendtech/ZendOptimizerPlus
  • 23. XCache http://xcache.lighttpd.net/
  • 24. PHP 5.5 has Zend Optimizer by default
  • 25. 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.
  • 26. Symfony2 ClassLoader Component with APC Caching http://symfony.com/doc/master/components/class_loader.html
  • 27. Scaling beyond a single server
  • 28. 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.
  • 29. The default in PHP is to persist sessions to disk http://www.php.net/manual/en/book.session.php
  • 30. It is better to store sessions in a database http://php.net/manual/en/function.session-set-save-handler.php
  • 31. Even better is to store in a database with a shared cache in front http://php.net/manual/en/memcached.sessions.php
  • 32. 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
  • 33. 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.
  • 34. Memcached http://memcached.org/
  • 35. Redis http://redis.io/
  • 36. • Any data that is expensive to generate/ query and long lived should be cached • Web Service Responses • HTTP Responses • Database Result Sets • Configuration Data
  • 37. I highly recommend the Doctrine ORM for PHP which has built-in caching support for Memcached or Redis. http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/caching.html
  • 38. Do blocking work in the background 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/
  • 43. http://kamisama.me/2012/10/09/background-jobs-with-php-and-resque-part-2-queue- system/ http://kamisama.me/2012/10/09/background-jobs-with-php-and-resque-part-3- installation/
  • 44. http://kamisama.me/2012/10/12/background-jobs-with-php-and-resque-part-4- managing-worker/ http://kamisama.me/2012/10/13/background-jobs-with-php-and-resque-part-5-creating- jobs/
  • 45. http://kamisama.me/2012/10/22/background-jobs-with-php-and-resque-part-7- manage-workers-with-fresque/ http://kamisama.me/2012/10/22/background-jobs-with-php-and-resque-part-8-a- glance-at-php-resque-ex/ http://resqueboard.kamisama.me/
  • 46. Leverage HTTP caching HTTP caching is one of the most misunderstood technologies on the Internet. Go read the HTTP caching specification. Don’t worry, I’ll wait. Seriously, go do it! They solved all of these caching design problems a few decades ago. It boils down to expiration or invalidation and when used properly can save your app servers a lot of load. Please read the excellent HTTP caching guide from Mark Nottingam. I highly recommend using Varnish as a reverse proxy cache to alleviate load on your app servers.
  • 47. RTFM http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html http://www.mnot.net/cache_docs/
  • 48. Expires or Invalidation
  • 49. • Varnish • Squid • Nginx Proxy Cache • Apache Proxy Cache
  • 50. I recommend usingVarnish as a reverse proxy cache to alleviate load on your app servers.
  • 51. Optimize your framework! Deep diving into the specifics of optimizing each framework is outside of the scope of this post, but these principles apply to every framework: Stay up-to-date with the latest stable version of your favorite framework Disable features you are not using (I18N, Security, etc) Enable caching features for view and result set caching
  • 52. • Stay up-to-date with the latest stable version of your favorite framework • Disable features you are not using (I18N, Security, etc) • Enable caching features for view and result set caching
  • 53. Sharding
  • 54. Taking a large problem and making it into manageable smaller problems
  • 55. Big apps use Service Oriented Architectures with Java/Scala/ Erlang backends with PHP or pure JavaScript frontends
  • 56. Companies of great scale move away from PHP or create their own variant
  • 57. Yahoo! & yPHP
  • 58. Facebook & HipHop HipHop 1.0 - HPHPc - Transformed subset of PHP code to C++ for performance HipHop 2.0 - HHVM - Virtual Machine, Runtime, and JIT for PHP https://github.com/facebook/hiphop-php https://github.com/facebook/hiphop-php/wiki
  • 59. Learn to how to profile code for PHP performance Xdebug is a PHP extension for powerful debugging. It supports stack and function traces, profiling information and memory allocation and script execution analysis. It allows developers to easily profile PHP code. WebGrind is an Xdebug profiling web frontend in PHP5. It implements a subset of the features of kcachegrind and installs in seconds and works on all platforms. For quick-and-dirty optimizations it does the job. Here’s a screenshot showing the output from profiling. XHprof is a function-level hierarchical profiler for PHP with a reporting and UI layer. XHProf is capable of reporting function-level inclusive and exclusive wall times, memory usage, CPU times and number of calls for each function. Additionally, it supports the ability to compare two runs (hierarchical DIFF reports) or aggregate results from multiple runs. XHprof XHGui AppDynamics is application performance management software designed to help dev and ops troubleshoot problems in complex production apps.
  • 60. Xdebug + WebGrind
  • 61. http://xdebug.org/ https://github.com/jokkedk/webgrind http://valgrind.org/info/tools.html#callgrind
  • 62. http://appdynamics.com/
  • 63. Don’t forget to optimize the client side PHP application performance is only part of the battle Now that you have optimized the server-side, you can spend time improving the client side! In modern web applications most of the end-user experience time is spent waiting on the client side to render. Google has dedicated many resources to helping developers improve client side performance.
  • 64. In modern web applications most of the latency comes from the client side In modern web applications most of the end-user experience time is spent waiting on the client side to render.
  • 65. Google PageSpeed https://developers.google.com/speed/pagespeed/ https://developers.google.com/speed/docs/best-practices/rules_intro
  • 66. Scalability is about the entire architecture, not some minor code optimizations.
  • 67. Questions?
  • 68. Please give any feedback on Joind.in http://joind.in/talk/view/8893 http://joind.in/talk/view/8893
  • 69. Find these slides on SpeakerDeck https://speakerdeck.com/ dustinwhittle https://speakerdeck.com/dustinwhittle

×