Scale	your	PHP	web	app	to	get	
ready	for	the	peak	season
harald.zeitlhofer@dynatrace.com
@HZeitlhofer
Harald	Zeitlhofer
• Technology	Strategist	at	Dynatrace
• Database	and	Web	Development
• Love	to	discover	new	things
Our	online	shop	is	too	slow!
We	need	to	scale up!!!
Performance	vs	Scalability
Does	performance	matter?
Scalability	is	more
• Create	well	sized	environment
• Ensure	high	availability
• Ensure	high	performance
• Handle	large	number	of	transactions
• Respond	to	changed	load	conditions
• Respond	to	feature	requests
• Find	and	fix	problems	fast
?
Bad	News
PHP is slower than Java
PHP is not the
bottleneck
Good	News
Prepare	to	scale
• Break	your	application	down	into	smaller	components
• Frontend
• Load	Balancer
• Web	Server
• Caching	Server
• PHP	engine
• Database
• Avoid	bottlenecks
• Reduce	load
Web	Request	handling
cached	content
can	still	create	roundtrips
to	the	network!
Split	web	server	and	PHP	engine
http://www.slideshare.net/HaraldZeitlhofer/boost-your-website-by-running-php-on-nginx
Load	balancing
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;
}
}
Optimize	first	?
Example:	
“we	have	to	catch	more	mice”
So	far	we	only	got	two	cats…
Focus	on	what’s	needed
Do	not	over-optimize
PHP tier represents
a major global bottleneck
for page load times
Example:	Performance	hotspot	PHP	execution
lessc CSS	
preprocessor
social login
module
slow PHP	
execution
Major	performance	hotspots
less	CSS	preprocessor
server-side calls to
unused external services
Social	Login	Module
average contribution of
PHP compilation time
Slow	PHP	execution?	Outdated	PHP	version?
7:00	a.m.
Low	Load	and	Service	running
on	minimum	 redudancy
12:00	p.m.
Scaled	up	service	during	peak	load
with	failover	of	problematic	node	
7:00	p.m.
Scaled	down	again	to	lower	load
and	move	to	different	geo	location
Deployments	are	no	longer	static
Performance	problem	after	upscaling
Database	Access
• Size	server	(cluster)	appropriately	for	maximum	number	of	connected	
instances
• Reduce	connections
• Reduce	SQL	statements
• Leverage	caching
Caching	!=	Caching
Client	Side	Caching
Server	Side	Caching
Varnish
Varnish
Nginx	FastCGI cache
• Part	of	Nginx'	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;
}
Data	cache	with	Memcached
<?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());
...
?>
Access	cached	data	with	Nginx
upstream 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;
}
}
my	first	microservice architecture
Browser Cache Webserver PHP Database
3rd party 3rd party
Java,	.Net,	
Node.js,	…
Application	Performance	Management
Database	Health
Charting	
data	in	
business	
dashboards
What	have	we	learned	today	about	scaling?
• Create	well	sized	environment
• Ensure	high	availability
• Ensure	high	performance
• Handle	large	number	of	transactions
• Respond	to	changed	load	conditions
• Respond	to	feature	requests
• Find	and	fix	problems	fast
• Monitor	properly
Harald	Zeitlhofer
Performance	Advocate
@HZeitlhofer
harald.zeitlhofer@dynatrace.com
http://blog.dynatrace.com
Dynatrace	Free	Trial
Free	Personal	License
Ruxit Free	Trial
http://bit.ly/monitoring-2016

Scaling PHP web apps