Scaling PHP web apps

70
Scale your PHP web app to get ready for the peak season [email protected] @HZeitlhofer

Transcript of Scaling PHP web apps

Page 1: Scaling PHP web apps

ScaleyourPHPwebapptogetreadyforthepeakseason

[email protected]@HZeitlhofer

Page 2: Scaling PHP web apps

HaraldZeitlhofer

• TechnologyStrategistatDynatrace• DatabaseandWebDevelopment• Lovetodiscovernewthings

Page 3: Scaling PHP web apps

Ouronlineshopistooslow!

Weneedtoscale up!!!

Page 4: Scaling PHP web apps

PerformancevsScalability

Page 5: Scaling PHP web apps
Page 6: Scaling PHP web apps
Page 7: Scaling PHP web apps
Page 8: Scaling PHP web apps

Doesperformancematter?

Page 9: Scaling PHP web apps
Page 10: Scaling PHP web apps
Page 11: Scaling PHP web apps
Page 12: Scaling PHP web apps
Page 13: Scaling PHP web apps
Page 14: Scaling PHP web apps
Page 15: Scaling PHP web apps

Scalabilityismore

• Createwellsizedenvironment• Ensurehighavailability• Ensurehighperformance• Handlelargenumberoftransactions• Respondtochangedloadconditions• Respondtofeaturerequests• Findandfixproblemsfast

Page 16: Scaling PHP web apps

?

Page 17: Scaling PHP web apps

BadNews

PHP is slower than Java

Page 18: Scaling PHP web apps

PHP is not the bottleneck

GoodNews

Page 19: Scaling PHP web apps
Page 20: Scaling PHP web apps

Preparetoscale

• Breakyourapplicationdownintosmallercomponents• Frontend• LoadBalancer• WebServer• CachingServer• PHPengine• Database

• Avoidbottlenecks• Reduceload

Page 21: Scaling PHP web apps

WebRequesthandling

Page 22: Scaling PHP web apps

cachedcontent

canstillcreateroundtripstothenetwork!

Page 23: Scaling PHP web apps

SplitwebserverandPHPengine

http://www.slideshare.net/HaraldZeitlhofer/boost-your-website-by-running-php-on-nginx

Page 24: Scaling PHP web apps

Loadbalancing

upstream php {ip_hash;server unix:/var/run/php5-fpm.sock weight=5;server 192.168.56.12:9000 weight=2;server 192.168.56.13:9000;server 192.168.56.14:9000;server 192.168.58.21:9000 backup;

}

server {listen 80;root /var/www/mysite;server_name www.mysite.com;location / {

try_files $uri =405;}location ~ \.php$ {

fastcgi_pass php;fastcgi_index index.php;include fastcgi_params;

}}

Page 25: Scaling PHP web apps
Page 26: Scaling PHP web apps

Optimizefirst?

Page 27: Scaling PHP web apps

Example:“wehavetocatchmoremice”

Page 28: Scaling PHP web apps

Sofarweonlygottwocats…

Page 29: Scaling PHP web apps
Page 30: Scaling PHP web apps

Focusonwhat’sneededDonotover-optimize

Page 31: Scaling PHP web apps

PHP tier representsa major global bottleneck

for page load times

Example:PerformancehotspotPHPexecution

Page 32: Scaling PHP web apps

lessc CSSpreprocessor

social loginmodule

slow PHPexecution

Majorperformancehotspots

Page 33: Scaling PHP web apps

lessCSSpreprocessor

Page 34: Scaling PHP web apps

server-side calls tounused external services

SocialLoginModule

Page 35: Scaling PHP web apps

average contribution ofPHP compilation time

SlowPHPexecution?OutdatedPHPversion?

Page 36: Scaling PHP web apps
Page 37: Scaling PHP web apps
Page 38: Scaling PHP web apps

7:00a.m.LowLoadandServicerunningonminimum redudancy

12:00p.m.Scaledupserviceduringpeakloadwithfailoverofproblematicnode

7:00p.m.Scaleddownagaintolowerloadandmovetodifferentgeolocation

Deploymentsarenolongerstatic

Page 39: Scaling PHP web apps
Page 40: Scaling PHP web apps
Page 41: Scaling PHP web apps
Page 42: Scaling PHP web apps
Page 43: Scaling PHP web apps

Performanceproblemafterupscaling

Page 44: Scaling PHP web apps
Page 45: Scaling PHP web apps
Page 46: Scaling PHP web apps
Page 47: Scaling PHP web apps

DatabaseAccess

• Sizeserver(cluster)appropriatelyformaximumnumberofconnectedinstances• Reduceconnections• ReduceSQLstatements• Leveragecaching

Page 48: Scaling PHP web apps

Caching!=Caching

Page 49: Scaling PHP web apps

ClientSideCaching

Page 50: Scaling PHP web apps

ServerSideCaching

Page 51: Scaling PHP web apps

Varnish

Page 52: Scaling PHP web apps

Varnish

Page 53: Scaling PHP web apps

NginxFastCGI cache

• PartofNginx'FastCGI module

fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=APPKEY:100m inactive=60m;fastcgi_cache_key "$scheme$request_method$host$request_uri";

location ~* \.php$ {fastcgi_index index.php;fastcgi_pass 127.0.0.1:9000;include fastcgi_params;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_cache APPKEY;fastcgi_cache_valid 200 60m;

}

Page 54: Scaling PHP web apps
Page 55: Scaling PHP web apps
Page 56: Scaling PHP web apps

DatacachewithMemcached<?php

...function __construct () {

$this->c = new Memcached();$this->c->addServer('memcached.server',11211);

}function setCache ($key, $content) {

$this->c->set($key, $content);}

...// get data structure$this->setCache('/gis/cave/getGeoJSON/'.$this->id, $this->getGeoJSON());...

?>

Page 57: Scaling PHP web apps

AccesscacheddatawithNginxupstream php {

server 192.168.56.1:9000;}

server {

location /gis/cave/getGeoJSON {set $memcached_key "$uri";memcached_pass memcached.server:11211;error_page 404 502 504 = @notincache;

}

location @notincache {fastcgi_pass php;

}}

Page 58: Scaling PHP web apps

myfirstmicroservice architecture

Page 59: Scaling PHP web apps

Browser Cache Webserver PHP Database

3rd party 3rd party

Java,.Net,Node.js,…

Page 60: Scaling PHP web apps

ApplicationPerformanceManagement

Page 61: Scaling PHP web apps
Page 62: Scaling PHP web apps
Page 63: Scaling PHP web apps
Page 64: Scaling PHP web apps
Page 65: Scaling PHP web apps
Page 66: Scaling PHP web apps

DatabaseHealth

Page 67: Scaling PHP web apps
Page 68: Scaling PHP web apps

Chartingdatainbusinessdashboards

Page 69: Scaling PHP web apps

Whathavewelearnedtodayaboutscaling?

• Createwellsizedenvironment• Ensurehighavailability• Ensurehighperformance• Handlelargenumberoftransactions• Respondtochangedloadconditions• Respondtofeaturerequests• Findandfixproblemsfast• Monitorproperly

Page 70: Scaling PHP web apps

[email protected]@dynatrace.comhttp://blog.dynatrace.com

DynatraceFreeTrialFreePersonalLicenseRuxit FreeTrialhttp://bit.ly/monitoring-2016