Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Evozon Full Page Cache for Magento

Evozon Full Page Cache for Magento

The technology stack behind Evozon's Full Page Cache solution for Magento.

Constantin Bejenaru

October 01, 2015
Tweet

Other Decks in Programming

Transcript

  1. Target audience Both technical and business people techies: • learn

    about the technology stack • hopefully gain new knowledge • critique and improve business guys: • speed up your website • rank higher in search engines • save big money • keep your customers happy
  2. How it all started Real Case Scenario: Evozon client •

    retail store chain • around 20.000 products • catalog and promotions updated many times a day • global stock - warehouse and around 80 physical shops • everything managed from the ERP
  3. The challenge • Nginx • PHP-FPM with APC • Magento

    EE with FPC • Percona Server (MySQL) • Solr • Redis + Memcached • slow response times • extremely high CPU and RAM usage • index problems • services crashed • angry customers • etc.
  4. Then we started to Speed up Magento Tweak services Find

    a solution • research other FPCs • research other technologies • benchmark • proof of concept • monitor • external audit • hardware resources • tweak PHP-FPM • reconfigure network • no cache persistence • monitor • refactor code • optimize queries • block and container cache • FPC invalidation • remove unused features • research • cache more data
  5. PHP & Magento • wrong language • wrong place •

    slow • big resource consumer Varnish • no SSL/TLS support • steep learning curve • still need a web server and an SSL terminator • researched VMOD’s not stable • existing Magento FPC extensions painful • selective invalidation too complex Approaches we didn’t like
  6. Goals Fast and stable • like really fast • predictable

    and reliable Feature complete • all features of Magento CE and EE available • simple to add, extend and overwrite features Adaptable and scalable • easily adapt to existing architecture • scale the cache system Simple and straightforward • quick learning curve • developer friendly
  7. Nginx? • Reverse proxying, FastCGI, uwsgi, SCGI, etc. • SMTP,

    POP3 and IMAP • Balancing with health checks and fault tolerance • Filters - gzip, chunked responses, XSLT, SSI, etc. • SSL and TLS support • SPDY, WebSockets and HTTP/2 support • Simultaneous connections and request limiting • Bandwidth throttling • Media streaming • Modular and scriptable: C, Perl, Lua, JavaScript Nginx is a web server with a strong focus on high concurrency, performance and low memory usage. It can also act as a reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer and an HTTP cache.
  8. Caching in Nginx Nginx has fastcgi_cache respectively proxy_cache to store

    full pages. fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2 keys_zone=MAGENTO:300m inactive=12h; fastcgi_cache MAGENTO; fastcgi_cache_key "$scheme://$host$uri$is_args$args";
  9. User specific content (aka hole punching) Some information cannot be

    cached! Customer name, cart information and other small areas need to be injected. • Varnish has ESI (Edge Side Includes), Nginx has SSI (Server Side Includes) • Commands: block, config, echo, if/elseif/else, include and set <!--# include virtual="/remote/body.php?argument=value" -->
  10. Under the hood Nginx will... • fetch the full page

    from cache or backend • make subrequests to Magento and retrieve blocks • assemble everything • return the complete page
  11. There are some drawbacks fastcgi_cache / proxy_cache ❌ no support

    for tagging ❌ saves cache on disk ❌ needs NFS on multiple servers ❌ not cool for conditional cache purging ◦ gather every single URL for purging hole punches ❌ save one request but add more subrequests ❌ still consuming resources ❌ can be cached, but needs different backend ❌ lots of Nginx or Magento scripting
  12. We want Easy integration • work as web server or

    reverse proxy • minimal configuration Aggressive but consistent • cache pages and hole punches • same backend and same logic Simplified invalidation • invalidation for modified content only • tagging Familiarity • do as much as possible in Magento • customise according to your needs
  13. Hole punch caching has some rules • Nginx makes inexpensive

    subrequests for SSI • Subrequest caching disallowed • Lua for subrequest caching is allowed
  14. Meet Lua Lua is a powerful, fast, lightweight and embeddable

    scripting language. It has a deserved reputation for performance. To claim to be "as fast as Lua" is an aspiration of other scripting languages.
  15. ✓ ngx_lua's cosocket API ✓ 100% non-blocking behavior ✓ fast

    and memory efficient ✓ full scripting flexibility Why Lua and not other modules?
  16. Let’s talk HTTP We store, then read HTTP responses: •

    status line • headers • body HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Date: Thu, 30 Jul 2015 13:19:25 GMT <html> <body> <h1>Hello World!</h1> </body> </html> How to read and write the cache
  17. Magento can do that! Because it doesn’t matter who saves

    the cache! • offers more control • configurable and extensible • familiar Mage_Core_Model_Cache • tagging support • easy invalidation • adapter for Redis and Memcached • write your own storage adapter
  18. Cache Processors • routes, hole punched blocks and widgets •

    controls cache storage • determines cache key and cache tags • cache lifetime configurable • extendable and flexible <catalog> <product> <view> <processor>evozon_fpc/processor_product</processor> <cache_options> <cache_lifetime>86000</cache_lifetime> </cache_options> </view> </product> </catalog>
  19. Hole punch placeholders • some blocks are hole punched •

    controlled via cache processor • placeholders added automatically • private content blocks cached per user • public content blocks cached and shared • AJAX blocks possible, but not cached <cart_sidebar> <placeholder>evozon_fpc/placeholder_private</placeholder> <container>evozon_fpc/container_block</container> <cache_options> <cache_lifetime>86000</cache_lifetime> </cache_options> </cart_sidebar>
  20. Cache invalidation • selective invalidation • full cache flushing on

    request • mostly by tags • automatically invalidates related content (category, products and related products, etc.) • most entities invalidated by index /** @var Mage_Catalog_Model_Product $product */ $product = Mage::getModel('catalog/product ')->load($productId); /** @var Mage_Core_Model_Cache $cache */ $cache->clean( $product->getCacheIdTags() );
  21. CSRF Protection There is Stateless CSRF Defence! If you don’t

    hit Magento, you fake it! But we don’t!
  22. How about external cache control? We respect and honor HTTP

    requests ! Bypass cache: • Cache-Control: no-cache • Pragma: no-cache Don’t store cache: • Cache-Control: no-store <nostore> <http> <get> <nocache/> <nostore/> <___SID/> <___store/> <___from_store/> </get> <header> <Cache-Control> < no-store/> </ Cache-Control> <X-Blackfire-Query /> </header> <cookie> <nocache/> <nostore/> </cookie> </http> </nostore>
  23. We did it! ✓ Extremely fast ✓ Stable ✓ All

    Magento CE & EE features ✓ Easy to extend and configure ✓ Scale in and out independently ✓ Developer friendly ✓ It works for you, not against you
  24. This is what we offer Magento Extension • clean and

    well written architecture • mostly event observer based • minimal configuration • full API documentation (phpDoc) • code strictly analysed by phpmd, phpcs and phpcpd • no introduced performance bottlenecks (blackfire.io) Nginx + Lua • sample VHost configuration ◦ regular web server and balancer ◦ reverse proxy and balancer • minimal configuration • Lua libraries and modules ◦ 3rd party libraries ◦ solution libraries and modules • full API documentation (LuaDoc)
  25. Our drawbacks (I hate this part) • Logging and built-in

    Magento reports are fragmented ◦ if you’re still using them, there’s Google Analytics and Co. • The built-in sitemap generator does not provide full path URL’s ◦ if you rely on the sitemap for cache warm-up, get yourself a better generator • You may need to increase number of connections to Redis/Memcached
  26. Our first benchmarks 1. Nginx + PHP-FPM ◦ 8 CPU

    cores ◦ 8 Gb RAM ◦ configurations optimised 2. MySQL ◦ 2 CPU cores ◦ 4 Gb RAM 3. Redis ◦ 2 CPU cores ◦ 2 Gb RAM • Magento Performance Toolkit ◦ 3 websites ◦ 3 store groups ◦ 3 store views ◦ 475.000 simple products ◦ 25.000 configurable products ◦ 1000 categories (nesting level 3) ◦ 50 catalog price rules ◦ 10 catalog target rules ◦ 50 cart price rules ◦ 2.000 registered customers • Apache jMeter ◦ 4 CPU cores ◦ 8 Gb RAM