Slide 1

Slide 1 text

By Thijs Feryn

Slide 2

Slide 2 text

3 statements

Slide 3

Slide 3 text

Slow websites suck

Slide 4

Slide 4 text

Web performance is an essential part of the user experience

Slide 5

Slide 5 text

Down Slowdown ~ downtime

Slide 6

Slide 6 text

Performance != Scalability

Slide 7

Slide 7 text

Performance: speed

Slide 8

Slide 8 text

Scalability: constant speed with increasing load

Slide 9

Slide 9 text

Improve

Slide 10

Slide 10 text

Hi, I’m Thijs

Slide 11

Slide 11 text

I’m @ThijsFeryn on Twitter

Slide 12

Slide 12 text

I’m an Evangelist At

Slide 13

Slide 13 text

I’m a at board member

Slide 14

Slide 14 text

What’s the problem?

Slide 15

Slide 15 text

Infrastructure

Slide 16

Slide 16 text

Code

Slide 17

Slide 17 text

The context

Slide 18

Slide 18 text

✓Tremendously popular ✓Even the big boys use it ✓Not the best code ✓It does the job ✓Powered by the LAMP stack The context: Wordpress

Slide 19

Slide 19 text

Identify slowest parts

Slide 20

Slide 20 text

Optimize database

Slide 21

Slide 21 text

✓More RAM ✓Faster disks ✓More database servers ✓Master-slave replication ✓Read/write splitting (HyperDB) ✓Query/Object caching (Redis) Optimize database

Slide 22

Slide 22 text

Optimize runtime

Slide 23

Slide 23 text

✓More RAM ✓Faster disks ✓More/faster CPUs ✓More servers ✓OPCache ✓PHP-FPM ✓Recent PHP version ✓HHVM or PHP 7? ✓Nginx or Apache? Optimize runtime

Slide 24

Slide 24 text

Optimize code

Slide 25

Slide 25 text

After a while you hit the limits

Slide 26

Slide 26 text

Optimize database Optimize runtime A void A void

Slide 27

Slide 27 text

Don’t recompute if the data hasn’t changed

Slide 28

Slide 28 text

Cache

Slide 29

Slide 29 text

3 x 2 = ?

Slide 30

Slide 30 text

✓WP Supercache ✓W3 Total Cache ✓Wordfence Cache ✓WP Rocket Wordpress caching?

Slide 31

Slide 31 text

✓WP Supercache ✓W3 Total Cache ✓Wordfence Cache ✓WP Rocket Wordpress caching? Forget about those* *sometimes

Slide 32

Slide 32 text

Varnish

Slide 33

Slide 33 text

Normally User Server

Slide 34

Slide 34 text

With Varnish User Varnish Server

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Stores HTTP output in memory

Slide 37

Slide 37 text

Respects cache-control headers

Slide 38

Slide 38 text

For page caching

Slide 39

Slide 39 text

Could be thousands of times faster

Slide 40

Slide 40 text

There are rules

Slide 41

Slide 41 text

✓Only GET & HEAD ✓No authorization headers ✓No cookies ✓No set-cookies ✓Valid cache-control/expires headers When does Varnish cache? Some rules …

Slide 42

Slide 42 text

PROBLEM !!!

Slide 43

Slide 43 text

Dealing with state

Slide 44

Slide 44 text

Varnish Configuration Language

Slide 45

Slide 45 text

✓ Remove all cookies & set-cookies from static assets ✓ Remove all tracking cookies ✓ Don't cache if wordpress_ or comment_ cookies are present ✓ Or just remove all non-Wordpress cookies ✓ Don't cache wp-login or wp-admin pages ✓ Don't cache preview pages ✓ Normalize URL ✓ Anonimize HTTP output ✓ Provide PURGE logic Required VCL changes

Slide 46

Slide 46 text

vcl 4.0; import std; backend default { .host = "127.0.0.1"; .port = "8080"; .max_connections = 300; .first_byte_timeout = 100s; .connect_timeout = 10s; .between_bytes_timeout = 5s; } acl purge { "192.168.33.10"; "localhost"; "127.0.0.1"; "::1"; } sub purge_regex { ban("obj.http.X-Req-URL ~ " + req.url + " && obj.http.X-Req-Host == " + req.http.host); } sub purge_exact { ban("obj.http.X-Req-URL == " + req.url + " && obj.http.X-Req-Host == " + req.http.host); } sub purge_page { set req.url = regsub(req.url, "\?.*$", ""); ban("obj.http.X-Req-URL-Base == " + req.url + " && obj.http.X-Req-Host == " + req.http.host); }

Slide 47

Slide 47 text

Make Varnish aware of Wordpress

Slide 48

Slide 48 text

https://gist.github.com/ ThijsFeryn/ aabd6d7ed425fb6614279e836d 9848c5 Download VCL here

Slide 49

Slide 49 text

Control Time To Live

Slide 50

Slide 50 text

sub vcl_backend_response { set beresp.ttl = 3h; } Control Time To Live

Slide 51

Slide 51 text

Header set Cache-Control "public, max-age=3600" Header set Cache-Control "public, s-maxage=2592000" Control Time To Live Apache .htaccess

Slide 52

Slide 52 text

add_header Cache-Control “public, s-maxage=3600;” Control Time To Live Nginx vhost config

Slide 53

Slide 53 text

Purging

Slide 54

Slide 54 text

https://wordpress.org/ plugins/varnish-http-purge/ Plugin that hooks into the CMS Invalidates cache

Slide 55

Slide 55 text

Performing the purge curl -XPURGE "http://example.com/bla" Wordpress does this internally Uses VCL logic at the Varnish end

Slide 56

Slide 56 text

URLs to purge / /category/uncategorised/ /author/admin/ /hello-world/ /feed/atom/ /feed/rdf/ /feed/rss/ /feed /comments/feed/ /hello-world/feed/ Example

Slide 57

Slide 57 text

Debugging

Slide 58

Slide 58 text

sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT"; } else { set resp.http.X-Cache = "MISS"; } } Debugging

Slide 59

Slide 59 text

Varnishlog Varnishtop Varnishncsa Monitor Varnish behaviour

Slide 60

Slide 60 text

What about SSL/TLS?

Slide 61

Slide 61 text

Varnish does not support SSL/TLS

Slide 62

Slide 62 text

You have to terminate SSL/TLS yourself

Slide 63

Slide 63 text

Varnish 4.1 supports PROXY protocol

Slide 64

Slide 64 text

✓HAProxy ✓Hitch ✓Stud ✓Nginx SSL offloader

Slide 65

Slide 65 text

Offload SSL User Varnish Server SSL offloader

Slide 66

Slide 66 text

User SSL offloading & load balancing Varnishes Servers Load balancer + SSL offloader HAProxy loadbalancers

Slide 67

Slide 67 text

Make Wordpress protocol aware define('FORCE_SSL_ADMIN', true); if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'){ $_SERVER['HTTPS']='on'; } Header set by SSL offloader

Slide 68

Slide 68 text

What about

Slide 69

Slide 69 text

✓Object caching ✓Session storage ✓Stanalone ✓Replicated ✓Clustered Redis

Slide 70

Slide 70 text

Consistent state When loadbalancer switches to another server

Slide 71

Slide 71 text

Cheers, hope this helps!

Slide 72

Slide 72 text

Don’t recompute if the data hasn’t changed

Slide 73

Slide 73 text

Varnish

Slide 74

Slide 74 text

No content