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

Micro-caching in Nginx for High Performance

Micro-caching in Nginx for High Performance

A talk on optimising server side performance using a micro-caching strategy implemented in Nginx. Watch on YouTube. The sample Nginx config file from this talk is available on GitHub.

Originally delivered at the 6th edition of the Bangalore Site Speed meetup group in August 2021.

Prateek Rungta

August 30, 2021
Tweet

More Decks by Prateek Rungta

Other Decks in Technology

Transcript

  1. #perf Combine CSS & JS Minification Unused CSS CDN GZIP

    HTTP/2 Automation Bundling Caching VPS Image Optimisation DNS prefetch Responsive Images Lazy loading Image Sprites Async Service Workers Server Push Code Splitting No .htaccess
  2. 
 Kitchen 
 
 Recipe 
 Cache 
 Input 


    🍅🧀🍖 🧑🍳🍳 🍕 🍅🧀🍖 🍕
  3. 
 Kitchen 
 
 Recipe 
 Cache 
 Input 


    🍅🧀🍖 😴 🍕 🍅🧀🍖 🍕 ✅
  4. 🧑🍳🍳 
 Kitchen 
 
 Recipe 
 Cache 
 Input

    
 🍇💧 🍷 🍅🧀🍖 🍕 🍇💧 🍷
  5. Contextual responses di ff er based on the request parameters

    e.g. Mobile/desktop variants, language variants
  6. Dynamic Static Contextual Private e.g. contact form e.g. user sessions

    e.g. news article e.g. mobile/desktop variants
  7. Other Layers e.g. Database queries Network e.g. CDN Web Server

    e.g. Nginx e.g. CMS – full pages, template partials Application
  8. “There are only two hard things in Computer Science: cache

    invalidation and naming things.” Phil Karlton
  9. 
 Kitchen 
 
 Recipe 
 Cache 
 Input 


    🍅🧀🍖 🍕 🍇💧 🍷 🍞🥬🥔🧀 🍔🍟
  10. 
 CMS 
 
 HTML 
 FastCGI Cache 
 Request

    
 🍅🧀🍖 🍕 🍇💧 🍷 🍞🥬🥔🧀 🍔🍟
  11. 
 CMS 
 
 HTML 
 FastCGI Cache 
 Request

    
 🍅🧀🍖 🍕 🍇💧 🍷 🍞🥬🥔🧀 🍔🍟 ❌ Invalidate 🍕
  12. 
 CMS 
 
 HTML 
 FastCGI Cache 
 Request

    
 🍇💧 🍷 🍞🥬🥔🧀 🍔🍟 ❌ Invalidate 🍕
  13. 
 CMS 
 
 HTML 
 FastCGI Cache 
 Request

    
 🍅🧀🍖 🍇💧 🍷 🍞🥬🥔🧀 🍔🍟 ❌
  14. 
 CMS 
 ⏳⏳⏳ 
 HTML 
 FastCGI Cache 


    Request 
 🍅🧀🍖 😑 🍅🧀🍖 🍕 🍇💧 🍷 🍞🥬🥔🧀 🍔🍟
  15. 
 CMS 
 
 HTML 
 FastCGI Cache 
 Request

    
 🍅🧀🍖 🍕 🍇💧 🍷 🍞🥬🥔🧀 🍔🍟 ❌ Invalidate 🍕
  16. 
 CMS 
 
 HTML 
 FastCGI Cache 
 Request

    
 ⚠ 🍅🧀🍖 🍕 🍇💧 🍷 🍞🥬🥔🧀 🍔🍟
  17. 
 CMS 
 
 HTML 
 FastCGI Cache 
 Request

    
 🍅🧀🍖 ⚠ ⚠ 🍅🧀🍖 🍕 🍇💧 🍷 🍞🥬🥔🧀 🍔🍟
  18. 
 CMS 
 ⏳ 
 HTML 
 FastCGI Cache 


    Request 
 🍅🧀🍖 🚀 ⚠ ⚠ 🍅🧀🍖 🍕 🍇💧 🍷 🍞🥬🥔🧀 🍔🍟
  19. 
 CMS 
 ⏳⏳ 
 HTML 
 FastCGI Cache 


    Request 
 ⚠ 🍅🧀🍖 🍕 🍇💧 🍷 🍞🥬🥔🧀 🍔🍟
  20. 
 CMS 
 ⏳⏳⏳ 
 HTML 
 FastCGI Cache 


    Request 
 🍅🧀🍖 🍕 🍇💧 🍷 🍞🥬🥔🧀 🍔🍟
  21. OK — /home (expired) GET /home Visitors Nginx CMS GET

    /home OK — /home GET /home GET /home OK — /home (expired) OK — /home (expired) GET /home OK — /home (expired on arrival) 🔒⏳ 🔒⏳ GET /home
  22. OK — /home (expired) GET /home Visitors Nginx CMS GET

    /home OK — /home GET /home GET /home OK — /home (expired) OK — /home (expired) GET /home OK — /home (expired on arrival) 🔒⏳ 🔒⏳ GET /home Every request gets a fast response, no waiting
  23. OK — /home (expired) GET /home Visitors Nginx CMS GET

    /home OK — /home GET /home GET /home OK — /home (expired) OK — /home (expired) GET /home OK — /home (expired on arrival) 🔒⏳ 🔒⏳ GET /home Every request gets a fast response, no waiting CMS handles a fraction of the tra ff ic
  24. http { … server { … # Setup FastCGI Cach

    e # <Path>, <Zone>, etc . fastcgi_cache_lock on; fastcgi_cache_use_stale updating; fastcgi_cache_background_update on; location ~ \.php$ { … # Qualifiers } } }
  25. Dynamic Static Contextual Private e.g. contact form e.g. Control Panel

    e.g. news article e.g. mobile / desktop variants
  26. 1. Static – same response for each request Enable FastCGI

    Cache in the PHP location block e.g. news article
  27. 1. Static – same response for each request Enable FastCGI

    Cache in the PHP location block e.g. news article fastcgi_cache_valid 200 301 404 1s;
  28. 2. Dynamic – unique response for each request FastCGI automatically

    ignores responses with cookies e.g. contact form
  29. 2. Dynamic – unique response for each request FastCGI automatically

    ignores responses with cookies e.g. contact form fastcgi_ignore_headers Cache-Contro l Expires Set-Cookie ; fastcgi_hide_header Set-Cookie;
  30. 2. Dynamic – unique response for each request Also, bypass

    cache by sending a custom header from your CMS e.g. page that shows current time, contact form
  31. 2. Dynamic – unique response for each request Also, bypass

    cache by sending a custom header from your CMS e.g. page that shows current time, contact form # eg. (in Twig ) {% header "X-Accel-Expires: 0" %}
  32. 3. Contextual – di ff er based on request params

    Add relevant request parameters to the cache key e.g. desktop / mobile version
  33. 4. Private – bypass the cache Don’t cache POST requests

    a) Form submissions fastcgi_cache_methods GET HEAD POST;
  34. 4. Private – bypass the cache Exclude URLs with `dra

    ft ` param a) Form submissions b) Dra ft s
  35. 4. Private – bypass the cache Add a second location

    block for PHP without FastCGI caching a) Form submissions b) Dra ft s c) Control Panel
  36. location ^~ /admin { try_files $uri $uri/ @phpfpm_nocache ; }

    location @phpfpm_nocache { # PH P # no FastCGI Cach e }
  37. 4. Private – bypass the cache Bypass if the session

    cookie is set a) Form submissions b) Dra ft s c) Control Panel d) Logged-in users
  38. 4. Private – bypass the cache Bypass if the session

    cookie is set a) Form submissions ; fastcgi_no_cache $cookie_1031b8c4[…];
  39. http {
 .. . 
 # Configure FastCGI cache
 fastcgi_cache_path

    /var/run/fastcgicache levels=1:2 keys_zone=fastcgicache:100m inactive=1d ; 
 # Cache config
 fastcgi_cache_lock on;
 fastcgi_cache_use_stale updating error timeout invalid_header http_500;
 fastcgi_cache_background_update on;
 fastcgi_cache_methods GET HEAD;
 fastcgi_cache_key "$scheme$request_method$host$request_uri";
 
 server {
 ...
 
 # Ensure admin / user-specific requests skip the cache
 # using a custom location handler
 location ^~ /admin {
 try_files $uri $uri/ @phpfpm_nocache;
 } # continued... # ...continued location ~ \.php$ {
 # Enable cache
 fastcgi_cache fastcgicache;
 fastcgi_ignore_headers Cache-Control Expires;
 fastcgi_cache_valid 200 301 404 1s;
 fastcgi_cache_bypass $arg_token $cookie_session_cookie;
 fastcgi_no_cache $arg_token $cookie_session_cookie;
 # Regular PHP handling ...
 }
 
 location @phpfpm_nocache {
 # No FastCGI Cache
 fastcgi_cache_bypass 1;
 fastcgi_no_cache 1;
 # Regular PHP handling ... 
 }
 }
 }
  40. We ran some tests $ ab 
 -c 10 #concurrency

    
 -t 30 #timelimit 
 guidingtech.com
  41. • Near instant responses • Exponentially scales up capacity to

    handle tra ff ic & spikes • All* requests get fast responses • Can make a cheap VPS fly *Except the very first Advantages
  42. • Great #perf booster • Drastically reduces infrastructure cost •

    No negative impact on AX • No user or client complaints about stale content • No JavaScript usage or dependency