Scaling WordPress - WordCamp Antwerp 2016

Scaling WordPress - WordCamp Antwerp 2016

Slides for my WordPress scalability talk at WordCamp Antwerp 2016. http://2016.antwerp.wordcamp.org

Ca901ddcea38854b9783781c91fc87c9?s=128

Thijs Feryn

June 04, 2016
Tweet

Transcript

  1. By Thijs Feryn

  2. 3 statements

  3. Slow websites suck

  4. Web performance is an essential part of the user experience

  5. Down Slowdown ~ downtime

  6. Performance != Scalability

  7. Performance: speed

  8. Scalability: constant speed with increasing load

  9. Improve

  10. Hi, I’m Thijs

  11. I’m @ThijsFeryn on Twitter

  12. I’m an Evangelist At

  13. I’m a at board member

  14. What’s the problem?

  15. Infrastructure

  16. Code

  17. The context

  18. ✓Tremendously popular ✓Even the big boys use it ✓Not the

    best code ✓It does the job ✓Powered by the LAMP stack The context: Wordpress
  19. Identify slowest parts

  20. Optimize database

  21. ✓More RAM ✓Faster disks ✓More database servers ✓Master-slave replication ✓Read/write

    splitting (HyperDB) ✓Query/Object caching (Redis) Optimize database
  22. Optimize runtime

  23. ✓More RAM ✓Faster disks ✓More/faster CPUs ✓More servers ✓OPCache ✓PHP-FPM

    ✓Recent PHP version ✓HHVM or PHP 7? ✓Nginx or Apache? Optimize runtime
  24. Optimize code

  25. After a while you hit the limits

  26. Optimize database Optimize runtime A void A void

  27. Don’t recompute if the data hasn’t changed

  28. Cache

  29. 3 x 2 = ?

  30. ✓WP Supercache ✓W3 Total Cache ✓Wordfence Cache ✓WP Rocket Wordpress

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

    caching? Forget about those* *sometimes
  32. Varnish

  33. Normally User Server

  34. With Varnish User Varnish Server

  35. None
  36. Stores HTTP output in memory

  37. Respects cache-control headers

  38. For page caching

  39. Could be thousands of times faster

  40. There are rules

  41. ✓Only GET & HEAD ✓No authorization headers ✓No cookies ✓No

    set-cookies ✓Valid cache-control/expires headers When does Varnish cache? Some rules …
  42. PROBLEM !!!

  43. Dealing with state

  44. Varnish Configuration Language

  45. ✓ 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
  46. 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); }
  47. Make Varnish aware of Wordpress

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

  49. Control Time To Live

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

    To Live
  51. <ifModule mod_headers.c> Header set Cache-Control "public, max-age=3600" <filesMatch "\.(7z|avi|bmp|bz2|css|csv|doc|docx|eot|flac|flv| gif|gz|ico|jpeg|jpg|js|less|mka|mkv|mov|mp3|mp4|mpeg|mpg|odt|otf|

    ogg|ogm|opus|pdf|png|ppt|pptx|rar|rtf|svg|svgz|swf|tar|tbz|tgz| ttf|txt|txz|wav|webm|webp|woff|woff2|xls|xlsx|xml|xz|zip)$"> Header set Cache-Control "public, s-maxage=2592000" </filesMatch> </ifModule> Control Time To Live Apache .htaccess
  52. add_header Cache-Control “public, s-maxage=3600;” Control Time To Live Nginx vhost

    config
  53. Purging

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

  55. Performing the purge curl -XPURGE "http://example.com/bla" Wordpress does this internally

    Uses VCL logic at the Varnish end
  56. URLs to purge / /category/uncategorised/ /author/admin/ /hello-world/ /feed/atom/ /feed/rdf/ /feed/rss/

    /feed /comments/feed/ /hello-world/feed/ Example
  57. Debugging

  58. sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache

    = "HIT"; } else { set resp.http.X-Cache = "MISS"; } } Debugging
  59. Varnishlog Varnishtop Varnishncsa Monitor Varnish behaviour

  60. What about SSL/TLS?

  61. Varnish does not support SSL/TLS

  62. You have to terminate SSL/TLS yourself

  63. Varnish 4.1 supports PROXY protocol

  64. ✓HAProxy ✓Hitch ✓Stud ✓Nginx SSL offloader

  65. Offload SSL User Varnish Server SSL offloader

  66. User SSL offloading & load balancing Varnishes Servers Load balancer

    + SSL offloader HAProxy loadbalancers
  67. Make Wordpress protocol aware define('FORCE_SSL_ADMIN', true); if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'){

    $_SERVER['HTTPS']='on'; } Header set by SSL offloader
  68. What about

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

  70. Consistent state When loadbalancer switches to another server

  71. Cheers, hope this helps!

  72. Don’t recompute if the data hasn’t changed

  73. Varnish

  74. None