108
SCALING PHP IN THE REAL WORLD! Dustin Whittle

Scaling PHP in The Real World

Embed Size (px)

DESCRIPTION

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.

Citation preview

Page 1: Scaling PHP in The Real World

SCALING PHP IN THE REAL WORLD!Dustin Whittle

Page 2: 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.

Page 3: Scaling PHP in The Real World

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

Page 4: Scaling PHP in The Real World

DUSTIN WHITTLE

• dustinwhittle.com

• @dustinwhittle

• Technologist, Pilot, Skier, Diver, Sailor, Golfer

Page 5: Scaling PHP in The Real World

WHAT I HAVE WORKED ON

• Developer Evangelist @

• Consultant & Trainer @

• Developer Evangelist @

Page 6: Scaling PHP in The Real World

DID YOU KNOW FACEBOOK, YAHOO, ZYNGA, TUMBLR, ETSY, AND

WIKIPEDIA WERE ALL BUILT ON PHP?

Page 7: Scaling PHP in The Real World

WHY DOES PERFORMANCE MATTER?

Page 8: Scaling PHP in The Real World

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

Page 9: Scaling PHP in The Real World

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

Page 10: Scaling PHP in The Real World

AMAZON AND WALMART INCREASED REVENUE 1% FOR

EVERY 100MS OF IMPROVEMENT

Page 11: Scaling PHP in The Real World
Page 12: Scaling PHP in The Real World

PERFORMANCE DIRECTLY IMPACTS THE BOTTOM LINE

Page 13: Scaling PHP in The Real World
Page 14: Scaling PHP in The Real World

PHP IS SLOWER THAN JAVA, C++, ERLANG, SCALA, AND

GO!

Page 15: Scaling PHP in The Real World

HTTP://PHPSADNESS.COM/

Page 16: Scaling PHP in The Real World

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

Page 17: Scaling PHP in The Real World

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

Page 18: Scaling PHP in The Real World

PHP IS NOT YOUR PROBLEM!

Page 19: Scaling PHP in The Real World

WHAT VERSION OF PHP DO YOU RUN?

Page 20: Scaling PHP in The Real World

UPGRADE YOUR PHP ENVIRONMENT TO 2014!

Page 21: Scaling PHP in The Real World

NGINX + PHP-FPM

Page 22: Scaling PHP in The Real World

USE AN OPCODE CACHE!

Page 23: Scaling PHP in The Real World

PHP 5.5 HAS ZEND OPCACHE

Page 24: Scaling PHP in The Real World

APC

Page 25: Scaling PHP in The Real World

USE AUTOLOADING AND PSR-0

Page 26: Scaling PHP in The Real World

SYMFONY2 CLASSLOADER COMPONENT WITH APC

CACHING

Page 27: Scaling PHP in The Real World

SCALING BEYOND A SINGLE SERVER IN PHP

Page 28: Scaling PHP in The Real World

OPTIMIZE YOUR SESSIONS!

Page 29: Scaling PHP in The Real World

THE DEFAULT IN PHP IS TO PERSIST SESSIONS TO DISK

Page 30: Scaling PHP in The Real World

IT IS BETTER TO STORE SESSIONS IN A DATABASE

Page 31: Scaling PHP in The Real World

EVEN BETTER IS TO STORE IN A DATABASE WITH A

SHARED CACHE IN FRONT

Page 32: Scaling PHP in The Real World

PECL INSTALL MEMCACHED

Page 33: Scaling PHP in The Real World

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”

Page 34: Scaling PHP in The Real World

THE BEST SOLUTION IS TO LIMIT SESSION SIZE AND STORE ALL DATA

IN A SIGNED OR ENCRYPTED COOKIE

Page 35: Scaling PHP in The Real World

LEVERAGE AN IN-MEMORY DATA CACHE

Page 36: Scaling PHP in The Real World

MEMCACHED.ORG

Page 37: Scaling PHP in The Real World

REDIS.IO

Page 38: Scaling PHP in The Real World

• Any data that is expensive to generate/query and long lived should be cached

• Web Service Responses

• HTTP Responses

• Database Result Sets

• Configuration Data

Page 39: Scaling PHP in The Real World

GUZZLE HTTP CLIENT HAS BUILT-IN SUPPORT FOR CACHING

WEB SERVICE REQUESTS

Page 40: Scaling PHP in The Real World

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

Page 41: Scaling PHP in The Real World

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

Page 42: Scaling PHP in The Real World

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

Page 43: Scaling PHP in The Real World

DO BLOCKING WORK IN BACKGROUND TASKS VIA

QUEUES

Page 44: Scaling PHP in The Real World

• Resque

• Gearman

• RabbitMQ

• Kafka

• Beanstalkd

• ZeroMQ

• ActiveMQ

Page 45: Scaling PHP in The Real World

RESQUE

Page 46: Scaling PHP in The Real World

• 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

Page 47: Scaling PHP in The Real World
Page 48: Scaling PHP in The Real World
Page 49: Scaling PHP in The Real World
Page 50: Scaling PHP in The Real World
Page 51: Scaling PHP in The Real World

LEVERAGE HTTP CACHING

Page 52: Scaling PHP in The Real World
Page 53: Scaling PHP in The Real World
Page 54: Scaling PHP in The Real World

EXPIRES OR INVALIDATION

Page 55: Scaling PHP in The Real World

EXPIRATION

Page 56: Scaling PHP in The Real World
Page 57: Scaling PHP in The Real World
Page 58: Scaling PHP in The Real World

VALIDATION

Page 59: Scaling PHP in The Real World
Page 60: Scaling PHP in The Real World
Page 61: Scaling PHP in The Real World

EXPIRATION AND INVALIDATION

Page 62: Scaling PHP in The Real World
Page 63: Scaling PHP in The Real World
Page 64: Scaling PHP in The Real World
Page 65: Scaling PHP in The Real World

SYMFONY2 HTTPFOUNDATION COMPONENT WITH HTTP

CACHING

Page 66: Scaling PHP in The Real World

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,

));

Page 67: Scaling PHP in The Real World

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

}

Page 68: Scaling PHP in The Real World

• Varnish

• Squid

• Nginx Proxy Cache

• Apache Proxy Cache

Page 69: Scaling PHP in The Real World

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

Page 70: Scaling PHP in The Real World

OPTIMIZE YOUR FRAMEWORK!

Page 71: Scaling PHP in The Real World
Page 72: Scaling PHP in The Real World

• 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

Page 73: Scaling PHP in The Real World

SHARDING

Page 74: Scaling PHP in The Real World

TAKING A LARGE PROBLEM AND MAKING IT INTO MANAGEABLE

SMALLER PROBLEMS

Page 75: Scaling PHP in The Real World
Page 76: Scaling PHP in The Real World
Page 77: Scaling PHP in The Real World

SERVICE ORIENTED ARCHITECTURE !

JAVA/SCALA/ERLANG/GO/NODE BACKEND !

PHP OR PURE JAVASCRIPT FRONTEND

Page 78: Scaling PHP in The Real World

PHP AS GLUE

Page 79: Scaling PHP in The Real World

COMPANIES OF GREAT SCALE MOVE AWAY FROM PHP OR

CREATE THEIR OWN VARIANT

Page 80: Scaling PHP in The Real World

YAHOO! & YPHP

Page 81: Scaling PHP in The Real World

FACEBOOK & HIPHOP

Page 82: Scaling PHP in The Real World
Page 83: Scaling PHP in The Real World

LEARN TO HOW TO PROFILE CODE FOR PHP

PERFORMANCE

Page 84: Scaling PHP in The Real World

XDEBUG + WEBGRIND

Page 85: Scaling PHP in The Real World
Page 86: Scaling PHP in The Real World

XHPROF + XHPROF GUI

Page 87: Scaling PHP in The Real World
Page 88: Scaling PHP in The Real World
Page 89: Scaling PHP in The Real World
Page 90: Scaling PHP in The Real World
Page 91: Scaling PHP in The Real World
Page 92: Scaling PHP in The Real World
Page 93: Scaling PHP in The Real World
Page 94: Scaling PHP in The Real World
Page 95: Scaling PHP in The Real World

• 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

Page 96: Scaling PHP in The Real World

DON’T FORGET TO OPTIMIZE THE CLIENT SIDE

Page 97: Scaling PHP in The Real World

IN MODERN WEB APPLICATIONS MOST OF THE LATENCY COMES

FROM THE CLIENT SIDE

Page 98: Scaling PHP in The Real World

USE ASSETIC TO OPTIMIZE CLIENT-SIDE ASSETS

Page 99: Scaling PHP in The Real World

GOOGLE PAGESPEED

Page 100: Scaling PHP in The Real World

GOOGLE PAGESPEED INSIGHTS

Page 101: Scaling PHP in The Real World
Page 102: Scaling PHP in The Real World
Page 103: Scaling PHP in The Real World

GOOGLE PAGESPEED API

Page 104: Scaling PHP in The Real World

CURL "HTTPS://WWW.GOOGLEAPIS.COM/PAGESPEEDONLINE/V1/RUNPAGESPEED?

URL=HTTP://DUSTINWHITTLE.COM/&KEY=XXX"

Page 105: Scaling PHP in The Real World

SCALABILITY IS ABOUT THE ENTIRE ARCHITECTURE, NOT SOME

MINOR CODE OPTIMIZATIONS.

Page 106: Scaling PHP in The Real World

QUESTIONS?

Page 107: Scaling PHP in The Real World

FIND THESE SLIDES ON SPEAKERDECK !

HTTPS://SPEAKERDECK.COM/DUSTINWHITTLE

Page 108: Scaling PHP in The Real World