building high performance sites nginx, mysql, varnish and php
TRANSCRIPT
![Page 1: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/1.jpg)
Building High Performance Sites Nginx, MySQL, Varnish and Php
Tamas KozakPercona Live London 2011
![Page 2: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/2.jpg)
www.percona.com
Introduction
● IT Director at Percona ● Scaling one of the most visited (top10) site in
Hungary ● Worked at an alexa top50 company before
joining Percona
![Page 3: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/3.jpg)
www.percona.com
Agenda
● How to serve 2 million pageviews / server / day● Surviving “slashdot” effects● Scaling existing and new sites with Varnish,
Nginx, MySQL and PHP-FPM with minimal modifications
● Backend optimization only
![Page 4: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/4.jpg)
www.percona.com
Why?
● Faster page loads increase user experience● Optimizing / rewriting code is often expensive
or not an option ● Easily handle daily peak time● Lowering datacenter costs
![Page 5: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/5.jpg)
www.percona.com
Key components
● Varnish cache– Reverse proxy, flexible configuration with inline C support
● Nginx – Event based– Lightweight– Runs more than 8% of the web (Netcraft)
● PHP-FPM – Runs in standalone mode– Best FastCGI implementation available for PHP
● MySQL or Percona Server
![Page 6: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/6.jpg)
www.percona.com
Lets put it together
![Page 7: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/7.jpg)
www.percona.com
About our “testing” app
● Legacy CMS written by an external contractor● Heavy pages that are very expensive to
generate● Source code available but written many years
ago● Millions of visitors, thousands of editors
![Page 8: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/8.jpg)
www.percona.com
Benchmark without caching first
![Page 9: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/9.jpg)
www.percona.com
Fine tune defaults
● Find the optimal amount of worker processes for PHP (throughput vs response time)
● Make sure you can't run out of memory ● Test if your application is stable with APC and
turn it on if yes. ● Small changes but they mean 80% increase in
performance
![Page 10: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/10.jpg)
www.percona.com
Getting better
![Page 11: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/11.jpg)
www.percona.com
Configure caching
● Use malloc storage instead of file (no disk IO)● Define your rules using Varnish VCL● Set a header to pass original IP to the backend● FPM and PHP timeouts should match● Set grace period
![Page 12: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/12.jpg)
www.percona.com
Configure caching
● backend default { .host = "127.0.0.1"; .port = "80";}sub vcl_recv { set req.backend = default; set req.grace = 30s; remove req.http.X-Real-IP; set req.http.X-Real-IP = client.ip;
if (req.http.host ~ "test.com(:[0-9]+)?$") { if (req.url ~ "nocache") {
return (pass); } return (lookup); }
}
![Page 13: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/13.jpg)
www.percona.com
Real world benchmarking
● Necessary as we would get 100% cache hit for a simple benchmark
● Replay last items from access log● Start with cold cache for each run
![Page 14: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/14.jpg)
www.percona.com
Varnish + Nginx together
![Page 15: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/15.jpg)
www.percona.com
Benchmark individual page
● This is 100% Cache hit scenario
![Page 16: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/16.jpg)
www.percona.com
Benchmarks cont'd
● Cached content loads instantly – no waiting on the user side
● The more (similar) request it receives the faster it becomes – cache hit rate increases
● IO wait and CPU load decreases instantly● < 3 ms latency ● 5000 req/s under load avg: 1● Network will be the bottleneck● But...
![Page 17: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/17.jpg)
www.percona.com
Problems
● Session handling● Logged in users● Other dynamic parts of the site● Site content and cache can be out of sync
![Page 18: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/18.jpg)
www.percona.com
Solutions
● Disable cache for logged in users ● Faking dynamic behaviour on cached pages:
● Edge Side Includes (ESI) ● Javascript ● Caching user specific content → different users get
different content based on their session id
![Page 19: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/19.jpg)
www.percona.com
Cache invalidation
● Content purged automatically after 5 mins (default)
● Invalidate cached page when content updated● Directly via Varnish purge method● Pushing requests to a message queue
– Better for frequently changing content– Avoids too many invalidations like user saves content
after every change
![Page 20: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/20.jpg)
www.percona.com
Scaling up
● Need for more processing power● Handling different tasks with different servers
![Page 21: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/21.jpg)
www.percona.com
How?
● Add Nginx/PHP nodes (recommended)
OR● Add more PHP-FPM nodes● Balance traffic with Varnish● Store sessions centrally● Sync user generated content if needed (rsync,
inotify, glusterfs, external storage, application based solution)
![Page 22: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/22.jpg)
www.percona.com
Real world example
![Page 23: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/23.jpg)
www.percona.com
Results
![Page 24: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/24.jpg)
www.percona.com
Results
● Enough capacity to serve 6m pageviews / day● Response time remains stable under extreme
load and better response times in general– 90-150ms for cache miss– Less than 5ms for cached hit
● Users can't notice bad effects of caching● Server count reduced, less administration, and
lower costs
![Page 25: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/25.jpg)
www.percona.com
Conclusions
● Flexible and easy to scale up, scale down● Cache rules can be tricky to write● Might need to modify app to make it cacheable● Not just for start-ups where financial resources
are limited● Not for everyone: heavily updated sites need
different approach
![Page 26: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/26.jpg)
www.percona.com
Thanks to Our Sponsors
Platinum Sponsor
Gold Sponsor
Silver Sponsors
![Page 27: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/27.jpg)
www.percona.com
Percona Live London Sponsors
Exhibitor Sponsors
Friends of Percona Sponsors
Media Sponsors
![Page 28: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/28.jpg)
www.percona.com
Annual MySQL Users Conference Presented by Percona Live
The Hyatt Regency Hotel, Santa Clara, CA
April 10th-12th, 2012
Featured SpeakersMark Callaghan, Facebook
Jeremy Zawodny, Craigslist
Marten Mickos, Eucalyptus Systems
Sarah Novotny, Blue Gecko
Peter Zaitsev, Percona
Baron Schwartz, Percona
The Call for Papers is Now Open!
Visit www.percona.com/live/mysql-conference-2012/
![Page 30: Building High Performance Sites Nginx, MySQL, Varnish and Php](https://reader031.vdocuments.site/reader031/viewer/2022021815/586a1e6f1a28ab06068b6c4a/html5/thumbnails/30.jpg)
www.percona.com/live