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/6577

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

It3works3on3my3computer

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

It’s3the3internet’s3fault

Slide 12

Slide 12 text

Suits & hipsters

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

GeBng3the3numbers3right

Slide 20

Slide 20 text

Let’s3do3a3complete3rewrite3!

Slide 21

Slide 21 text

Have3you3got3the3Gme?

Slide 22

Slide 22 text

Or3the3money?

Slide 23

Slide 23 text

What3do3you3do?

Slide 24

Slide 24 text

Nothing

Slide 25

Slide 25 text

Throw3 servers3at3 the3 problem

Slide 26

Slide 26 text

Do3some3 tricks3!

Slide 27

Slide 27 text

Throw3servers3at3the3problem

Slide 28

Slide 28 text

Scale3verGcally

Slide 29

Slide 29 text

Fast Cheap vs Limited Expensive Long% term Short% term

Slide 30

Slide 30 text

Scale3horizontally

Slide 31

Slide 31 text

Efficient Unlimited vs ConsideraGons IniGal3effort Long% term Short% term

Slide 32

Slide 32 text

What3kind3of3infrastructure3do3you3need?

Slide 33

Slide 33 text

What3kind3of3infrastructure3do3you3need? One3big3 box?

Slide 34

Slide 34 text

What3kind3of3infrastructure3do3you3need? Scale3across3 1003nodes?

Slide 35

Slide 35 text

What3kind3of3infrastructure3do3you3need? That3big3F53 loadbalancer?

Slide 36

Slide 36 text

What3kind3of3infrastructure3do3you3need? Only3the3DB3 gets3 hammered

Slide 37

Slide 37 text

What3kind3of3infrastructure3do3you3need? Lots3of3staGc3 traffic

Slide 38

Slide 38 text

What3kind3of3infrastructure3do3you3need? Shared3hosGng3 will3do3...3 except3during3the3 month3of3the3event

Slide 39

Slide 39 text

About3a3mulGYserver3setup

Slide 40

Slide 40 text

Goal:3(almost)3no3code3changes

Slide 41

Slide 41 text

Cache

Slide 42

Slide 42 text

Why?

Slide 43

Slide 43 text

Is3caching3 the3 answer?

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

What? When3not? How3long? CompaGbility Limited3size

Slide 46

Slide 46 text

Tricks

Slide 47

Slide 47 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 48

Slide 48 text

Use3mod_rewrite Write3output3to3file3and3print3to3screen

Slide 49

Slide 49 text

APC

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

Auto3prepend Read3from3cache3or3start3output3buffer

Slide 52

Slide 52 text

Auto3append Close3output3buffer,3store3data3in3cache3&3 print3output

Slide 53

Slide 53 text

Data3cache Caching3layer3 before3on3top3 of3the3 database

Slide 54

Slide 54 text

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

Slide 55

Slide 55 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 56

Slide 56 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 57

Slide 57 text

You3can3also3use3Memcached

Slide 58

Slide 58 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 59

Slide 59 text

Reverse3 (caching)3 proxies

Slide 60

Slide 60 text

Put3‘em3before3 your3 webserver

Slide 61

Slide 61 text

They3honor3 your3caching3 headers

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 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 64

Slide 64 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 65

Slide 65 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 66

Slide 66 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 67

Slide 67 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 68

Slide 68 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 69

Slide 69 text

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

Slide 70

Slide 70 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 71

Slide 71 text

No content

Slide 72

Slide 72 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 73

Slide 73 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 74

Slide 74 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 75

Slide 75 text

No content

Slide 76

Slide 76 text

Content Delivery Network

Slide 77

Slide 77 text

+3 W33Total3 Cache

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

apc MAGE_

Slide 84

Slide 84 text

memcached

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Boost Varnish Memcached

Slide 87

Slide 87 text

Other3tricks3...

Slide 88

Slide 88 text

Mess3with3the3hosts3file Test3load3&3change3docroot Play3with3Apache3&3MySQL3 seBngs Add3a3database3index Enable3MySQL3query3caching

Slide 89

Slide 89 text

Don’t3be3clueless

Slide 90

Slide 90 text

Ask3for3help

Slide 91

Slide 91 text

Apply3the3tricks

Slide 92

Slide 92 text

And3don’t3forget3to3refactor3alerwards

Slide 93

Slide 93 text

And3then3relax3...3and3be3happy

Slide 94

Slide 94 text

No content