Your SlideShare is downloading. ×
Scaling PHP in The Real World
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Scaling PHP in The Real World

1,232
views

Published on

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 …

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.

Published in: Technology

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,232
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
32
Comments
0
Likes
2
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! Dustin Whittle
  • 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 BUILT ON PHP?
  • 7. WHY DOES PERFORMANCE MATTER?
  • 8. WHEN MOZILLA SHAVED 2.2 SECONDS OFFTHEIR 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. PERFORMANCE DIRECTLY IMPACTS THE BOTTOM LINE
  • 12. PHP IS SLOWER THAN JAVA, C++, ERLANG, SCALA, AND GO!
  • 13. HTTP://PHPSADNESS.COM/
  • 14. ...AND PHP HAS SOME SERIOUS DESIGN ISSUES AND INCONSISTENCIES!
  • 15. ...BUT THERE ARE WAYS TO SCALE TO HANDLE HIGH TRAFFIC APPLICATIONS
  • 16. PHP IS NOT YOUR PROBLEM!
  • 17. WHAT VERSION OF PHP DO YOU RUN?
  • 18. UPGRADE YOUR PHP ENVIRONMENT TO 2014!
  • 19. NGINX + PHP-FPM
  • 20. USE AN OPCODE CACHE!
  • 21. PHP 5.5 HAS ZEND OPCACHE
  • 22. APC
  • 23. USE AUTOLOADING AND PSR-0
  • 24. SYMFONY2 CLASSLOADER COMPONENT WITH APC CACHING
  • 25. SCALING BEYOND A SINGLE SERVER IN PHP
  • 26. OPTIMIZE YOUR SESSIONS!
  • 27. THE DEFAULT IN PHP IS TO PERSIST SESSIONS TO DISK
  • 28. IT IS BETTER TO STORE SESSIONS IN A DATABASE
  • 29. EVEN BETTER IS TO STORE IN A DATABASE WITH A SHARED CACHE IN FRONT
  • 30. PECL INSTALL MEMCACHED
  • 31. 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”
  • 32. THE BEST SOLUTION IS TO LIMIT SESSION SIZE AND STORE ALL DATA IN A SIGNED OR ENCRYPTED COOKIE
  • 33. LEVERAGE AN IN-MEMORY DATA CACHE
  • 34. MEMCACHED.ORG
  • 35. 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. GUZZLE HTTP CLIENT HAS BUILT-IN SUPPORT FOR CACHING WEB SERVICE REQUESTS
  • 38. $memcache = new Memcache(); $memcache->connect('localhost', 11211); ! $memcacheDriver = new DoctrineCommonCacheMemcacheCache(); $memcacheDriver->setMemcache($memcache); ! $client = new GuzzleHttpClient(‘http://www.test.com/’); ! $cachePlugin = new GuzzlePluginCacheCachePlugin(array( ‘storage’ => new GuzzlePluginCacheDefaultCacheStorage( new GuzzlePluginCacheDoctrineCacheAdapter($memcacheDriver) ) )); $client->addSubscriber($cachePlugin); ! $response = $client->get(‘http://www.wikipedia.org/’)->send(); ! $response = $client->get(‘http://www.wikipedia.org/’)->send();
  • 39. DOCTRINE ORM FOR PHP HAS BUILT-IN CACHING SUPPORT FOR MEMCACHED AND REDIS
  • 40. $memcache = new Memcache(); $memcache->connect('localhost', 11211); ! $memcacheDriver = new DoctrineCommonCacheMemcacheCache(); $memcacheDriver->setMemcache($memcache); ! $config = new DoctrineORMConfiguration(); $config->setQueryCacheImpl($memcacheDriver); $config->setMetadataCacheImpl($memcacheDriver); $config->setResultCacheImpl($memcacheDriver); ! $entityManager = DoctrineORMEntityManager::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();
  • 41. DO BLOCKING WORK IN BACKGROUND TASKS VIA QUEUES
  • 42. • Resque • Gearman • RabbitMQ • Kafka • Beanstalkd • ZeroMQ • ActiveMQ
  • 43. RESQUE
  • 44. • 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
  • 45. LEVERAGE HTTP CACHING
  • 46. EXPIRES OR INVALIDATION
  • 47. EXPIRATION
  • 48. VALIDATION
  • 49. EXPIRATION AND INVALIDATION
  • 50. SYMFONY2 HTTPFOUNDATION COMPONENT WITH HTTP CACHING
  • 51. use SymfonyComponentHttpFoundationResponse; ! $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, ));
  • 52. use SymfonyComponentHttpFoundationRequest; use SymfonyComponentHttpFoundationResponse; ! $request = Request::createFromGlobals(); ! $response = new Response(‘Hello World!’, 200, array(‘content- type’ => ‘text/html’)); ! if ($response->isNotModified($request)) { $response->send(); }
  • 53. • Varnish • Squid • Nginx Proxy Cache • Apache Proxy Cache
  • 54. USE VARNISH AS A REVERSE PROXY CACHE TO ALLEVIATE LOAD ON YOUR APP SERVERS
  • 55. OPTIMIZE YOUR FRAMEWORK!
  • 56. • 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
  • 57. SHARDING
  • 58. TAKING A LARGE PROBLEM AND MAKING IT INTO MANAGEABLE SMALLER PROBLEMS
  • 59. SERVICE ORIENTED ARCHITECTURE ! JAVA/SCALA/ERLANG/GO/NODE BACKEND ! PHP OR PURE JAVASCRIPT FRONTEND
  • 60. PHP AS GLUE
  • 61. COMPANIES OF GREAT SCALE MOVE AWAY FROM PHP OR CREATE THEIR OWN VARIANT
  • 62. YAHOO! & YPHP
  • 63. FACEBOOK & HIPHOP
  • 64. LEARN TO HOW TO PROFILE CODE FOR PHP PERFORMANCE
  • 65. XDEBUG + WEBGRIND
  • 66. XHPROF + XHPROF GUI
  • 67. • 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
  • 68. DON’T FORGET TO OPTIMIZE THE CLIENT SIDE
  • 69. IN MODERN WEB APPLICATIONS MOST OF THE LATENCY COMES FROM THE CLIENT SIDE
  • 70. USE ASSETIC TO OPTIMIZE CLIENT-SIDE ASSETS
  • 71. GOOGLE PAGESPEED
  • 72. GOOGLE PAGESPEED INSIGHTS
  • 73. GOOGLE PAGESPEED API
  • 74. CURL "HTTPS://WWW.GOOGLEAPIS.COM/ PAGESPEEDONLINE/V1/RUNPAGESPEED? URL=HTTP://DUSTINWHITTLE.COM/ &KEY=XXX"
  • 75. SCALABILITY IS ABOUTTHE ENTIRE ARCHITECTURE, NOT SOME MINOR CODE OPTIMIZATIONS.
  • 76. QUESTIONS?
  • 77. FIND THESE SLIDES ON SPEAKERDECK ! HTTPS://SPEAKERDECK.COM/ DUSTINWHITTLE