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

Transcript

119 slides
  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. ๐Ÿง‘๐Ÿณ๐Ÿณ โ€จ Kitchen โ€จ โ€จ Recipe โ€จ Cache โ€จ Input

    โ€จ ๐Ÿ‡๐Ÿ’ง ๐Ÿท ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿ• ๐Ÿ‡๐Ÿ’ง ๐Ÿท
  6. Contextual responses di ff er based on the request parameters

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

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

    e.g. Nginx e.g. CMS โ€“ full pages, template partials Application
  9. โ€œThere are only two hard things in Computer Science: cache

    invalidation and naming things.โ€ Phil Karlton
  10. โ€จ Kitchen โ€จ โ€จ Recipe โ€จ Cache โ€จ Input โ€จ

    ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿ• ๐Ÿ‡๐Ÿ’ง ๐Ÿท ๐Ÿž๐Ÿฅฌ๐Ÿฅ”๐Ÿง€ ๐Ÿ”๐ŸŸ
  11. โ€จ CMS โ€จ โ€จ HTML โ€จ FastCGI Cache โ€จ Request

    โ€จ ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿ• ๐Ÿ‡๐Ÿ’ง ๐Ÿท ๐Ÿž๐Ÿฅฌ๐Ÿฅ”๐Ÿง€ ๐Ÿ”๐ŸŸ
  12. โ€จ CMS โ€จ โ€จ HTML โ€จ FastCGI Cache โ€จ Request

    โ€จ ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿ• ๐Ÿ‡๐Ÿ’ง ๐Ÿท ๐Ÿž๐Ÿฅฌ๐Ÿฅ”๐Ÿง€ ๐Ÿ”๐ŸŸ โŒ Invalidate ๐Ÿ•
  13. โ€จ CMS โ€จ โ€จ HTML โ€จ FastCGI Cache โ€จ Request

    โ€จ ๐Ÿ‡๐Ÿ’ง ๐Ÿท ๐Ÿž๐Ÿฅฌ๐Ÿฅ”๐Ÿง€ ๐Ÿ”๐ŸŸ โŒ Invalidate ๐Ÿ•
  14. โ€จ CMS โ€จ โ€จ HTML โ€จ FastCGI Cache โ€จ Request

    โ€จ ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿ‡๐Ÿ’ง ๐Ÿท ๐Ÿž๐Ÿฅฌ๐Ÿฅ”๐Ÿง€ ๐Ÿ”๐ŸŸ โŒ
  15. โ€จ CMS โ€จ โณโณโณ โ€จ HTML โ€จ FastCGI Cache โ€จ

    Request โ€จ ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿ˜‘ ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿ• ๐Ÿ‡๐Ÿ’ง ๐Ÿท ๐Ÿž๐Ÿฅฌ๐Ÿฅ”๐Ÿง€ ๐Ÿ”๐ŸŸ
  16. If we delete invalid caches, the next requester has to

    waitโ€ฆ โณโณโณ
  17. โ€จ CMS โ€จ โ€จ HTML โ€จ FastCGI Cache โ€จ Request

    โ€จ ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿ• ๐Ÿ‡๐Ÿ’ง ๐Ÿท ๐Ÿž๐Ÿฅฌ๐Ÿฅ”๐Ÿง€ ๐Ÿ”๐ŸŸ โŒ Invalidate ๐Ÿ•
  18. โ€จ CMS โ€จ โ€จ HTML โ€จ FastCGI Cache โ€จ Request

    โ€จ โš  ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿ• ๐Ÿ‡๐Ÿ’ง ๐Ÿท ๐Ÿž๐Ÿฅฌ๐Ÿฅ”๐Ÿง€ ๐Ÿ”๐ŸŸ
  19. โ€จ CMS โ€จ โ€จ HTML โ€จ FastCGI Cache โ€จ Request

    โ€จ ๐Ÿ…๐Ÿง€๐Ÿ– โš  โš  ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿ• ๐Ÿ‡๐Ÿ’ง ๐Ÿท ๐Ÿž๐Ÿฅฌ๐Ÿฅ”๐Ÿง€ ๐Ÿ”๐ŸŸ
  20. โ€จ CMS โ€จ โณ โ€จ HTML โ€จ FastCGI Cache โ€จ

    Request โ€จ ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿš€ โš  โš  ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿ• ๐Ÿ‡๐Ÿ’ง ๐Ÿท ๐Ÿž๐Ÿฅฌ๐Ÿฅ”๐Ÿง€ ๐Ÿ”๐ŸŸ
  21. โ€จ CMS โ€จ โณโณ โ€จ HTML โ€จ FastCGI Cache โ€จ

    Request โ€จ โš  ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿ• ๐Ÿ‡๐Ÿ’ง ๐Ÿท ๐Ÿž๐Ÿฅฌ๐Ÿฅ”๐Ÿง€ ๐Ÿ”๐ŸŸ
  22. โ€จ CMS โ€จ โณโณโณ โ€จ HTML โ€จ FastCGI Cache โ€จ

    Request โ€จ ๐Ÿ…๐Ÿง€๐Ÿ– ๐Ÿ• ๐Ÿ‡๐Ÿ’ง ๐Ÿท ๐Ÿž๐Ÿฅฌ๐Ÿฅ”๐Ÿง€ ๐Ÿ”๐ŸŸ
  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
  24. 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
  25. 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
  26. 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 } } }
  27. Dynamic Static Contextual Private e.g. contact form e.g. Control Panel

    e.g. news article e.g. mobile / desktop variants
  28. 1. Static โ€“ same response for each request Enable FastCGI

    Cache in the PHP location block e.g. news article
  29. 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;
  30. 2. Dynamic โ€“ unique response for each request FastCGI automatically

    ignores responses with cookies e.g. contact form
  31. 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;
  32. 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
  33. 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" %}
  34. 3. Contextual โ€“ di ff er based on request params

    Add relevant request parameters to the cache key e.g. desktop / mobile version
  35. 4. Private โ€“ bypass the cache Donโ€™t cache POST requests

    a) Form submissions fastcgi_cache_methods GET HEAD POST;
  36. 4. Private โ€“ bypass the cache Exclude URLs with `dra

    ft ` param a) Form submissions b) Dra ft s
  37. 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
  38. location ^~ /admin { try_files $uri $uri/ @phpfpm_nocache ; }

    location @phpfpm_nocache { # PH P # no FastCGI Cach e }
  39. 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
  40. 4. Private โ€“ bypass the cache Bypass if the session

    cookie is set a) Form submissions ; fastcgi_no_cache $cookie_1031b8c4[โ€ฆ];
  41. 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 ... โ€จ }โ€จ }โ€จ }
  42. We ran some tests $ ab โ€จ -c 10 #concurrency

    โ€จ -t 30 #timelimit โ€จ guidingtech.com
  43. โ€ข 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
  44. โ€ข 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

More Decks by Prateek Rungta

Other Decks in Technology