Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Evangelist

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

@ThijsFeryn

Slide 7

Slide 7 text

Please3 rate3my3talk3on3 Joind.in https://joind.in/6247

Slide 8

Slide 8 text

It3works3on3my3computer

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

It’s3the3internet’s3fault

Slide 11

Slide 11 text

Suits & hipsters

Slide 12

Slide 12 text

“Isn't'hos*ng' supposed'to'be'a' bulk'product?”

Slide 13

Slide 13 text

“The'Cloud'will' make'sure'my'app' scales”

Slide 14

Slide 14 text

“Large'campaigns' on'shared'hos*ng,' why'is'that'a' problem?”

Slide 15

Slide 15 text

“What's'a' database'index?”

Slide 16

Slide 16 text

“We'll'solve'that' with'an'Ajax'call”

Slide 17

Slide 17 text

“We'll'probably'have' 10.000'visitors'on' the'next'campaign”

Slide 18

Slide 18 text

GeBng3the3numbers3right

Slide 19

Slide 19 text

Let’s3do3a3complete3rewrite3!

Slide 20

Slide 20 text

Have3you3got3the3Gme?

Slide 21

Slide 21 text

Or3the3money?

Slide 22

Slide 22 text

What3do3you3do?

Slide 23

Slide 23 text

Nothing

Slide 24

Slide 24 text

Throw3 servers3at3 the3 problem

Slide 25

Slide 25 text

Do3some3 tricks3!

Slide 26

Slide 26 text

Throw3servers3at3the3problem

Slide 27

Slide 27 text

Scale3verGcally

Slide 28

Slide 28 text

Fast Cheap vs Limited Expensive Long% term Short% term

Slide 29

Slide 29 text

Scale3horizontally

Slide 30

Slide 30 text

Efficient Unlimited vs ConsideraGons IniGal3effort Long% term Short% term

Slide 31

Slide 31 text

What3kind3of3infrastructure3do3you3need?

Slide 32

Slide 32 text

What3kind3of3infrastructure3do3you3need? One3big3 box?

Slide 33

Slide 33 text

What3kind3of3infrastructure3do3you3need? Scale3across3 1003nodes?

Slide 34

Slide 34 text

What3kind3of3infrastructure3do3you3need? That3big3F53 loadbalancer?

Slide 35

Slide 35 text

What3kind3of3infrastructure3do3you3need? Only3the3DB3 gets3 hammered

Slide 36

Slide 36 text

What3kind3of3infrastructure3do3you3need? Lots3of3staGc3 traffic

Slide 37

Slide 37 text

What3kind3of3infrastructure3do3you3need? Shared3hosGng3 will3do3...3 except3during3the3 month3of3the3event

Slide 38

Slide 38 text

About3a3mulGYserver3setup

Slide 39

Slide 39 text

Goal:3(almost)3no3code3changes

Slide 40

Slide 40 text

Cache

Slide 41

Slide 41 text

Frontend Backend Data% cache Opcode% cache Page% cache APC Memcached SQLite File Redis +more +more

Slide 42

Slide 42 text

What? When3not? How3long? CompaGbility Limited3size

Slide 43

Slide 43 text

Tricks

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

Use3mod_rewrite Write3output3to3file3and3print3to3screen

Slide 46

Slide 46 text

APC

Slide 47

Slide 47 text

Auto3prepend3&3auto3append AutomaGcally3executed3before3every3request [PHP] auto_prepend_file*“/path/to/prepend.php” auto_append_file*“/path/to/append.php”

Slide 48

Slide 48 text

Auto3prepend Read3from3cache3or3start3output3buffer

Slide 49

Slide 49 text

Auto3append Close3output3buffer,3store3data3in3cache3&3 print3output

Slide 50

Slide 50 text

Data3cache Caching3layer3 before3on3top3 of3the3 database

Slide 51

Slide 51 text

_mysqlDependency = $mysqlDependency; } public function fetchQueryFromDatabase($key) { return $this->_mysqlDependency- >fetchQueryEscaped("SELECT * FROM `myTable` WHERE `myKey` = '%s'",$key); } } No% cache

Slide 52

Slide 52 text

_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

Slide 53

Slide 53 text

... 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

Slide 54

Slide 54 text

You3can3also3use3Memcached

Slide 55

Slide 55 text

You3can3also3use3Memcached memcached.sess_prefix = "memc.sess.key." session.save_path="127.0.0.1:11211" session.save_handler = memcached Also% for%session% handling

Slide 56

Slide 56 text

Reverse3 (caching)3 proxies

Slide 57

Slide 57 text

Put3‘em3in3 front3of3your3 webserver

Slide 58

Slide 58 text

They3honor3 your3caching3 headers

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

Request vcl_recv In cache? vcl_hash Cacheable? vcl_hit() vcl_miss() vcl_deliver() vcl_fetch() No Yes No Yes Response

Slide 61

Slide 61 text

backend default { .host = "127.0.0.1"; .port = "8080"; } acl purge { "localhost"; "127.0.0.1"; "some.host.name.com"; "1.2.3.4"; }

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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; } }

Slide 64

Slide 64 text

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; }

Slide 65

Slide 65 text

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; } }

Slide 66

Slide 66 text

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"; } }

Slide 67

Slide 67 text

FPM hfp://www.php.net/manual/en/install.fpm.configuraGon.php

Slide 68

Slide 68 text

[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

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

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; }

Slide 71

Slide 71 text

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"; } }

Slide 72

Slide 72 text

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"; } }

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

Content Delivery Network

Slide 75

Slide 75 text

+3 W33Total3 Cache

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

✓Enable3caching ✓Enable3cron.php ✓Configure3log3retenGon ✓Flat3catalog ✓Enable3compilaGon ✓Install3Varnish3Cache3plugin ✓Split3frontend3&3backend3servers ✓Perform3indexing3on3separate3 server

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

Boost Varnish Memcached

Slide 80

Slide 80 text

Don’t3be3clueless

Slide 81

Slide 81 text

Ask3for3help

Slide 82

Slide 82 text

Apply3the3tricks

Slide 83

Slide 83 text

And3don’t3forget3to3refactor3akerwards

Slide 84

Slide 84 text

And3then3relax3...3and3be3happy

Slide 85

Slide 85 text

No content