scaling php web apps
TRANSCRIPT
ScaleyourPHPwebapptogetreadyforthepeakseason
[email protected]@HZeitlhofer
HaraldZeitlhofer
• TechnologyStrategistatDynatrace• DatabaseandWebDevelopment• Lovetodiscovernewthings
Ouronlineshopistooslow!
Weneedtoscale up!!!
PerformancevsScalability
Doesperformancematter?
Scalabilityismore
• Createwellsizedenvironment• Ensurehighavailability• Ensurehighperformance• Handlelargenumberoftransactions• Respondtochangedloadconditions• Respondtofeaturerequests• Findandfixproblemsfast
?
BadNews
PHP is slower than Java
PHP is not the bottleneck
GoodNews
Preparetoscale
• Breakyourapplicationdownintosmallercomponents• Frontend• LoadBalancer• WebServer• CachingServer• PHPengine• Database
• Avoidbottlenecks• Reduceload
WebRequesthandling
cachedcontent
canstillcreateroundtripstothenetwork!
SplitwebserverandPHPengine
http://www.slideshare.net/HaraldZeitlhofer/boost-your-website-by-running-php-on-nginx
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;
}}
Optimizefirst?
Example:“wehavetocatchmoremice”
Sofarweonlygottwocats…
Focusonwhat’sneededDonotover-optimize
PHP tier representsa major global bottleneck
for page load times
Example:PerformancehotspotPHPexecution
lessc CSSpreprocessor
social loginmodule
slow PHPexecution
Majorperformancehotspots
lessCSSpreprocessor
server-side calls tounused external services
SocialLoginModule
average contribution ofPHP compilation time
SlowPHPexecution?OutdatedPHPversion?
7:00a.m.LowLoadandServicerunningonminimum redudancy
12:00p.m.Scaledupserviceduringpeakloadwithfailoverofproblematicnode
7:00p.m.Scaleddownagaintolowerloadandmovetodifferentgeolocation
Deploymentsarenolongerstatic
Performanceproblemafterupscaling
DatabaseAccess
• Sizeserver(cluster)appropriatelyformaximumnumberofconnectedinstances• Reduceconnections• ReduceSQLstatements• Leveragecaching
Caching!=Caching
ClientSideCaching
ServerSideCaching
Varnish
Varnish
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;
}
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());...
?>
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;
}}
myfirstmicroservice architecture
Browser Cache Webserver PHP Database
3rd party 3rd party
Java,.Net,Node.js,…
ApplicationPerformanceManagement
DatabaseHealth
Chartingdatainbusinessdashboards
Whathavewelearnedtodayaboutscaling?
• Createwellsizedenvironment• Ensurehighavailability• Ensurehighperformance• Handlelargenumberoftransactions• Respondtochangedloadconditions• Respondtofeaturerequests• Findandfixproblemsfast• Monitorproperly
[email protected]@dynatrace.comhttp://blog.dynatrace.com
DynatraceFreeTrialFreePersonalLicenseRuxit FreeTrialhttp://bit.ly/monitoring-2016