Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Scalability Issues ... cure first, prevent later
Search
Thijs Feryn
June 09, 2012
Technology
2
780
Scalability Issues ... cure first, prevent later
Slides for the DPC12 edition of my scalability talk.
Thijs Feryn
June 09, 2012
Tweet
Share
More Decks by Thijs Feryn
See All by Thijs Feryn
Caching the uncacheable with Varnish - PHP London 2020
thijsferyn
0
390
Accelerating OTT video platforms with Varnish - London Video Tech meetup 2020
thijsferyn
0
330
't Oncachebare cachen
thijsferyn
0
290
Caching the uncacheable with Varnish - PHP UG FFM 19
thijsferyn
1
610
Developing cacheable PHP applications - PHP Barcelona 2019
thijsferyn
0
570
Caching the uncacheable with Varnish - FullstackEU 2019
thijsferyn
0
430
Varnish beyond basic web acceleration - Symfony Live Berlin 2019
thijsferyn
0
350
Developing cacheable PHP applications
thijsferyn
0
360
Varnish beyond basic web acceleration - DAHO.AM 2019
thijsferyn
0
350
Other Decks in Technology
See All in Technology
開発生産性を上げながらビジネスも30倍成長させてきたチームの姿
kamina_zzz
2
1.7k
OTelCol_TailSampling_and_SpanMetrics
gumamon
1
130
障害対応指揮の意思決定と情報共有における価値観 / Waroom Meetup #2
arthur1
5
470
TanStack Routerに移行するのかい しないのかい、どっちなんだい! / Are you going to migrate to TanStack Router or not? Which one is it?
kaminashi
0
590
20241120_JAWS_東京_ランチタイムLT#17_AWS認定全冠の先へ
tsumita
2
250
10XにおけるData Contractの導入について: Data Contract事例共有会
10xinc
6
630
BLADE: An Attempt to Automate Penetration Testing Using Autonomous AI Agents
bbrbbq
0
300
OCI Network Firewall 概要
oracle4engineer
PRO
0
4.1k
個人でもIAM Identity Centerを使おう!(アクセス管理編)
ryder472
3
200
IBC 2024 動画技術関連レポート / IBC 2024 Report
cyberagentdevelopers
PRO
0
110
インフラとバックエンドとフロントエンドをくまなく調べて遅いアプリを早くした件
tubone24
1
430
AWS Media Services 最新サービスアップデート 2024
eijikominami
0
200
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
243
12k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Embracing the Ebb and Flow
colly
84
4.5k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
120
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
410
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Transcript
None
Evangelist
None
None
None
@ThijsFeryn
Please3 rate3my3talk3on3 Joind.in https://joind.in/6247
It3works3on3my3computer
None
It’s3the3internet’s3fault
Suits & hipsters
“Isn't'hos*ng' supposed'to'be'a' bulk'product?”
“The'Cloud'will' make'sure'my'app' scales”
“Large'campaigns' on'shared'hos*ng,' why'is'that'a' problem?”
“What's'a' database'index?”
“We'll'solve'that' with'an'Ajax'call”
“We'll'probably'have' 10.000'visitors'on' the'next'campaign”
GeBng3the3numbers3right
Let’s3do3a3complete3rewrite3!
Have3you3got3the3Gme?
Or3the3money?
What3do3you3do?
Nothing
Throw3 servers3at3 the3 problem
Do3some3 tricks3!
Throw3servers3at3the3problem
Scale3verGcally
Fast Cheap vs Limited Expensive Long% term Short% term
Scale3horizontally
Efficient Unlimited vs ConsideraGons IniGal3effort Long% term Short% term
What3kind3of3infrastructure3do3you3need?
What3kind3of3infrastructure3do3you3need? One3big3 box?
What3kind3of3infrastructure3do3you3need? Scale3across3 1003nodes?
What3kind3of3infrastructure3do3you3need? That3big3F53 loadbalancer?
What3kind3of3infrastructure3do3you3need? Only3the3DB3 gets3 hammered
What3kind3of3infrastructure3do3you3need? Lots3of3staGc3 traffic
What3kind3of3infrastructure3do3you3need? Shared3hosGng3 will3do3...3 except3during3the3 month3of3the3event
About3a3mulGYserver3setup
Goal:3(almost)3no3code3changes
Cache
Frontend Backend Data% cache Opcode% cache Page% cache APC Memcached
SQLite File Redis +more +more
What? When3not? How3long? CompaGbility Limited3size
Tricks
RewriteEngine*On RewriteCond*%{REQUEST_FILENAME}*=s*[OR] RewriteCond*%{REQUEST_FILENAME}*=l*[OR] RewriteCond*%{REQUEST_FILENAME}*=d RewriteRule*^.*$*=*[NC,L] RewriteRule*^.*$*index.php*[NC,L] Use3mod_rewrite Hit3frontcontroller3if3resource3doesn’t3exist
Use3mod_rewrite Write3output3to3file3and3print3to3screen <?php require('db.php'); $data = db_get_content_from_uri( $_SERVER['REQUEST_URI']); $fp =
fopen(dirname(__FILE__). $_SERVER['REQUEST_URI'], 'w'); fwrite($fp, $data); fclose($fp); echo $data;
APC
Auto3prepend3&3auto3append AutomaGcally3executed3before3every3request [PHP] auto_prepend_file*“/path/to/prepend.php” auto_append_file*“/path/to/append.php”
Auto3prepend Read3from3cache3or3start3output3buffer <?php if($_SERVER['REQUEST_METHOD'] == 'GET'){ $key = md5('http://'.$_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI']);
if(($output = apc_fetch($key)) !== false){ echo $output; exit(); } ob_start(); }
Auto3append Close3output3buffer,3store3data3in3cache3&3 print3output <?php if($_SERVER['REQUEST_METHOD'] == 'GET'){ $output = ob_get_contents();
ob_end_clean(); $key = md5('http://'.$_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI']); apc_store($key,$output,20); echo $output; }
Data3cache Caching3layer3 before3on3top3 of3the3 database
<?php class Data { private $_mysqlDependency; public function __construct($mysqlDependency) {
$this->_mysqlDependency = $mysqlDependency; } public function fetchQueryFromDatabase($key) { return $this->_mysqlDependency- >fetchQueryEscaped("SELECT * FROM `myTable` WHERE `myKey` = '%s'",$key); } } No% cache
<?php class Data { private $_namespace = 'myNamespace_'; private $_ttl
= 3600; private $_mysqlDependency; public function __construct($mysqlDependency) { $this->_mysqlDependency = $mysqlDependency; } public function fetchQueryFromDatabase($key) { if(($value = apc_fetch(md5( $this->_namespace.$key))) === false){ $value = $this->_fetchDataFromDatabase($key); $this->_storeDataInCache($key,$value); } return $value; } ... Override% &%use%cache Read% from%DB%&% store%in%cache
... private function _storeDataInCache($key,$value) { apc_store(md5($this->_namespace.$key),$value, $this->_ttl); return $this; }
private function _fetchQueryFromDatabase($key) { return $this->_mysqlDependency- >fetchQueryEscaped("SELECT * FROM `myTable` WHERE `myKey` = '%s'",$key); } } Fetch% from%DB Store% in%cache
You3can3also3use3Memcached
You3can3also3use3Memcached memcached.sess_prefix = "memc.sess.key." session.save_path="127.0.0.1:11211" session.save_handler = memcached Also% for%session%
handling
Reverse3 (caching)3 proxies
Put3‘em3in3 front3of3your3 webserver
They3honor3 your3caching3 headers
None
Request vcl_recv In cache? vcl_hash Cacheable? vcl_hit() vcl_miss() vcl_deliver() vcl_fetch()
No Yes No Yes Response
backend default { .host = "127.0.0.1"; .port = "8080"; }
acl purge { "localhost"; "127.0.0.1"; "some.host.name.com"; "1.2.3.4"; }
sub vcl_recv { if (req.request == "PURGE") { if (!client.ip
~ purge) { error 405 "Not allowed: " + client.ip; } return(lookup); } if (req.request != "GET" && req.request != "HEAD" && req.request != "PUT" && req.request != "POST" && req.request != "TRACE" && req.request != "OPTIONS" && req.request != "DELETE") { return (pipe); }
if (req.request != "GET" && req.request != "HEAD") { return
(pass); } if (req.http.Accept-Encoding) { if (req.url ~ "\.(gif|jpg|jpeg|swf|flv|mp3|mp4| pdf|ico|png|gz|tgz|bz2)(\?.*|)$") { remove req.http.Accept-Encoding; } else if (req.http.Accept-Encoding ~ "gzip") { set req.http.Accept-Encoding = "gzip"; } else if (req.http.Accept-Encoding ~ "deflate"){ set req.http.Accept-Encoding = "deflate"; } else { remove req.http.Accept-Encoding; } }
if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3| mp4|pdf|ico|png)(\?.*|)$") { unset req.http.cookie; set req.url
= regsub(req.url, "\?.*$", ""); return (lookup); } if (req.url ~ "\?(utm_(campaign|medium|source|term)| adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er| iew))=") { set req.url = regsub(req.url, "\?.*$", ""); } if (req.http.cookie ~ "^ *$") { unset req.http.cookie; }
sub vcl_fetch { if (req.url ~ "wp-(login|admin)" || req.url ~
"preview=true" || req.url ~ "xmlrpc.php") { return (hit_for_pass); } if ( (!(req.url ~ "(wp-(login|admin)|login)"))) { unset beresp.http.set-cookie; set beresp.ttl = 1h; } if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3| mp4|pdf|ico|png)(\?.*|)$") { set beresp.ttl = 365d; } }
sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache
= "HIT"; } else { set resp.http.X-Cache = "MISS"; } } sub vcl_hit { if (req.request == "PURGE") { purge; set obj.ttl = 0s; error 200 "OK"; } } sub vcl_miss { if (req.request == "PURGE") { purge; error 200 "OK"; } }
FPM hfp://www.php.net/manual/en/install.fpm.configuraGon.php
[www] listen = 9000 user = www-data group = www-data
pm = dynamic pm.max_children = 10 pm.start_servers = 4 pm.max_spare_servers = 6 pm.max_requests = 500 php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f
[email protected]
php_flag[display_errors] = off php_admin_value[error_log] = /var/log/fpm-php.www.log php_admin_flag[log_errors] = on php_admin_value[memory_limit] = 32M
None
upstream fpm { server php1.server.com:9000; server php2.server.com:9000; } upstream memcached
{ server memcached1.server.com:11211; server memcached2.server.com:11211; } server { root /var/www; index index.php index.html index.htm; server_name nginx.server.com; location / { try_files $uri $uri/ /index.php; } location @php { fastcgi_pass fpm; fastcgi_index index.php; include fastcgi_params; }
location ~ \.php$ { set $memcached_key $request_uri; memcached_pass memcached; memcached_next_upstream
not_found; default_type text/html; error_page 404 405 502 = @php; } location ~* \.(?:ico|css|js|gif|jpe?g|png)$ { expires max; add_header Pragma public; add_header Cache-Control "public, must- revalidate, proxy-revalidate"; } }
location ~ \.php$ { set $memcached_key $request_uri; memcached_pass memcached; memcached_next_upstream
not_found; default_type text/html; error_page 404 405 502 = @php; } location ~* \.(?:ico|css|js|gif|jpe?g|png)$ { expires max; add_header Pragma public; add_header Cache-Control "public, must- revalidate, proxy-revalidate"; } }
None
Content Delivery Network
+3 W33Total3 Cache
None
✓Enable3caching ✓Enable3cron.php ✓Configure3log3retenGon ✓Flat3catalog ✓Enable3compilaGon ✓Install3Varnish3Cache3plugin ✓Split3frontend3&3backend3servers ✓Perform3indexing3on3separate3 server
None
Boost Varnish Memcached
Don’t3be3clueless
Ask3for3help
Apply3the3tricks
And3don’t3forget3to3refactor3akerwards
And3then3relax3...3and3be3happy
None